Overview

In this tutorial, we will create some tests in Python using pytest.

Requirements

  • Install the Python "pip"
  • Install "pytest"
 pip install -U pytest


Description

Below are two Python scripts implementing tests with subtle differences.

test_sample.py
def func(x):
    return x + 1
def test_answer():
    assert func(3) == 5
test_class.py
class TestClass:
    def test_one(self):
        x = "this"
        assert 'h' in x
    def test_two(self):
        x = "hello"
        assert hasattr(x, 'check')


After running the tests and generating the JUnit XML report (e.g., py-results1.xml), it can be imported to Xray (either by the REST API or through the Import Execution Results action within the Test Execution).

pytest --junitxml=py-results1.xml


JUnit's Test Case is mapped to a Generic Test in Jira, and the Generic Test Definition field contains "pytest" concatenated with the name of the method that implements the test case.

The Execution Details of the Generic Test contains information about the Test Suite, which in this case corresponds to "pytest".


References