Skip to content

Instantly share code, notes, and snippets.

@okken
Last active December 5, 2019 11:32
Show Gist options
  • Save okken/6a7fde2eb76381f79749bf8d17b337b1 to your computer and use it in GitHub Desktop.
Save okken/6a7fde2eb76381f79749bf8d17b337b1 to your computer and use it in GitHub Desktop.
pytest parameterized ids function differences between fixtures and functions/classes
(venv) $ pytest -v test_ids.py
============================= test session starts ==============================
platform darwin -- Python 3.5.2, pytest-3.0.0, py-1.4.31, pluggy-0.3.1 -- /Users/okken/projects/book/bopytest/Book/venv/bin/python3.5
cachedir: ../.cache
rootdir: /Users/okken/projects/book/bopytest/Book/code/pytest/um_project, inifile:
collected 8 items
test_ids.py::test_ids_strings[a] PASSED
test_ids.py::test_ids_strings[b] PASSED
test_ids.py::test_ids_paramterized_function[<1>-<2>] PASSED
test_ids.py::test_ids_paramterized_function[<2>-<4>] PASSED
test_ids.py::TestIds::test_ids_paramterized_class[<1>-<2>] PASSED
test_ids.py::TestIds::test_ids_paramterized_class[<2>-<4>] PASSED
test_ids.py::test_ids_parameterized_fixture[<(1, 2)>] PASSED
test_ids.py::test_ids_parameterized_fixture[<(2, 4)>] PASSED
# -----------------------------------------------------------------------
# To Demonstrate ids work differently in functions/classes than fixtures
# -----------------------------------------------------------------------
def id_func(value):
return '<{}>'.format(value)
@pytest.mark.parametrize("x,expected", testdata, ids=id_func)
def test_ids_paramterized_function(x,expected):
assert times_2(x) == expected
@pytest.mark.parametrize("x,expected", testdata, ids=id_func)
class TestIds:
def test_ids_paramterized_class(self,x,expected):
assert times_2(x) == expected
@pytest.fixture(params=testdata, ids=id_func)
def x_expected_data(request):
x, expected = request.param
return {'x': x, 'expected':expected }
def test_ids_parameterized_fixture(x_expected_data):
x = x_expected_data['x']
expected = x_expected_data['expected']
assert times_2(x) == expected
@nnop
Copy link

nnop commented Dec 5, 2019

Where does testdata came from?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment