Tips, tricks and all the info about Microsoft Visual Studio 2008

Unit testing with Visual Studio 2008

, posted: 2-Apr-2008 09:00

One of the great improvements of Visual Studio 2008 brought to developers is the built in support for unit testing. With Unit Testing support, it is very easy for developer to create, execute and repeat unit test cases.

Setting up a test project in Visual Studio 2008 is extremely easy. All it requires is adding a test project into a solution by selecting Test Project template under Test project type.


 
A test class is generated once the Test project is created. Visual studio automatically mark the test class and method with TestClassAttribute and TestMethodAttribute



Adding a unit test
It is recommended to create one test class per class to be tested. A test class can be added by either selecting from Add New Test popup or by selecting Unit Test from Add menu. 
 


Alternatively, Unit test can be added by right clicking the method name and select “Create Unit Tests” in context menu.  This brings up the Create Unit Tests screen where multiple methods to be tested can be selected.
 


By default Visual Studio 2008 appends “UnitTest” in front of ClassName as the test file name and class name and “Test” in front of MethodName as the test method name - bearing in mind these are all configurable by clicking Settings button located on the left bottom corner of the popup.


 
Unit testing support for ASP.NET website/application
Visual Studio 2008 allows developers create test cases for methods defined in ASP.NET website or applications. Even better, tests are running under ASP.NET context which means settings from Web.config file are automatically picked up once the test case starts running.

This is really handy for applications that contain a lot of applications settings such as database connections, logging configurations, Active Directory etc as duplicating all the settings in the testing project is no longer needed. The following code illustrates what the test method looks like. (this test method just for demo, I can’t imagine anyone would unit test a button click event handler.)


 
It worth pointing out two attributes HostTypeAttribute and UrlToTestAttribute. HostType makes sure that test case is running ASP.NET host process. UrlToTest specifies URL to test when test is run.

If the application requires authentication, there might be problem relates to VSEnterpriseHelper.axd when test is run. This is due to the test context is not authenticated. To solve the problem, permission to access VSEnterpriseHelper.axd need to be granted by adding following code in Web.config file.


 
Execute test method
Test method can be started by right clicking the test method name and select Run Tests. Visual Studio 2008 displays test results in Test Result output window.
 


If all test cases in a single test class need to be run, just simply right click the test class name select Run Tests from the context menu. The results look like this:


 
Debugging support in Unit Testing
All test classes/methods can be run under debug mode. Simply put a break point where the code needs to stop and start the test by clicking “Debug Tests in Current Context” button located in the toolbar or “Tests in Current Context” menu of Debug option under Test menu.


 
Execute tests in a predefined order
In real life, it is mostly likely for a test project contains a set of test methods which form test cases. Making sure test methods are run in a particular order could be a pain. Visual Studio 2008 allows developers to predefine an Ordered Test in which the sequence of test methods is run.

An ordered test can be added into a test project either by selecting Ordered Test template from Add New Test popup or add Ordered Test directly from Add option of a test project:


 

Adding an Ordered Test brings up an Ordered Test Editor where the test methods are included and the order is specified.


 

In the Ordered Test Editor, test methods can be selected from the Available tests list then added into to the Selected tests list by clicking the right arrow.

To run the ordered test, go to the Test list editor check the newly created ordered test. Click the Run checked test button located on the top left corner of the Test list editor.


 
Conclusion
Visual Studio 2008 makes creating and managing unit testing real easy. On top of what has been discussed here, it provides configuration support for deployment, setup/cleanup and host etc.  Compare with previous versions of Visual Studio, this is definitely one step forward.

Download Visual Studio 2008 90 day trial
For detailed information and to request a free 90-day trial DVD of Visual Studio 2008 Team Suite to be sent out to you, go to the Microsoft Visual Studio webpage.

About the Author
Nathan Li is a senior .NET developer at Datacom. Over the past few years, Nathan has been designing and delivering enterprise solutions in various industries. Recently, he is working as a technical lead on a solution in .NET 3.5. He is also running the Datacom .NET user group. He organises and presents in monthly seminars around leading edge Microsoft technologies.

Other related posts:
The New Zealand ALM Conference 2011 (Application Life Cycle Management)
Writing your own Html Helpers for the ASP.NET MVC Framework
Automating Visual Studio 2008








Comment by Param, on 10-Oct-2008 00:20

nice article. good introduction to how unit test works in VS 2008


Comment by Srini, on 9-Nov-2008 18:07

Hey

I was running into the VSEnterprisehelper.axd problem u mentioned. But it didnt get solved with the solution you mentioned.

Any help is appreciated? Also i wasnt able to find this axd file anywhere in my drive. i am running VS 2008 SP1 with TFS.


Comment by Trambak Swami, on 5-Mar-2009 23:08

really a Good Article to read and understand test project structure in Visual Studio 2008 .


Comment by sanniv, on 20-May-2009 22:56

i was looking for VS unit testing tutorial. i found this blog very useful becoz of its simplicity and clear instructions step by step with appropriate snapshots...


Comment by Brian Link, on 17-Oct-2009 03:31

You seem to be incorrect in one of your opening remarks - it seems that web.config is not picked up by unit tests. For instance, if I write a test that calls a method in my web app that uses a ConnectionString in web.config, that method can't find the ConnectionString.


Comment by gil ziny, on 22-Feb-2010 04:30

When I associate the unit test with TFS testing policy, if fails during check-in, but pass when I generate it from within the project.

Is it a bug?


Comment by zefo, on 2-Mar-2010 00:08

Just minor recommendation: better use *.png for the screenshots. Jpeg is more suitable for photos. PNG would be for this article much better choice. But overall, very useful article. Thank you.


About the Visual Studio 2008 Blog

In the years since the hugely successful release of Visual Studio 2005, Microsoft has used developer feedback from all over the world to introduce over 250 new features in the 2008 release.

This sponsored blog will bring Visual Studio 2008 tips and tricks from well known developers in the Australian and New Zealand tech community directly to you.

Make sure you bookmark this blog or subscribe to our RSS feed.



Other blog posts

The New Zealand ALM Conference...
Writing your own Html Helpers ...
Automating Visual Studio 2008...
Windows Azure Table Storage - ...
Windows Azure Table Storage - ...
XML and XSLT in Visual Studio ...
Introduction to New SQL Server...
Using the Visual Studio 2008 P...
Getting started with Windows A...
Why consider sitting for an MC...


Some recent comments

Praveen M on Visual Studio 2008 support for building SilverLigh: I installed SILVERLIGHT TOOLS for VS2008 at my work. I am able to do silverlight...

zefo on Unit testing with Visual Studio 2008: Just minor recommendation: better use *.png for the screenshots. Jpeg is more su...

gil ziny on Unit testing with Visual Studio 2008: When I associate the unit test with TFS testing policy, if fails during check-in...

lynette on Visual Studio 2008 JavaScript Debugging: i have enable in IE the ability to debug scripts. if i go to my section in my c...

Junkie on Why consider sitting for an MCTS or MCP Certificat: The way you presented the case for certification has been completely different f...

Brian Link on Unit testing with Visual Studio 2008: You seem to be incorrect in one of your opening remarks - it seems that web.conf...

Dipanwaya on Unit testing .Net Compact Framework applications w: Steps Behind the Test in the VS 2008? How to test a module in the VS 2008 Envir...

Ricardo on Visual Studio 2008 support for building SilverLigh: Yes! Found the solution myself :) Reset the environment of Visual Web Developer...

Ricardo on Visual Studio 2008 support for building SilverLigh: Have the same problem after installing Visual Web Developer 2008 SP1 and Silverl...

kjward on Automating Visual Studio 2008: thanks lots for all the great info. i'm sure the answer to my question is in th...



Disclaimer

The Visual Studio 2008 blog is sponsored by Microsoft NZ. The blog posts are the authors' genuine accounts of their experiences with Visual Studio 2008 and are not influenced or filtered by Microsoft NZ in any way.