Tuesday, March 10, 2009

Unit testing in Visual Studio 2008 Team System - part IV

Next (maybe not last) part re units tests inside VS.

[Ignore()]

Useful if one of test methods failed – you can use [Ignore()] attribute to decide later what to do with that test

So, our complete class code looks now as follow:







Refactoring

If we look at above code we can see lot of code duplication – refactoring is necessary.

We can appoint common block of code, it is worth to split tests on small 1 function methods.

To achieve that we will:

  • Define local members (source + destination)
  • Add Init() method marked by attribute [TestInitialize()] – executed before each tests
    (please note method need to be public)
  • Break down test methods on 3 atomic methods

After all our code shall look as follow:





Labels:

Unit testing in Visual Studio 2008 Team System - part III

In previous post Unit testing in Visual Studio 2008 Team System - part II I’ve described how to perform units tests using new test functionality in VS2008 TS.

In this part I will continue describing test functionality (base on nUnit guide’s example)


In Part II I’ve showed how to use Assert methods to evaluate tests.

Asert() is not only one way to perform tests. Units testing gives also possibility to examine if expected exception occurred.


Let’s return to nUnit example.

[ExpectedException(typeof(InsufficientFundsException))]


If we assume that minimum acceptable account’s balance can be 10F, and if balance is less custom exception (named “InsufficientFundsException“) will be throw.

In that case we can add attribute to our test method informing that we expecting exception:
[ExpectedException(typeof(InsufficientFundsException))]


So, we need to archive this is


1) implement own Exception class:

public class InsufficientFundsException : ApplicationException {}


2) correct orrect our test method:






3) correct our production Account.TransferFunds() method:









Labels:

Unit testing in Visual Studio 2008 Team System - part II

In previous post (http://csharpmatters.blogspot.com/2009/03/unit-testing-in-visual-studio-2008-team.html) I’ve described new test functionality in VS2008 TS.

In this post I drill down unit tests inside VS2008 more detail.


VS built-in units tests use attributes (similar to NUnit).

Attributes are used to denote various test methods and test classes.


Common attributes:


[TestClass()] mark class representing a unit test case. Class need to be public.

[TestMethod()]marks Test Method


Another popular attributes:


[ClassInitialize()] –code need to be run before running the first test in the class

[ClassCleanup()] – code need to be run after all tests in class

[TestInitialize()] – code need to be run before each test

[TestCleanup ()] – code need to be run after each test


Example:


[TestClass()]
public class MyClassTest

[TestMethod()]
public void MyClassInitTest()

VS provides Assert class with static methods to check various conditions.

All Asserts methods accept 2 parameters: Boolean condition and exception message provided by user. If condition is FALSE: test exception with custom msg is thrown.


More about assert methods here: http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.assert_members.aspx


If you worked with nUnit tool you can see a lot of similarities.

(Please note UI testing is not supported in the current version of unit testing with VS.NET 2008)

Let’s follow nUnit example (http://www.nunit.org/index.php?p=quickStart&r=2.4.7) but using VS built-in tests this time.


Production code example:


Test Class code:


After test run you will see that test failed with message:

TransferFundsTest UnitTests Assert.AreEqual failed. Expected:<250>. Actual:<150>.

All because production method TransferFunds() has no implementation yet.

Let’s correct that as follows:


public void TransferFunds ( Account destination, float amount ) {

destination.Deposit( amount );

Withdraw(amount);

}

After that correction test is passed.


Please find more about testing in next part(s) of post

Labels:

Unit testing in Visual Studio 2008 Team System

As you probably noticed, VS.NET 2008 comes with unit testing integrated within.

There two ways to create test:

1) From existing, production code

2) Manually

Obviously option 1 requires less work, and I’m going to describe this scenario.

Quick start:

  1. Open your existing project
  2. Right click on the source and select “Create Unit Tests”
  3. Select methods you want to create tests
  4. (Optionally) Change settings (by clicking on “Settings..” button
  5. Click OK to create new test project


After all new test project will be created and added to your solution.


Now you can change run your tests (Menu Test \ Run)

TIP: you can double click on test result record to see more detail view.


What's also very useful: you can also add some brakpoints and start debuging test classes.


Of course auto generated tests are not very valuable there are inconclusive (at least)

You need to change test methods according to unit tests art – but most of dirty work was done by VS!


In next posts I will try explain more about unit testing.


Keep reading!



Labels: