---
title: "Testing using pytest in Python"
canonical: "https://docs.getxray.app/space/XRAYCLOUD/44564769/Testing%20using%20pytest%20in%20Python"
format: markdown
---
# Overview

In this tutorial, we will create some tests in Python using [pytest](http://doc.pytest.org/en/latest/contents.html).

> ℹ️ **Please note**
> ℹ️ 
> ℹ️ There is a [more up-to-date tutorial using pytest](https://getxraydocs.atlassian.net/wiki/spaces/XRAYCLOUD/pages/44579651) providing additional capabilities. Please check it instead.

# 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**

```python
def func(x):
    return x + 1
def test_answer():
    assert func(3) == 5
```

##### **test_class.py**

```python
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., ), it can be imported to Xray (by using either the REST API or 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".

![image](media://dde58bfc-87f4-49ef-adc9-b140363496f0)

# References

- [Python Guide Writing Tests](http://docs.python-guide.org/en/latest/writing/tests/)
- [How to invoke pytest](http://doc.pytest.org/en/latest/usage.html#creating-junitxml-format-files)