Extend MSTest : TestCaseSource aka runtime inputs for test cases


Extending MSTest


Read this great post first by William Kempf. And It's true!

 http://www.digitaltapestry.net/blog/extending-mstest

 Next if you're willing to bear with all that, these are the only 2 posts from circa-2009 on the intertubes that give you any hope

MSDN Blogs - Bruce Taimana Part 1. Part 2
 

Writing an extension in 2013

The details have changed since 2009 and combined with the paucity of information, it was three days before I had something to show.

So my target extension was going to bring in NUnit's TestCaseSource functionality, whereby you could supply the parameters for a parameterized test via a method (at runtime. Compile-time is already supported via XML and DBs.. though cumbersome.)
[TestMethod] 
[TestCaseSource("DivideCases")]
public void MultipleParams(int n, int d, int q)
{
   Assert.AreEqual( q, n / d );
}

public static object[] DivideCases()
{
   return new[] 
   {
       new object[] {12, 3, 4},
       new object[] {12, 2, 6},
       new object[] {12, 4, 3}
   };
}

Taking it for a spin 

  • Get the binaries from the google code repository
  • Copy the extension dll (and pdb) to the Visual studio folder - C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\PrivateAssemblies 
  • Next you need to set some registry keys - Install.reg and Uninstall.reg files provided in the binaries folder. Merge the install one. 
  • Phew. Deployment on build machines is gonna be a pain. Start up Visual studio. Create a test project as usual. Reference the extension dll to gain access to the attributes 
  • The changes: 
    • Mark your test class with the new ParamTestClass attribute. 
    • The test method attribute stays the same. Specify inputs that you wish to pass to your test methods.
    • Specify the name of the method that will provide the inputs via the TestCaseSource attribute. It has to be a public instance or static method which returns an IEnumerable, object[] or IEnumerable. Look at the Sample tests. TestCases can take single or multiple parameters ; they can also be named e.g. Test Login  'for admin' 'for normal'




Lessons from writing the extension

  • The libraries you need to reference have been renamed since the 2009 post
  • You can't add new tests at runtime.. (as mentioned in the digitaltapestry post) The best you can do is 
    • control invocation of the test method (e.g. call it in a loop with diff params)
    • add pre/post processing (e.g. collate a single test result)
  • I couldn't get the UI extension part to work - it seems like those methods were only for use by the Test Results window (which was a .trx viewer). However both of them seem to have been deprecated. No idea what's the new way to better format the test results. I finally gave up and settled for the console output support. Couldn't figure how to set a fixed font for the VS 2012 'Test Output' for tabular output. So the UI Subfolder in the source is useless
  • Full Source - http://code.google.com/p/gishu-util/source/browse/#git%2FMSTestExtensions

1 comment:

  1. Any chance you've gotten this to work in resharper?

    ReplyDelete