Patrick Desjardins Blog
Patrick Desjardins picture from a conference

VSTS to run only your fast unit test

Posted on: 2016-04-18

If you are using Visual Studio Team Services (VSTS) for your continuous integration (CI) you may want to run only specific tests. In the past, I had a project that were dividing tests that needed database and not. One was integration tests, the other one unit tests. In the CI you want only to run unit tests. The reason is that they doesn't not require any infrastructure like databases, cached, third-party services or external API. That said, if you cannot specify an assembly name (or pattern) how can you tell the CI to run only the unit test?

VSTS' build system can run the Visual Studio Test step with test filter criteria. This is where you can define which TestCategory you want to execute.

This is not really obvious from VSTS website what you can put there. The "i" icon gives you some information but this is quite limited. A better user interface (UI) would have been to have a quick cheat sheet available on the "i". Nevertheless, you can type "TestCategory=YOUR_CATEGORY" where YOUR_CATEGORY is the one defined in the test.

At this point, you are almost done. I didn't had any test category defined on any test since tests were divided by assemblies before. Now, they are all in the same. The quickest way to add test category to test was to go on each file, open these related to unit test and do a search and replace on open files. The searching query is for [TestMethod]. The replacement is [TestMethod][TestCategory("UnitTest")]. From there, you just need to commit and push you change and see the CI running the unit tests.