Visual Studio 2012/2013 TestCase Extension

NUnit style TestCase and TestCaseSource extensions for MSTest and Visual Studio 2012/2013





Introduction:

There's been lots of talk about MSTest and Visual Studio not supporting NUnit style TestCases (sometimes reffered to as row tests.

There are lots of discussions online (e.g., on StackOverflow here and here), and the Microsoft Visual Studio Team has written a blog on How to extend Visual Studio with new Unit Test Types, and they do support so-called Data-Driven Tests, but the syntax of TestCases is so succinct:

            [TestMethod]
            [TestCase(3, 0, 1)]
            [TestCase(4, 1, 2)]
            [TestCase(7, 3, 4)]
            [TestCase(9, 5, 4)]
            public void TestSum(int expected, int a, int b)
            {
                Assert.AreEqual(expected, a + b);
            }
And I wasn't able to find anyone that has packaged up an extension that has those features.

So, I've waded through all the documentation, examples, and blogs; and created a C# library for Visual Studio 2012/2013 (complete with an installer) that supports the TestCase and TestCaseSource attributes familiar to NUnit users. In order to hook-into Visual Studio, your test-class must have the included TestCaseClass attribute (instead of the usual TestClass), and your test-methods must be annotated with the TestMethod attribute.

Multiple TestCases (or TestCaseSources) on a TestMethod will be run, and if one (or more) of your TestCases fail (note the first two test-cases above are intentionally incorrect), you get a meaningful error message:

          TestCase Failure(s)
            1) TestCaseTesting.SimpleTestCases.TestSum(3, 0, 1) :
               Assert.AreEqual failed. Expected:<3>. Actual:<1>. 
    	         at Microsoft.VisualStudio.TestTools.UnitTesting.Assert.HandleFail(String assertionName, String message, Object[] parameters)
    	         at Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual[T](T expected, T actual, String message, Object[] parameters)
    	         at Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual[T](T expected, T actual)
    	         at TestCaseTesting.SimpleTestCases.TestSum(Int32 expected, Int32 a, Int32 b) in ...\SimpleTestCases.cs:line 19

            2) TestCaseTesting.SimpleTestCases.TestSum(4, 1, 2) :
    	        Assert.AreEqual failed. Expected:<4>. Actual:<3>. 
    	         at Microsoft.VisualStudio.TestTools.UnitTesting.Assert.HandleFail(String assertionName, String message, Object[] parameters)
    	         at Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual[T](T expected, T actual, String message, Object[] parameters)
    	         at Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual[T](T expected, T actual)
    	         at TestCaseTesting.SimpleTestCases.TestSum(Int32 expected, Int32 a, Int32 b) in ...\SimpleTestCases.cs:line 19


Download/Install:

Download the installer, InstallTestCaseExtension.exe. Then add a reference to C:\Program Files\Microsoft Visual Studio 11.0\Common7\IDE\PrivateAssemblies\TestCaseExtension.dll to your test-project. For 64-bit versions use Program Files (x86) instead.

Note: Visual Studio will usually insert relative reference paths into your Project files (e.g., ..\..\Program Files\Microsoft Visual Studio 11.0\...), so if you plan on building your test-project in different directories (e.g., storing it in source-control) then you can edit the csproj file by hand and convert the reference to an absolute path.


Examples:

Here's a few examples that show the features of the extensions. Feel free to look them over, or try them out once you install the TestCaseExtension.dll and add a reference to your project. Enjoy!

SimpleTestCases.csPlainHTML
SimpleTestCaseSources.csPlainHTML
TestClassBasics.csPlainHTML
MoreTestCaseExamples.csPlainHTML

After running these, you should see output in Visual Studio Test Explorer similar to:


Source Code:

The source is available under the GNU General Public License. You can dowload a Zip File, which includes the CS-Project, NSIS Installer Script, and a Test/Example Project.

Or, you can browse them here online.


Contact:
If you find the Extension useful, or have any questions/comments feel free to email me (Bryan Chadwick):
bryan (at) bryanchadwick.com