Creating Selenium Tests that work with both NUnit and MSTest

This website is no longer actively supported

Written by John DeVightJohn DeVight on 20 Jun 2011 17:52

Download Sample Project

Overview

I use CruiseControl.NET to run my nightly builds. CruiseControl.NET executes a NAnt script to build the application and then run the unit tests. I found that a NAnt script running Selenium unit tests with NUnit works great, but I could not get an NAnt script running Selenium unit tests with MSTest to work. However, I still wanted to run my Selenium unit tests from within Visual Studio so that I could easily debug my Selenium unit tests. I looked around for a solution on the internet and the Visual Studio plugins that I found for NUnit that claimed to support debugging didn't work well. I came to the conclusion that the easiest and fastest way to to support both NUnit and MSTest was to add a conditional compiler statement at the top of each test class file. If the application is compiled in debug, then the MSTest namespace was referenced and the Microsoft attribute test classes were aliased to the NUnit attribute test classes. If the application is compiled in release, then the NUnit namespace was referenced.

Here is the conditional compiler statement:

#if DEBUG
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TestFixture = Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute;
using SetUp = Microsoft.VisualStudio.TestTools.UnitTesting.TestInitializeAttribute;
using TearDown = Microsoft.VisualStudio.TestTools.UnitTesting.TestCleanupAttribute;
using Test = Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute;
#else
using NUnit.Framework;
#endif

Implementing the support for NUnit and MSTest in the Sample Project

To demonstrate this, I took the sample application from Creating Selenium Tests for Multiple Browsers and added the conditional compiler statement to the AspNetWikiTests.cs file. However, when I tried to run the tests from within Visual Studio, I got the following message:

No tests are run because no tests are loaded or the selected tests are disabled.

I did a search on the internet, and found an article, Manually creating a MS Test Project, that had the solution. Because I originally created the Visual Studio project as a Class Library and not a Test Project, there were two "project type guids" that were not added to the project file. I followed the instructions from the article and added the following ProjectTypeGuids element to the first ProjectGroup element:

<ProjectTypeGuids>
    {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}
</ProjectTypeGuids>

After that, I was able to debug the Selenium tests from within Visual Studio and run the Selenium tests using NUnit on the CruiseControl.NET continuous build server.

References

Discussion Closed

Add a New Comment

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License