PYTHON: PYTEST TESTING

Diwakar pratap
4 min readMar 30, 2022

Pytest could be a python primarily based testing framework, that is employed to jot down and execute take a look at codes. within the gift days of REST services, pytest is especially used for API testing even supposing we will use pytest to jot down easy to complicated tests, i.e., we will write codes to check API, database, UI, etc.

The pytest framework makes it straightforward to jot down little, clear tests, and may scale to support advanced useful testing for applications and libraries.

pytest requires

->Python 3.7+ or PyPy3.

PyPI package name

->pytest

Features

· Detailed data on failing assert statements (no got to keep in mind self.assert* names)
· Auto-discovery of take a look at modules and functions
· Modular fixtures for managing tiny or parametrized long take a look at resources
· Can run unittest (including trial) and nose take a look at suites out of the box
· Python 3.7+ or PyPy three
· Rich plugin design, with over 800+ external plugins and thriving communitys and libraries.

Installation commands

pip install pytest

Confirm the installation using the following command to display the help section of pytest.

pytest -h

Tests

· Fixture
· Parametrize
· Xfail
· Skip
· — maxfail

1. Fixture

Fixtures square measure functions, which can run before every take a look at perform to that it’s applied. Fixtures square measure accustomed feed some information to the tests like information connections, URLs to check and a few style of computer file. Therefore, rather than running an equivalent code for each take a look at, we will attach fixture perform to the take a look at and it’ll run and come the info to the take a look at before corporal punishment every test.

syntax
@pytest.fixture

Example

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

Output

test_div_by_3_6.py::test_divisible_by_3 PASSED
test_div_by_3_6.py::test_divisible_by_6 FAILED
=================FAILURES=================
________________________ test_divisible_by_6__________________________
input_value = 39
def test_divisible_by_6(input_value):
assert input_value % 6 == 0
E assert (39 % 6) == 0
test_div_by_3_6.py:12: AssertionError
======= 1 failed, 1 passed, 6 deselected in 0.07 seconds=======

2. Parametrize

Parameterizing of a test is done to run the test against multiple sets of inputs

Syntax
@pytest.mark.parametrize

Example

import pytest
@pytest.mark.parametrize(“num, output”,[(1,11),(2,22),(3,35),(4,44)])
def test_multiplication_11(num, output):
assert 11*num == output

Output

test_multiplication.py::test_multiplication_11[1–11] PASSED
test_multiplication.py::test_multiplication_11[2–22] PASSED
test_multiplication.py::test_multiplication_11[3–35] FAILED
test_multiplication.py::test_multiplication_11[4–44] PASSED
================FAILURES===============
_________________ test_multiplication_11[3–35] __________________
num = 3, output = 35
@pytest.mark.parametrize(“num, output”,[(1,11),(2,22),(3,35),(4,44)])
def test_multiplication_11(num, output):
> assert 11*num == output
E assert (11 * 3) == 35
test_multiplication.py:5: AssertionError
======= 1 failed, 3 passed, 8 deselected in 0.08seconds=========

3. Xfail

Pytest will execute the xfailed test, but it will not be considered as part failed or passed tests. Details of these tests will not be printed even if the test fails (remember pytest usually prints the failed test details).

Syntax

@pytest.mark.xfail

Example

import pytest
@pytest.mark.xfail
@pytest.mark.great
def test_greater():
num = 100
assert num > 100
@pytest.mark.xfail
@pytest.mark.great
def test_greater_equal():
num = 100
assert num >= 100

Output

test_compare.py::test_greater xfail
test_compare.py::test_greater_equal XPASS
test_compare.py::test_less SKIPPED
====== 1 skipped, 1 xfailed, 1 xpassed in 0.06seconds==========

4. Skip

Skipping a test means that the test will not be executed.

Syntax
@pytest.mark.skip

Example

import pytest
@pytest.mark.skip
@pytest.mark.others
def test_less():
num = 100
assert num < 200

Output

test_compare.py::test_greater xfail
test_compare.py::test_greater_equal XPASS
test_compare.py::test_less SKIPPED
======= 1 skipped, 1 xfailed, 1 xpassed in 0.06 seconds========

5. — maxfail

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.

Syntax
pytest — maxfail = <num>

Example

import pytest
import math
def test_sqrt_failure():
num = 25
assert math.sqrt(num) == 6
def test_square_failure():
num = 7
assert 7*7 == 40
def test_equality_failure():
assert 10 == 11

Output

pytest test_failure.py -v — maxfail 1
test_failure.py::test_sqrt_failure FAILED

===============FAILURES===================________________________test_sqrt_failure __________________________
def test_sqrt_failure():
num = 25
> assert math.sqrt(num) == 6
E assert 5.0 == 6
E + where 5.0 = <built-in function sqrt>(25)
E + where <built-in function sqrt>= math.sqrt
test_failure.py:6: AssertionError
============= 1 failed in 0.04 seconds=============

Advantages

· Pytest will run multiple tests in parallel, that reduces the execution time of the take a look at suite.
· Pytest has its own thanks to find the take a look at file and take a look at functions mechanically, if not mentioned expressly.
· Pytest permits North American nation to skip a set of the tests throughout execution.
· Pytest permits North American nation to run a set of the whole take a look at suite.
· Pytest is free and open supply.
· Because of its easy syntax, pytest is extremely straightforward to start out with.

Example

def inc(x):
return x + 1
def test_answer():
assert inc(3) == 5

Output

$ pytest
================= test session starts ===========
platform linux — Python 3.x.y, pytest-7.x.y, pluggy-1.x.y
rootdir: /home/sweet/project
collected 1 item
test_sample.py F [100%]
================ FAILURES ==================
_______________________ test_answer ________________________________
def test_answer():
> assert inc(3) == 5
E assert 4 == 5
E + where 4 = inc(3)
test_sample.py:6: AssertionError
============= short test summary info =============
FAILED test_sample.py::test_answer — assert 4 == 5
============= 1 failed in 0.12s ==============

--

--