Using XUnit with Team Build 2010
My team is making the switch from MSTest to XUnit for our unit testing framework. As a result of this change, I needed to enable our TFS Build to run XUnit and publish the results. Searching around I found a couple old posts like this one; however, all of them left me with a decent amount of work to do. I wanted to create something that I could easily distribute to the rest of my teams and it would be simple to integrate.
The first step was to create a workflow task to execute XUnit against my test DLL. My goal for the task was to work as close to the MSTest task as possible. That meant passing in a file spec such as **\*tests*.dll. To make it easy on the consumer, we’re also going to not assume a path, but have that as an argument as well.
Now that we’ve created the arguments that we need, we can start dropping in the activities. The first activity is to use the TFS Build Find Matching Files task. This will use our file spec to find all of the unit test dlls that are built. Next we confirm that we have items in our file collection or issuing a warning (not shown) that we couldn’t find any items. There is no TFS Build 2010 XUnit activity from the XUnit project. As such we’re going to be invoking the command line runner. This runner can only run one dll at a time. This means that the next step must be to loop over the test dlls with the ForEach activity.
Now that we have our individual test dlls processing, the next step is to invoke XUnit against each of those files. We do this with an InvokeProcess activity. The file name is one of the arguments that’s the path to the XUnit console runner. The arguments that are passed are “<path> /silent /nunit results.xml”
The next step is to publish the results to TFS. For this I created a coded build activity that mimicked the functionality of the NUnit publish task from the community build extensions.
The catch with the way the publish works is that the result file must be different for each test dll run. For this we add a counter to append to the result xml as we go through the files. The full activity can be downloaded and built from GitHub. Now that we have a xUnit build activity, it’s just a matter of dropping it in a full build workflow. This is the easy part, as we’re just completely removing the MSTest portions and replacing it with the XUnit activity we just finished writing.
And voila, we have XUnit tests running in a TFS 2010 build process and publishing the results. Hopefully this is helpful to help you get XUnit integrated into your process.