NUnit defines itself as a unit-testing framework for all .Net languages, which was originally ported from JUnit. Although it is designed for unit-testing, it has the potential— and is often used—to implement and run tests at different levels like integration testing and end-to-end testing with the support of libraries.
In this tutorial, we will explore how to integrate the test results generated by an NUnit automated test run within TestRail using the TestRail CLI. This will allow you to centralize your automated test results and take advantage of all the analytical and reporting capabilities TestRail provides.
Overview
Throughout this tutorial, we will be using a sample project to guide you through the process of setting up an NUnit automated tests project that is compatible with the TestRail CLI and uploading the generated test results.
After reading through this tutorial, you will be able to:
- Execute tests from an NUnit project
- Install the TestRail CLI
- Configure your TestRail instance
- Run the CLI
- See your test cases and test results in TestRail
Prerequisites
To be able to install and run a simple NUnit tests project all you need is the .NET SDK.
You will also need Python to install and run the TestRail CLI that will be used to import the test results to TestRail.
Prerequisite | Description |
---|---|
.NET SDK |
Download the appropriate .NET framework and follow the install wizard. To make sure the install was successful, try executing |
Python 3.10.4 |
Download the version for your operating system and follow the install wizard instructions. To make sure the install was successful, try executing the commands |
Installing the sample project
Let’s start by fetching the sample project code and installing the required dependencies.
- Clone or download the sample project
- Open your command prompt on the project root folder and execute the command below
$ dotnet restore
$ dotnet build
Requirement | Description |
---|---|
NUnit | Testing framework for .Net languages. Defines the rules to implement test cases and provides assertion mechanisms. |
NUnit.ConsoleRunner | Tool to execute NUnit tests. |
Exploring the sample project
Use your favorite IDE to open the sample project and start digging into the test files. We’ve kept the automated tests code simple so that the focus of this tutorial remains on how to import the execution results. These tests consist of simply opening a page on the browser using selenium and performing a few actions and validations, such as the one on the example below.
using NUnit.Framework;
namespace SimpleTestProject
{
[TestFixture]
[Category("SimpleMaths")]
public class SumTests
{
[Test]
public void SumTwoNumbers()
{
Assert.AreEqual(2, 1+1);
}
[Test]
public void SumTwoNumbersWithDecimals()
{
Assert.AreEqual(2.5, 1 + 1.49);
}
[Category("SimpleMaths")]
class SumMultipleNumbersTests
{
[Test]
public void SumThreeNumbers()
{
Assert.AreEqual(3, 1 + 1 + 1);
}
}
}
}
Executing the sample project
On the same command prompt, execute the command below to run the NUnit tests in the project and save the results in JUnit XML format.
$ .\NuGet\nunit.consolerunner\3.15.0\tools\nunit3-console `
> SimpleTestProject\bin\Debug\net6.0\SimpleTestProject.dll `
> --where "cat == SumAndSubtract" `
> --result "reports/junit-results.xml;transform=SimpleTestProject/nunit3-junit.xslt"
Notice the transform=SimpleTestProject/nunit3-junit.xslt
on the result command line option. NUnit has its own native report format, but it also allows you to convert it to a JUnit style report by using an XSLT file to transform the XML. To meet this purpose, the NUnit team hosts nunit3-junil.xslt transform file on their GitHub repository, available to everyone.
If the nunit3-console call was correctly executed, you should be able to see your test results on the reports folder. There should be a file called junit-report.xml, as per our NUnit command options, with results in JUnit XML format. This file will be parsed by the TestRail CLI to create the test run and upload your test results to TestRail on the next step.
<testsuites tests="7" failures="2" disabled="0" time="0.758739">
<properties>
<property name="_PID" value="16964"></property>
<property name="_APPDOMAIN" value="nunit-agent"></property>
</properties>
<testsuite tests="3" time="0.079189" errors="0" failures="1" skipped="0" timestamp="2022-06-13T14:22:57.4201483Z" name="SimpleTestProject.MultiplicationTests.">
<testcase name="MultiplyDataset(2,1,2)" assertions="1" time="0.007227" status="Passed" classname="SimpleTestProject.MultiplicationTests" />
<testcase name="MultiplyDataset(2,2,4)" assertions="1" time="0.000076" status="Passed" classname="SimpleTestProject.MultiplicationTests" />
<testcase name="MultiplyDataset(2,3,5)" assertions="1" time="0.070076" status="Failed" classname="SimpleTestProject.MultiplicationTests">
<failure message=" Expected: 5
 But was: 6
"> at SimpleTestProject.MultiplicationTests.MultiplyDataset(Int32 a, Int32 b, Int32 expected) in C:\Github\automation-frameworks-integration\samples\dotnet\nunit\SimpleTestProject\Tests\MultiplicationTests.cs:line 19
</failure>
</testcase>
</testsuite>
<testsuite tests="1" time="0.000395" errors="0" failures="0" skipped="0" timestamp="2022-06-13T14:22:57.5001385Z" name="SimpleTestProject.SubtractionTests.">
<testcase name="SubtractTwoNumbers" assertions="1" time="0.000312" status="Passed" classname="SimpleTestProject.SubtractionTests" />
</testsuite>
<testsuite tests="2" time="0.001553" errors="0" failures="1" skipped="0" timestamp="2022-06-13T14:22:57.5005518Z" name="SimpleTestProject.SumTests.">
<testcase name="SumTwoNumbers" assertions="1" time="0.000061" status="Passed" classname="SimpleTestProject.SumTests" />
<testcase name="SumTwoNumbersWithDecimals" assertions="1" time="0.001433" status="Failed" classname="SimpleTestProject.SumTests">
<failure message=" Expected: 2.5d
 But was: 2.4900000000000002d
"> at SimpleTestProject.SumTests.SumTwoNumbersWithDecimals() in C:\Github\automation-frameworks-integration\samples\dotnet\nunit\SimpleTestProject\Tests\SumTests.cs:line 18
</failure>
</testcase>
</testsuite>
<testsuite tests="1" time="0.000144" errors="0" failures="0" skipped="0" timestamp="2022-06-13T14:22:57.5021166Z" name="SimpleTestProject.SumTests+SumMultipleNumbersTests.">
<testcase name="SumThreeNumbers" assertions="1" time="0.000082" status="Passed" classname="SimpleTestProject.SumTests+SumMultipleNumbersTests" />
</testsuite>
</testsuites>
Importing results to TestRail
After the tests have been executed and the junit-report.xml file is generated, you can easily import your test results (and test cases!) to TestRail by using the TestRail CLI. This will bring visibility to your automated test runs and will enable you to look at the big picture of how you’re actually testing your app all within TestRail.
Installing the TestRail CLI
Given you already have Python installed on your machine, installing the TestRail CLI is as simple as executing the following command on your command line.
$ pip install trcli
Configuring TestRail
Secondly, you need to configure your TestRail instance according to the instructions below.
- Enable the TestRail API by going to Admin > Site Settings, click on the API tab, and checking the Enable API option.
- Create a Custom Field in order to map your automated test cases code to the actual TestRail cases. You can do so by going to Admin > Customizations and clicking Add Field. After you’ve reached the field creation screen, there are two requirements for this custom field:
- The System Name must be automation_id
- The Type must be Text
Sending results to TestRail
After you’re done installing the TestRail CLI and finished configuring your TestRail instance, you can upload your test results by simply using a one-liner such as the one below.
$ trcli -y \
> -h https://INSERT-INSTANCE-NAME.testrail.io \
> --project "My Project" \
> --username INSERT-EMAIL \
> --password INSERT-PASSWORD \
> parse_junit \
> --title "NUnit Automated Test Run" \
> -f junit-report.xml
Note that the file name after the -f option should match the path to your report file in case you change its default location. All others options should be according to your TestRail instance and project. You can check other command line options by checking the TestRail CLI README.md file on the project repository, the TRCLI documentation article, or the embedded CLI help through the commands below.
$ trcli --help
$ trcli parse_junit --help
Visualizing the results on TestRail
Now, if you go to the Test Cases page in your TestRail project, you’ll notice that the TestRail CLI automatically created the test cases that were on your test results report. Notice that it added a unique Automation ID by combining the classname
and name
attributes on each test on the JUnit report. This Automation ID is used to map the tests on your automation code base to the test cases on TestRail. This means that each time you run the TestRail CLI, it first attempts to match an existing test case on TestRail and only creates a new one in case there is no test case with that Automation ID.
If you change the name of your tests, the name of the file or its location, the Automation ID for those tests will change and they won’t be mapped to the existing test cases on TestRail.
On the Test Runs & Results page, we can see that a test run with the name NUnit Automated Test Run was created. By opening it we can dig further into the details of each automated test result and perform a high level analysis of why any test is failing since the error message provided by the test automation framework is also registered on the test result, as you can see on the image below.
What next?
Now that you have centralized your test results on TestRail, not only can you check the results of your automated test runs, along with the error messages for failed tests, but you can also aggregate both your manual and automated testing efforts on reports that show you the full test coverage surrounding your app and even track test automation progress. You can also report a bug directly from the automated test result to an issue tracker of your preference as you would do for your manual test results!
You can look into the TestRail’s Reports and Test Metrics video to learn about how you can leverage TestRail’s reporting capabilities.