Visual Studio 2012 comes with an extensible Unit Test Framework. Many of your favorite Unit Test frameworks (xUnit.Net, nUnit..) already have built adapters to Visual Studio. Peter Provost maintains a list of adapters here. If you have another unit test framework you like, you can build an adapter super easy. In this article, I will walk you through the steps required to build a Visual Studio 2012 Unit Test Adapter.
For the purposes of this discussion, I am going to write a Unit Test Adapter for XML files. The XML file will contain tests in the following format.
After we install the new XML Test Adapter, we will see the following status in Test Explorer.
Step 1: Create a Discoverer
A Discoverer implements the ITestDiscoverer interface & has a single Method to discover tests.
Here is the code for our XMLTestDiscoverer.
I load the XML and read the name attribute on each Test node.
I create a TestCase and send it to Discovery Sink (which is a component of the Unit Test Framework).
Step 2: Create an Executor
An Executor implements the ITestExecutor interface which has 3 methods.
- Cancel is called to cancel the execution of tests.
- RunTests has two overloads. The first one takes a list of test cases and the second one a list of source file names.
For the XML Test Executor, I need to read the outcome attribute of the Test nodes. I see that the code is similar to what I have in TestDiscoverer. I refactor the code into a GetTests Method as shown below. I pass in null for the discoverySink when calling this method from the Executor.
The full code for the Test Executor is shown below. In the RunTests method, I create a TestResult and pass it to the FrameworkHandle (another component of Unit Test Framework)
Step 3: Create a TestContainerDiscoverer
Test Container Discoverer provides Visual Studio Test Explorer Integration. It has a list of Test Containers and the Unique identifier for the Executor. It also has an event handler for cases where Test Containers are updated.
The constructor for XMLTestContainerDiscoverer registers various event handlers for Visual Studio Solution and Project events.
Here is my code which identifies TestContainers. Visual Studio APIs are used to identify the elements in the loaded projects and pick out the xml files.
You can see similar code present in the Chutzpah Adapter. I have taken the Chutzpah code and modified it to built by Container Discoverer. nUnit & xUnit.Net have a different implementations of Test Container Discoverers.
Step 4: Build an Installer
You would have seen in the earlier code snippets that XMLTestContainerDiscoverer is implemented as MEF component. I will use a VSIX project to install all the components that we built earlier. Here is how the VSIX Manifest looks like.
The full source code for my XML Adapter is attached.
In this article, I showed you how to build a unit test adapter for XML files and integrate it with Visual Studio. After you install the VSIX from this project, you can open any solution with XML files in the format defined earlier and you will be able to see the tests listed in Test Explorer. You can run them from the Test explorer and it will display the outcomes defined in the XML file.
I have referred to 2 articles to build this one. Special thanks to Matthew Manela & Bhuvaneshwari K.
For a detailed list of topics related to Unit Testing in Visual Studio 2012, visit http://blogs.msdn.com/b/mathew_aniyan/archive/2012/05/17/content-index-for-unit-test.aspx
Program Manager – Visual Studio ALM.