@@ -485,4 +485,54 @@ of our ``test_func1`` was skipped. A few notes:
485485 values as well.
486486
487487
488+ Set marks or test ID for individual parametrized test
489+ --------------------------------------------------------------------
490+
491+ Use ``pytest.param `` to apply marks or set test ID to individual parametrized test.
492+ For example::
493+
494+ # content of test_pytest_param_example.py
495+ import pytest
496+ @pytest.mark.parametrize('test_input,expected', [
497+ ('3+5', 8),
498+ pytest.param('1+7', 8,
499+ marks=pytest.mark.basic),
500+ pytest.param('2+4', 6,
501+ marks=pytest.mark.basic,
502+ id='basic_2+4'),
503+ pytest.param('6*9', 42,
504+ marks=[pytest.mark.basic, pytest.mark.xfail],
505+ id='basic_6*9'),
506+ ])
507+ def test_eval(test_input, expected):
508+ assert eval(test_input) == expected
509+
510+ In this example, we have 4 parametrized tests. Except for the first test,
511+ we mark the rest three parametrized tests with the custom marker ``basic ``,
512+ and for the fourth test we also use the built-in mark ``xfail `` to indicate this
513+ test is expected to fail. For explicitness, we set test ids for some tests.
514+
515+ Then run ``pytest `` with verbose mode and with only the ``basic `` marker::
516+
517+ pytest -v -m basic
518+ ============================================ test session starts =============================================
519+ platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y
520+ rootdir: $REGENDOC_TMPDIR, inifile:
521+ collected 4 items
522+
523+ test_pytest_param_example.py::test_eval[1+7-8] PASSED
524+ test_pytest_param_example.py::test_eval[basic_2+4] PASSED
525+ test_pytest_param_example.py::test_eval[basic_6*9] xfail
526+ ========================================== short test summary info ===========================================
527+ XFAIL test_pytest_param_example.py::test_eval[basic_6*9]
528+
529+ ============================================= 1 tests deselected =============================================
530+
531+ As the result:
488532
533+ - Four tests were collected
534+ - One test was deselected because it doesn't have the ``basic `` mark.
535+ - Three tests with the ``basic `` mark was selected.
536+ - The test ``test_eval[1+7-8] `` passed, but the name is autogenerated and confusing.
537+ - The test ``test_eval[basic_2+4] `` passed.
538+ - The test ``test_eval[basic_6*9] `` was expected to fail and did fail.
0 commit comments