Skip to content
Nikolay Pianikov edited this page Aug 29, 2017 · 22 revisions

Test .net code with TeamCity

TeamCity provides a lot of capabilities when it comes to running tests. One of its of benefits is the ability to report test results in a usable form after the tests have finished. Moreover, TeamCity can report results on-the-fly, while testing is in progress. Since the test time can be significant, it is preferable to have fast feedback.

How can it be applied to testing in .Net?

Microsoft has presented its vision on what testing should looks like in .net. This approach works fine for any target framework as well as for multiple frameworks at the same time. This means it'll work for any test engine which has a test adapter:

In short, a test project needs to have three additional package references at least:

  • Microsoft.NET.Test.Sdk, which contains the MSbuild targets and properties for test projects and marks a project as a test project.
  • Test Framework which contains a set of attributes or something highlighting tests
  • Test Adapter which contains a test adapter which allows running tests using the selected test engine inside the selected test platform.

This should be enough to run tests locally or in on any CI, but it is better if CI provides immediate feedback and test results in a usable form as we mentioned before.

To support a TeamCity integration, JetBrains in its turn has provided the TeamCity.VSTest.TestAdapter NuGet package, containing a special logger which integrates with the test platform and sends text messages to a TeamCity server.

To turn on the integration, add a package reference to TeamCity VSTest Adapter. Note that this package does not impact tests run locally, but only those run under TeamCity.

Here is an example of a project:

Visual Studio NuGet Dependencies

The project file for a MS test project can be as follows:

<Project Sdk="Microsoft.NET.Sdk">
...
  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
    <PackageReference Include="MSTest.TestAdapter" Version="..." />
    <PackageReference Include="MSTest.TestFramework" Version="..." />
    <PackageReference Include="TeamCity.VSTest.TestAdapter" Version="..." />    
  </ItemGroup>
...
</Project>

To configure a build project in TeamCity, you can use the TeamCity plugin for .NET Core projects, similarly to how it is done in the following example, where a test project contains one class UnitTests:

namespace MS.Tests
{
    using System;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    [TestClass]
    public class UnitTests
    {
        [TestMethod, Ignore]
        public void TestIgnored()
        {
        }

        [TestMethod]
        public void TestPassed()
        {
            Console.WriteLine("some text");
        }
    }
}

A TeamCity page for the tests step can have the following form:

.

After a build is run, TeamCity shows the test results:

This .net project has two target platforms, net45 and netcoreapp1.0, that is why each test is run for both framework and you can see several results per test.

Read more about TeamCity plugin for .NET Core projects here.

It is also possible to use other TeamCity build runners like Command Line Runner.

Important notes

  • Note that this package does not impact tests run locally, but only those run under TeamCity. The TeamCity.VSTest.TestAdapter package looks for the environment variable TEAMCITY_VERSION to know whether it is running in a TeamCity environment and that it needs to send the TeamCity service messages to the TeamCity server.

Links

Clone this wiki locally