NUnit Integration

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

The NUnit integration 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 test

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. These particular identifiers which can be either prefix or suffix will map the result of each of those test methods to the corresponding test cases in Zephyr Squad 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 Squad 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 Squad. 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 Squad. 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 Squad.

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 Squad

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 execute the tests and generate the JUnit XML results file, It is required 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 been executed, the results can be uploaded to Zephyr Squad. There are two options for uploading test results:

  • Using a single JUnit XML results file.

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

Both options work the same way and can be uploaded using the same API endpoint:

https://smartbear.portal.swaggerhub.com/zephyr-squad/default/zephyr-zquad-cloud-api-2#/Automations/createJUnitExecutions

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

curl -H "Authorization: Bearer ${TOKEN}" -F "[email protected];type=application/xml" https://prod-api.zephyr4jiracloud.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 "[email protected];type=application/x-zip-compressed" https://prod-api.zephyr4jiracloud.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 Squad:

https://github.com/SmartBear/zephyr-for-jira-examples

Publication date: