Pytest Integration

Pytest is the standard test automation framework for Python. The integration with Zephyr Squad works through uploading a JUnit XML results file generated by pytest to Zephyr Squad. Once the file is uploaded to Zephyr Squad, it is possible to generate reports and leverage all the other capabilities thatZephyr Squad offers.

For more info on pytest, see here.

This integration works with JUnit 4 and JUnit 5. Both versions of JUnit export the same results file format - an XML containing the test results. Additionally, any framework that is able to export such file type should be compatible with the API endpoint used in this integration.

Writing pytest test

See below for an example of how to write test cases with pytest and Zephyr Squad.

class TestClass:

    # will match with test case with key DEV-T21
    def test_method_3_DEV_T21(self):
        assert 5 == 5

    # will match with test case named tests.test_sample.TestClass.test_method
    def test_method(self):
        x = 'this'
        assert 'h' in x


# will match with test case named tests.test_sample.test_method_1_without_class
def test_method_1_without_class():
    assert 1 == 1

Note the comments above each method describing how they map to existing test cases within Zephyr Squad. This varies depending on whether a test case key or name is provided, as shown above.

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 the Zephyr Squad. In this case, if you intend to automatically create test cases that don’t exist previously, a flag 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 test name following the <package name>.<class name (if using classes)>.<method 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

Configuring pytest to output JUnit XML results file

No configuration is required beforehand. In order to instruct pytest to generate the JUnit XML results file, all that is required is to execute the tests with --junitxml parameter followed by the XML file name.

Here is an example:

pytest --junitxml=output/junitxml_report.xml

Uploading results to Zephyr Squad

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

  • Using a single JUnit XML results file generated by pytest.

  • Using a zip file containing multiple JUnit XML results file generated by pytest.

Both options work the same way and can be uploaded using the same API endpoint: Zephyr Squad for Jira Cloud API .

Below, 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"&amp;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 projectKeyspecifies what project will be used to create the test cycle. The autoCreateTestCase will create a test case using the pytest test method name when no matching test case is found

Code samples

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

Publication date: