Configuring a Visual Studio Project ready to run unit tests

Create Plumbing

First off, create a blank SharePoint Solution Package Project with a Feature (inc. Feature Receiver). You can use your Productivity tool of choice for this, STSDev is used in this instance.


Ensure you select 'Simple Feature Solution (C# assembly)

Scope the Feature at 'Site' for this sample and ensure the tick box is clicked to get the blank Feature Reciever file.

It's always best to test that your environment is working correctly before you go forward. So deploy the empty Solution Package to your development environment to ensure there are no errors. For example, with the STSDev created project, select 'DebugDeploy' from Build Targets and Build your Visual Studio Solution.

The result in the Output window in Visual Studio should state that it succeeded e.g.:

========== Build: 1 succeeded or up-to-date, 0 failed, 0 skipped ==========

Creating a Test Visual Studio Project

Now that you have the plumbing for the Solution Package with a Feature and the Feature Receiver class. You can now add a Test Project, simply create a Class Library Project by going 'File | New Project' in Visual Studio. Select the 'Class Library' Project and enter a name for your project and click 'OK' button.

It is recommended to append '.Test' after the project name in order to keep the project and its namespace isolated from the actual non test code.

Add references to Unit Test Framework

Now you need to add the references to the Unit Test Framework in the Test Project. Right click on the 'References' folder in the Solution Explorer for the Visual Studio Project and click 'Add Reference...'. Select 'nunit.framework' as shown below:

Add references to Mocking Framework

Now you need to add the references to the Mocking Framework in the Test Project. Right click on the 'References' folder in the Solution Explorer for the Visual Studio Project and click 'Add Reference...'. Select 'Typemock Isolator' and 'Typemock.ArrangeActAssert' as shown below:

Add using statements in classes
For each class file you want to add Unit Test Framework and Mocking Framework objects to, you will need the using statements at the top of the page.
using NUnit.Framework;
using TypeMock;
using TypeMock.ArrangeActAssert;

Integrate test run with Visual Studio

The best way to do this is to have a command to run NUnit Console from the Tools Menu. You can add this by selecting 'Tools|External Tools' and adding these settings:

Setting Name Value
Title &NUnit Console
Command C:\Program Files\NUnit 2.4.8\bin\nunit-x86.exe
Arguments /run $(ProjectDir)/$(ProjectFileName)
Initial Directory $(ProjectDir)\bin\Debug

Then you can simply select 'Tools|NUnit Console' and it'll fire off NUNit Console and run the tests it finds in the Test Project. Note you have to have the Test Project selected as the Start Up project in Solution Explorer to get the context of the project.

Labels

nunit nunit Delete
typemock typemock Delete
unittest unittest Delete
visualstudio visualstudio Delete
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.

Creative Commons License
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License. Hosted generously by CustomWare