NUnit Integration

Applies to Zephyr Scale Cloud, last modified on July 05, 2021.

NUnit is a unit-testing framework for all .NET languages. For more info on NUnit, see here.

The NUnit integation relies on the JUnit integration. We will explain how to generate JUnit XML results file from NUnit so you can use the JUnit integration to push your results.

Writing NUnit tests

Here is an example. Consider a class named UnitTest.cs:

using NUnit.Framework;

namespace UnitTest
{
    public class Tests
    {
        [SetUp]
        public void Setup()
        {
        }

        [Test]
        public void Substract()
        {
            double res = 10 - 10;
            Assert.AreEqual(res, 0);
        }

        [TestCase(2, 2, 4)]
        [TestCase(0, 5, 5)]
        public void Add_NET_T1743(double a, double b, double c)
        {
            double res = a + b;
            Assert.AreEqual(res, c);
        }

        [Test]
        public void NET_T1744_Divide()
        {
            double res = 10 / 5;
            Assert.AreEqual(res, 2);
        }

    }
}

Note that the two method names include respectively NET_T1743 and NET_T1744. This particular identifiers that can be either prefix or suffix will map the result of each of those test methods to the corresponding test cases in Zephyr Scale if they exist, respectively test cases with key NET_T1743 and NET_T1744.

Also, note that the method Substract does not include a test case key. Zephyr Scale will try to find an existing test case that matches the package, class and method name, in this case UnitTest.Tests.Substract, and map the result of the test method to the test case. If these test cases don’t exist, an error will be returned when the results are uploaded to Zephyr Scale. If you want to create the test case when it does not exist yet, please use the query parameter autoCreateTestCases as described below.

If any test case, prefixed with the test case key or not, is not found, an error will be returned when the results are uploaded to Zephyr Scale. In this case, if you intend to automatically create test cases that don’t exist previously, the query parameter autoCreateTestCases=true can be set when uploading test results to Zephyr Scale.

To summarize, the rules for matching a test method to a test case are:

  • Try to match by parsing a test case key from the test method name.

  • If no test case is matched, then try to match a test case by using the method full qualified name.

  • If no test case is matched, then:

    • If the parameter autoCreateTestCases is true, create the test case and create a new test execution for it.

    • If the parameter autoCreateTestCases is false or not present, return an error - no test cases have been matched.

Uploading results to Zephyr Scale

Nunit report file does not support JUnitXml format, however, there is a way to convert  NUnit3 results to JUnit-style results.

In order to instruct Nunit to excute the tests and generate the JUnit XML results file, It is required is to execute nunit-console like this:

nunit3-console.exe YourTestAssembly.dll --result=junit-results.xml;transform=nunit3-junit.xslt

The command line above will execute the tests and will generate the JUnit XML results file junit-results.xml. More info here.

Once the NUnit tests have executed, the results can be uploaded to Zephyr Scale. There are two options for uploading test results:

  • Using a single JUnit XML results file.

  • Using a zip file containing multiple JUnit XML results file.

Both options work the same way and can be uploaded using the same API endpoint: https://support.smartbear.com/zephyr-scale-cloud/api-docs/#operation/createJUnitExecutions.

Below, an example using Curl of how to use the API endpoint for uploading one single file:


curl -H "Authorization: Bearer ${TOKEN}" -F "file=@junit-results.xml;type=application/xml" https://api.zephyrscale.smartbear.com/v2/automations/executions/junit?projectKey="JQA"&autoCreateTestCases=true

…and for uploading a zip file containing multiple XML files:


curl -H "Authorization: Bearer ${TOKEN}" -F "file=@TestSuites.zip;type=application/x-zip-compressed" https://api.zephyrscale.smartbear.com/v2/automations/executions/junit?projectKey="JQA"&autoCreateTestCases=true

Note the query parameters on the URL. The projectKey specifies what project will be used to create the test cycle. The autoCreateTestCase will create a test case using the JUnit test method name when no matching test case is found.

Code samples

Here you can find a complete example of how to upload NUnit results to Zephyr Scale:

https://github.com/SmartBear/zephyr-scale-nunit-automation

Suggested Improvements

Is something missing? If you’d like to suggest an improvement to this integration, take a look at our Ideas Portal.

You can search for existing ideas or create your own. We review ideas regularly and will take them into account for future development.

Highlight search results