How To Write Unit Tests in Visual Studio Express
In an earlier blog post I described how to use Nunit for unit testing in Visual Studio Express. One short coming of this strategy is that you can’t easily debug your tests. This is because Visual Studio Express doesn’t seem to let you attach external processes. (We would need to attach the external Nunit process to step into the code).
To get around this rather annoying problem, I have created my own testing framework; ExpressUnit. The current version of ExpressUnit is now 4.0. The latest version offers several new benefits over previous versions:
- The new UI interface has been created using WPF
-
ExpressUnit 4.0 supports continuous integration with Hudson which means you can use ExpressUnit to run tests on your build server. In my current development environment I have configured ExpressUnit to run all tests on every code check-in. My continuous Integration environment is currently set up using Subversion, Hudson and ExpressUnit.
-
ExpressUnit 4.0 now supports multithreaded execution of tests
-
ExpressUnit 4.0 now supports running Selenium web tests out of the box
Instead of relying on an external application to run the tests, ExpressUnit will run from within your Visual Studio Solution. The idea is that you download ExpressUnit in the form of a Visual Studio project and add it to your solution.
The added project becomes your test project where you may add your test classes with belonging tests. No configuration is required.
To add tests, simply add a test class with public test methods. The class and methods must be decorated with the [TestClass] and [UnitTest]/[IntegrationTest]/[WebTest] attributes respectively. The unit test attribute should be used for unit tests and the Integration test attribute is meant for integration tests. The new Web test atttribute is used to tag a test as a web test (more on than later). ExpressUnit will let you filter test runs on these attributes.
It’s usually my preference to add all my tests to the ExpressUnit project, but ExpressUnit allows for tests to be defined in any project in your solution (as long as the ExpressUnit project has a reference to it).
Anyway, that’s enough background information! To make this as useful as possible I will demonstrate how to use ExpressUnit through an example.
Step 1:
Download ExpressUnitGui from ExpressUnit
Step 2:
Add the ExpressUnitGui project to your already existing Visual Studio solution.
After completing this step, your solution will look similar to Figure1
Figure 1
Step 3:
Start writing your first ExpressUnit test.
As mentioned earlier, you must start by adding a test class with at least one test method to the ExpressUnitGui project. The class may look something like the following:
Step 4:
Run your test!
To run your tests, select ExpressUnitGui as your startup project and press F5
The UI window will load with a simple interface (Figure2). You may run entire test suits or individual test by right clicking the appropriate tree node in the tree view. To run all tests at once, all you have to do is click the green "Run" button. The test results are shown in the bottom pane, and as you can see in Figure2, each test result offers an expand button to view more details about failing tests.
There is also an option to run all tests automatically on start-up of ExpressUnit. This feature is controlled by the runTestsOnStartup flag in the application’s App.Config (Default value is false)
Figure 2
Multithreading
To boost performance, ExpressUnit now supports multihreaded test execution. The degree of multi-threading is controlled through configuration, and by setting the degreeOfParallelism to n, your tests will execute using up to n threads, depending on your hardware.
API
The following is an overview of the API offered by ExpressUnit
Confirm Methods
The confirm class contains several static methods used to confirm the outcome of your tests
Confirm.Equals(object obj1, object obj2):
This method compares two objects for equality by using the object’s own Equals implementation.
Confirm.Equals will throw EqualsException if the two objects are not equal.
Confirm.Different(object obj1, object obj2):
This method is the inverse of Confirm.Equals, and is used to check if two objects are unequal.
UnEqualityException is thrown if the two objects are equal.
Confirm.IsGreater(long/double val1, long/double val2)
This method is used to test if val1 is numerical bigger than val2
Confirm.SameCollections(IEnumerable param1, IEnumerable param2)
This method will test if two objects of type IEnumerable are the "same", and the definition of "same" is that all contained objects are equal.(based on the contained object’s own Equal implementation. For example, it may be used to compare the content of two arrays or collections.
EqualityException is thrown if the two instances are not the "same".
Attributes
[TestClass]
This attribute defines a class as a test class.
[UnitTest]
This attribute defines a method as a unit test
[IntegrationTest]
This attribute defines a method as an integration test
[WebTest]
This attribute defines a method as a web test
[Ignore]
This attribute will cause a test to be ignored when running your tests
[ExceptionThrown(Type exceptionType)]
This attribute can be put on a test to indicate that an exception of type exceptionType is expected to be thrown. The test will fail if the exception is not thrown.
Conclusion
If this this article has made you curious about ExpressUnit, you may download it from
ExpressUnitYou may also want to check out my article on running Selenium web tests in ExpressUnit: How to Execute Selenium Tests in Multiple Threads
Don’t hesitate to drop me a line if you have comments or would like to see additional functionality in ExpressUnit.
Feel free to comment on this article!
Your name
Title
Your comment