Learn to use Visual Studio, Visual Studio Online, Application Insights and Team Foundation Server to decrease rework, increase transparency into your application and increase the rate at which you can ship high quality software throughout the application lifecycle
More videos »
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.
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).
An Executor implements the ITestExecutor interface which has 3 methods.
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)
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.
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.
Glad to see this kind of support.
Too bad the test runner is junk. The one in VS2010 was great.
Hi, is it only possible to run test with a custom adapter locally in VS or is there also a way to run tests using the same adapter on TFS centrally?
Instructions for using 3rd party test adapters is available in the blog - blogs.msdn.com/.../build-on-the-team-foundation-service.aspx
Same procedure can be used for any test adapter you build.
hi Mathew, what would be the license terms for this project source?Does it come under Microsoft public license?