Writing a Visual Studio 2012 Unit Test Adapter

Mathew Aniyan MSFT

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.

image

After we install the new XML Test Adapter, we will see the following status in Test Explorer.

image

 

Step 1: Create a Discoverer

image

A Discoverer implements the ITestDiscoverer interface & has a single Method to discover tests.

Here is the code for our XMLTestDiscoverer.

image

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

image

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.

image

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)

image

 

Step 3: Create a TestContainerDiscoverer

image

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.

image

 

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.

 

image

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.

image

 

The full source code for my XML Adapter is attached.

Summary

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.

 

Resources

I have referred to 2 articles to build this one. Special thanks to Matthew Manela & Bhuvaneshwari K.

1. http://blogs.msdn.com/b/bhuvaneshwari/archive/2012/03/13/authoring-a-new-visual-studio-test-adapter.aspx

2. http://matthewmanela.com/blog/anatomy-of-the-chutzpah-test-adapter-for-vs-2012-rc/

 

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

 

Mathew Aniyan

Program Manager – Visual Studio ALM.

XmlTestAdapter.zip

1 comment

Discussion is closed. Login to edit/delete existing comments.

  • Ramprakash Natesan 0

    Thanks for the detailed post.
    I have implemented the same and I am able to get my xml based tests discovered under Unit Test Explorer. But I am not able to associate those tests with TFS/MTM manual test cases. When I right click the test case, the “Associate to Test Case” option is disabled.
    Is it possible to get that option enabled to associate with a manual TFS test case ?

Feedback usabilla icon