Microsoft Fakes and TeamCity (and XUnit)
This post is a note to my future self on how to configure a TeamCity build to run tests that use Microsoft Fakes. If you haven’t ever come across Microsoft Fakes then take a look at this post - Testing the untestable with Microsoft Fakes (http://msmvps.com/blogs/bsonnino/archive/2013/08/11/testing-the-untestable-with-microsoft-fakes.aspx) for a good introduction.
Setting up the Build Agent
You will need to install Visual Studio 2012 and make sure that Update 3 is applied, no additional installation is required as Microsoft Fakes comes bundled.
If your tests are written using XUnit, which in my case they were then the next step is very important, (if you are using MSTest then you can skip this step and go to Configuring the Build Step).
You will then need to logon to the build agent machine with the user that the build agent runs as (i.e. not you!). Once logged on, launch Visual Studio and install the XUnit test runner. The reason for this is that Visual Studio extensions are installed per user. This caught me out!
Next up, you will need to download the TeamCity vstest logger from GitHub (https://github.com/nickers/vstest.teamcity.logger), build this and follow the instructions from the project page as to where the built DLL needs to go.
At this point, everything on the build server is setup and it is time to configure the TeamCity project.
Configuring the Build Step
Go into the setup of your project and add a new Build step.
Runner type: Command line
Step name:
Execute step: Only if all previous steps were successful
Working directory: Set to the bin\Release (or bin\Debug) folder for your solution, depending on which one you are building/testing, this will most likely be Release
Run: Custom Script
Custom Script: C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe myinsertingproject.tests.dll /logger:TeamCityLogger /UseVsixExtensions:true
Update 21/11/2013 -
Turns out you do need to set the working directory, also updated the custom script to include the full path to the vstest.console.exe (thanks to @kevindrouvin)
Summary and Resources
That should be all you need to do to get the tests running and being reported back into TeamCity. Depending on how your environment path is setup you might need to give the directory to the vstest.console.exe in the Custom Script setup.
Here are a few different resources that I used:
- VSTest.Console.exe Command-Line Options - http://msdn.microsoft.com/en-us/library/vstudio/jj155796.aspx
- VSTest logger for TeamCity - https://github.com/nickers/vstest.teamcity.logger
- Unit testing – Visual Studio 2012 Fakes in Team City - http://blog.degree.no/2012/09/unit-testing-visual-studio-2012-fakes-in-team-city/
🍪 I use Disqus for comments
Because Disqus requires cookies this site doesn't automatically load comments.
I don't mind about cookies - Show me the comments from now on (and set a cookie to remember my preference)