Integrating NCover With TeamCity

Configure NCover Code Central to work with your TeamCity based build process.

Code Coverage With TeamCity

3 steps to configure NCover to work with your TeamCity server

NCover Code Central allows you to monitor any number of build servers and test machines and collect coverage data. One build server many customers like to integrate with is TeamCity. Code Central can collect coverage directly if it is installed locally on the TeamCity build server or it can connect from the TeamCity build server to any machine with Collector installed and collect coverage remotely.

Follow these three steps to integrate NCover into your automated TeamCity build process

Step 1: Create A New NCover Project

Your NCover project is where your code coverage results will be sent and displayed in the NCover GUI. Whether you are planning to collect coverage locally with Code Central, or remotely with Collector, you need to create your new project within Code Central and give your project a name.

On a build server, your project will typically be set to collect coverage on NUnit or MSTest, although any .NET testing tool can be used and the process for setting up your project is similar.

Once you have created your NCover project, select "Edit Match Rules" and add a new match rule for the appropriate test runner process (more on this later).

If you are using Code Central to collect coverage locally, you can also use Auto-Configure if you are not sure what process is loading your unit tests. If you want more information, we have step-by-step instructions on covering your build server in our documentation. Or, you can watch a brief video tutorial on creating a new projet that outlines the steps for creating a new project and setting process match rules.

Once you have created your new project and verified your match rules, be sure to save your preferences. You you are ready to build your NCover Run command.

Step 2: Integrate Your Testing Process With TeamCity Using The NCover Run Command

Since it's likely that you have more than one project running unit tests on your build server, you can use NCover Run to profile a specific test run. Having a Code Central project set up to simply monitor the NUnit agent, for example, whenever it runs might gather a confusing array of data on unrelated projects.

The NCover Run command allows NCover to:

  • collect data from the covered process,
  • send the coverage results to the NCover project you specify and
  • summarize the coverage results and generate links to the coverage summary html report.

The NCover Run command also provides a variety of options that allow you to:

  • specify build IDs,
  • return error codes,
  • output a coverage summary to the console,
  • register the NCover profiler DLLs and
  • define the profiled application’s working directory.

The syntax of the Run command will vary slightly depending on your particular test runner, but the approach is similar. Below, we have step-by-step instructions for two of the most common test runners, NUnit and MSTest.

For NUnit:

First, create a project in Code Central where the coverage results will be sent and displayed. On the "Processes" tab of the new project, click "Edit Match Rules." Add a match rule of Match type="Regex", Process matching="nunit-agent." Save the project (which will be automatically synced to all Collectors), then start building your NCover Run command.

The first part of the NCover Run command is the (required) NCover project, the second half is the profiled application (NUnit). Use the same syntax in the second part of the Run command that you would use to run NUnit from the command line. So, a very basic NCover Run command will look something like this:

ncover run --project="MyNUnitProject" -- "nunit-console-x86.exe" "UnitTest.dll"

Try NCover Run from the command line first, which will make it much easier to troubleshoot than running it from the NAnt build script. When NCover runs successfully, coverage will be collected on NUnit and displayed in your project in Code Central. Once NCover Run is working as expected from the command line, add it to your build script as an exec and test again.

Additional information on covering NUnit using NCover Run can be found in our documentation.

For MSTest:

First, create an NCover project. On the "Processes" tab, name your project, then click "Edit Match Rules." Add a match rule of Match type="Regex", Process matching="qtagent." Save the project (which will be automatically synced to all Collectors), then start building your NCover Run command.

The first part of the NCover Run command is the (required) NCover project and (optional) build ID, the second half is the covered application. Use the same syntax in the second part of the Run command that you would use to run MSTest from the command line. So, a very basic NCover Run command will look something like this:

ncover run --project="MyMSTestProject" -- "mstest.exe" "/testcontainer: UnitTest.dll"

Try NCover Run from the command line, which will make it easier to troubleshoot than running it from a build script. When the Run command completes successfully, coverage will be collected on MSTest and displayed for your project in the NCover GUI. Once NCover Run is working as expected from the command line, add it to your build script as an exec and test again.

Additional information on covering MSTest using NCover Run can be found in our documentation.

Step 3: Report On Your Coverage Results Within TeamCity

The NCover HTML Coverage Summary Report provides an excellent recap of coverage metrics for a specific execution and is a common method for integrating your coverage results into TeamCity. In addition to a basic summary report, NCover 5 also includes the option to include method and class detail in the report. The report can be generated from the Command Line and integrated as part of your build scripts.

Additional information on the NCover Report and its available options can be found in our documentation.

Detailed instructions on including third-party reports in the build results can be found in TeamCity's Documentation and applied to the NCover Report.

*TeamCity is a third-party build server and is not affiliated with NCover. TeamCity, and applicable trademarks and copyrights, are the sole property of JetBrains.

.NET Code Coverage Solutions For Improving Code Quality