Introduction
DNX will soon be released and there’ll be a growing number of developers wanting to write unit tests to improve the quality of developments.
Ideally you’d like to run these unit tests in a continuous integration process in order to make sure you didn’t introduce any regression/bug or degraded performances.
In .NET world you have many tools to write unit tests:
MSTest/VSTest
NUnit
xUnit
…
(a non-exhaustive list https://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C.23 )
In our case we chose to use xUnit because it is widely used and supported by the community, and it’s one of the few to support DNX at the moment.
Another thing, you must have noticed from my previous blog posts, we’ve been using a lot the tools provided by VSTS (VSO), in particular build2015 and release management.
And as a reminder DNX = “new framework” which means the tooling has to be adapted to it and compiled for it in order to support it properly.
Local tests and tests projects
After following this tutorial, you’ll be able to write and run tests in a dnx library project (which produces a nuget at the end) and to execute these tests locally via console (dnx test) or via the test explorer.
https://asp.net-hacker.rocks/2015/12/15/unit-testing-dnx-libraries-with-xunit.html
https://xunit.github.io/docs/getting-started-dnx.html
Note you can also use yeoman to scaffold the tests project typing yo aspnet and selecting the choice corresponding to tests.
Now that you can run tests locally the question is: how can I make VSO/VSTS run the tests and get the results with my build?
Executing tests with agent
The first step is to make the agent run the tests, it is as easy as running a powershell script.
dnx -p “.\src\BizDesk365ContentService.Tests" test -xml TestResults.xml
First parameter is the relative path to the tests project. The second one tells the xunit’s dnx runner to output the results to a xml file that will be needed later.
In my exemple I added some lines to ensure that dnvm, my runtime and dnx are here, but depending on your configuration it might not be required.
https://gist.github.com/baywet/4daabc875cab427ef1bc
Getting the tests results
Now you need to tell VSTS to get and process the unit tests results, to do so just add a publish test results step.
In type select xunit and give it the location of the test results file.
Conclusion
Tests results now show up with our build in VSTS and we can see that with a little bit of scripting and configuration it is easy to build unit tests for DNX projects
Have fun testing!