pytest Tutorial – Part 2

In my last article pytest Tutorial Part 1  I showed some basic concepts of testing with pytest.

Here are two additional concepts


Test parameterization follows the concept of DRY – Don’t repeat yourself

Instead of writing a new testcase for every different value you annotate your test function and handover the different values. The test will be executed as often as there are values:

import pytest

    "data, expected_mean",
        ([1], 1),
        ([1, 2], 1.5)
def test_calculate_mean(data, expected_mean):


With markers you can control test case execution:

import pytest
def test_win_ver():

def test_lin_ver():

def test_stuff_mac_os():
# content of pytest.ini
markers =
    windows: tests code under win
    linux: tests code under lin
    mac_os: tests code under mac os

Next part: pytest Tutorial – Part 3