Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Parametrize PyTest with pytest.mark.parametrize

  • @pytest.mark.parametrize
  • mark
  • parametrize
import pytest

@pytest.mark.parametrize("name", ["Foo", "Bar"])
def test_cases(name):
    print(f"name={name}")
    assert len(name) == 3

Output:

======== test session starts ========
platform linux -- Python 3.7.3, pytest-5.3.2, py-1.8.0, pluggy-0.13.0
rootdir: /home/gabor/work/slides/python-programming/examples/pytest
plugins: flake8-1.0.4
collected 2 items

test_with_param.py name=Foo
.name=Bar
.

=========== 2 passed in 0.00s ========