Technical · .NET Development

Unit Tests in Visual Studio Professional

July 14, 2007

.NET Development

Unit Tests in Visual Studio Professional

The concept of unit testing has been developing a whole new programming model, test-based models (TDD for Test Driven Development) have demonstrated their benefits, among which can quickly be counted the confidence in the generated code, the ability to do refactoring much faster and with more confidence, and the reduction in debugging times.


Visual Studio Professional version 2005 is one of the most used development tools in the world, and the preferred tool of developers on the Microsoft platform, however development teams that do not use Visual Studio Team System Developer or Tester edition do not have the possibility to do unit tests Out-Of-The-Box, this guide will help to configure Visual Studio Professional for the execution of unit tests.


1. Preparing the Environment.

The first thing is to download a unit testing framework, although there are several I recommend the use of NUnit, in our tests it has worked quite well, it has consistent functionality and it is easy to configure and use. Download it and install it from the following link: (For the NUnit Framework (.NET)) http://www.nunit.org/

Installing nUnit you will be able to write, execute and have results of unit tests, but still from outside of Visual Studio. For the integration with the IDE of Visual Studio you must install an Add-in that you can download from this address: http://www.testdriven.net/, this will allow the execution of unit tests, with debugging, coverage and other characteristics from the Visual Studio IDE environment itself.


2. Writing the Test Code.

With the environment ready we can create the Tests project for our assembly, I recommend creating another project to be able to have clear independence between the code project and the tests project, an additional advantage of doing it this way is that you can upload the tests project to your version control system. (An additional recommendation has to do with the nomenclature of the namespace, create a tests namespace for your project).

In the new created project you must add a reference to the nunit.framework.dll assembly that you will find in the nUnit 2.4/Bin folder, in the program files location or where you have installed the nUnit framework.

You must also add a reference to the project or assembly you want to test.

Now you can write a unit tests class that will look like this:


Imports NUnit.Framework
Imports TestClass = NUnit.Framework.TestFixtureAttribute
Imports TestMethod = NUnit.Framework.TestAttribute
Imports TestInitialize = NUnit.Framework.SetUpAttribute
Imports TestCleanup = NUnit.Framework.TearDownAttribute

<TestClass()> _
Public Class eMailTest

end Class.


As can be seen here, aliases have been used for the framework attributes that facilitate their use in the class, these aliases will allow the nunit framework to identify the tests class, the test methods, the initialization and finalization methods of the testing process.

These aliases will allow decorating the class as in the previous example where the class has been decorated with the TestClass attribute

The class is ready, now we can build test methods, a basic test method would look similar to this:

<TestMethod()> _
Sub AddFolderTest()

…Su Código aqui.

End Sub

Now with the right mouse button on the method or the test class you can execute the unit test.

Now, the idea of this little guide is not to reproduce the entire nunit manual but there are some tips that may be interesting, 1. Use the assert functions within your test methods, 2. in the cases in which the test must return an exception use the expectedexception decoration to indicate to nunit which is the type of exception it expects, 3. You can configure the test environment using a method decorated as TestInicialize and clean everything using TestCleanup.

In future articles I will talk about 1. How to use DB to execute automatic test suites simulating the functionality of Visual Studio Team System and 2. how to create a test model using Enterprise Services that at the end of the tests performs a rollback of all the test data inserted.

I hope it is useful.

Juan Carlos Peláez

MCTS

Keywords: Visual Studio Team System, Visual Studio Proffesional 2005, nUnit, Best Practices, Methodology, Microsoft, VB, Unit Tests, Test-based Development, Microsoft Colombia Events.

Need Expert Help?

Let's discuss how I can help with your technical challenges

Schedule a Consultation

Get the next one in your inbox

More like this, once a week. The Digest covers AI systems and agents in one weekly edition.

Confirm by email to complete your subscription. Unsubscribe anytime.

Related reading

More in the Digest — a weekly roundup of AI and agents news — or start with AI Strategy. Want help with this? Tell me about your needs →