KEMBAR78
Pytest Cheatsheet | PDF
0% found this document useful (0 votes)
66 views1 page

Pytest Cheatsheet

The document provides an overview of various pytest functionalities including grouping tests, using fixtures, parameterization, and handling test execution failures. It also explains how to run tests in parallel and generate test execution reports in XML format. Key commands and examples are included for clarity on usage.

Uploaded by

ayushman292140
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views1 page

Pytest Cheatsheet

The document provides an overview of various pytest functionalities including grouping tests, using fixtures, parameterization, and handling test execution failures. It also explains how to run tests in parallel and generate test execution reports in XML format. Key commands and examples are included for clarity on usage.

Uploaded by

ayushman292140
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

grouping

import pytest
@pytest.mark.great

pytest -m others -v

matching substring
pytest -k <substring> -v

Fixture
-------------
import pytest

@pytest.fixture
def input_value():
input = 39
return input

def test_divisible_by_3(input_value):
assert input_value % 3 == 0

def test_divisible_by_6(input_value):
assert input_value % 6 == 0

conftest.py ---> to make a fixture available to multiple test files, we have to


define the fixture function in a file called conftest.py.

@pytest.mark.parametrize
@pytest.mark.parametrize("num, output",[(1,11),(2,22),(3,35),(4,44)])

@pytest.mark.xfail
@pytest.mark.skip

what if we want to stop the execution of test suite soon after n number of test
fails. This can be done in pytest using maxfail
pytest --maxfail = <num>

Pytest - Run Tests in Parallel


================================
pip install pytest-xdist
pytest -n 3 --------n <num> runs the tests by using multiple workers, here it is
3.

We can generate the details of the test execution in an xml file.


==================================================================
pytest test_multiplication.py -v --junitxml="result.xml"

You might also like