Skip to main content

VWD Express 2010 Unit Tests with xUnit and MVC Templates

Microsoft's Visual Web Developer 2010 Express (VWD 2010 Express), by default does not come with a built in unit testing solution. However the ability is there to use unit tests with the Express version as can be seen when creating a new MVC project.


Tech spec stuff:
Microsoft Windows 7
Microsoft Visual Studio 2010 Express, Version 10.0.40219.1 SP1
Microsfot .NET Framework Version 4.0.30319 SP1

Getting started with xUnit
xUnit is a "developer testing framework, built to support Test Driven Development (TDD)". There are other unit testing frameworks available (MSTest, nUnit, etc.). There are pro's to each and some better than others but xUnit is pretty straightforward to get up and running with the VWD 2010 Express version.

Download and Install xUnit
The xUnit project can be found at: http://xunit.codeplex.com/. Downloads are available from http://xunit.codeplex.com/releases/view/62840.

Download the recommended version (currently 1.8 at time of writing), which is just a zip file and just extract the files to a suitable directory e.g. C:\Program Files\xunit-1.8.


Run the xunit.installer file.
This will execute the installer utility for xUnit.net where options can be selected to integrate xUnit with various environments. Select the required options (e.g. Windows Explorer integration, MVC2 templates, MVC3 templates), then click apply.
Explorer and/or VWD Express may need to be re-started.

Creating a Test Project
Now, when starting VWD Express 2010 and creating a new project, the option to "Create a unit test project" should be available to be checked (compare to first image where option is unavailable).

The test project name and test framework should be autofilled as shown in the following image.

Clicking OK will create the new project along with a project for the unit tests, which will contain MVC unit testing templates.

Creating a Simple Test
To demonstrate the xUnit GUI, this is just a simple example test, (2 + 2 should equal 4) based on the example from "How Do I Use xUnit.net" at http://xunit.codeplex.com/wikipage?title=HowToUse&referringTitle=Home.

A new class is created in the test project.


The line "using Xunit;" needs to the added for the class along with the actual code for the test:


Save the file and build the project. Note where the dll for the test project was created.
In this case the test dll is created in:
d:\dev\projects\MvcApplication2\MvcApplication2.Tests\bin\Debug\MvcApplication2.Tests.dll.

Using xUnit GUI
Locate the xunit.gui file which should be where the zip was extracted too earlier on. If using .NET framework v4, "xunit.gui.clr4" needs to be used to run the tests otherwise an error about loading the assembly may be thrown, along the lines of:

"Could not load file or assembly 'sometest.dll' or one of its dependencies. This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded."

To run the tests select "Assembly > Open" and browse to where the tests dll is. 

Once opened each test should be listed within the Methods window. These tests include the MVC framework tests and also the example test created above.

Clicking Run Selected (or Run All if no individual test is created) will run the tests. An icon will appear by the tests in the method window and any failure results will be displayed in the output window.

Showing an example of a failed test, the line Assert.Equal(4, 2+2) is changed to Assert.Equal(4, 2+3), which will blatantly fail as 2+3 does not equal 4.


The failed test is shown in the output window.

Comments

Popular posts from this blog

EF CodeFirst Database.SetInitializser Requires Connection to 'master' Database Error

One of the features of EF CodeFirst is the ability to automatically drop and recreate a database if the model changes or if the database does not exist, which is pretty useful when just doing development work. In a web app this is done in the Global.asax file within "protected void Application_Start()" as per this post from Scott Guthrie -  http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx . An example would be: protected void Application_Start() { Database.SetInitializer<yourdbcontex>(new DropCreateDatabaseIfModelChanges<yourdbcontext>()); AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); } However when using the Database.SetInitializer and trying to rebuild the database, the following exception may be encountered, especially if using SQL Server Express: "This operation requires a connection to the 'master'...

BlogEngine.NET Security Error After Publishing to IIS7 Web Server

After publishing the initial default set-up of BlogEngine.NET ( http://www.dotnetblogengine.net/ ) to an IIS 7 web server (i.e. not running under debug mode from Visual Studio), the following security exception may appear, stating : System.Security.SecurityException: Request for the permission of type 'System.Web.AspNetHostingPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed. BlogEngine.NET Security Exception To get BlogEngine.NET running, the "User Profile" of the application pool that the application runs under in IIS needs to be updated. IIS 7 Application Pools The "Load User Profile", accessed through advanced settings of the application pool, needs to be set to true.  Application Pool Advanced Settings - Load User Profile That should be it to get a proper response from BlogEngine.NET on the server. Of course don't forget to add write permissions for the app_data folder. Thanks to  David Burke ...