Skip to content

Commit 0a3cd88

Browse files
committed
Add docs about using pytest.param in parametrized fixtures
1 parent 2efaf39 commit 0a3cd88

File tree

4 files changed

+46
-11
lines changed

4 files changed

+46
-11
lines changed

_pytest/mark/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ class MarkerError(Exception):
2020

2121

2222
def param(*values, **kw):
23-
"""Specify a parameter in a `pytest.mark.parametrize`_ call.
23+
"""Specify a parameter in `pytest.mark.parametrize`_ calls or
24+
:ref:`parametrized fixtures <fixture-parametrize-marks>`.
2425
2526
.. code-block:: python
2627

doc/en/example/reportingdemo.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ get on the terminal - we are working on that)::
358358
> int(s)
359359
E ValueError: invalid literal for int() with base 10: 'qwe'
360360
361-
<0-codegen $PYTHON_PREFIX/lib/python3.5/site-packages/_pytest/python_api.py:609>:1: ValueError
361+
<0-codegen $PYTHON_PREFIX/lib/python3.5/site-packages/_pytest/python_api.py:613>:1: ValueError
362362
______________________ TestRaises.test_raises_doesnt _______________________
363363
364364
self = <failure_demo.TestRaises object at 0xdeadbeef>

doc/en/fixture.rst

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,40 @@ Running the above tests results in the following test IDs being used::
623623
624624
======================= no tests ran in 0.12 seconds =======================
625625

626+
.. _`fixture-parametrize-marks`:
627+
628+
Using marks with parametrized fixtures
629+
--------------------------------------
630+
631+
:func:`pytest.param` can be used to apply marks in values sets of parametrized fixtures in the same way
632+
that they can be used with :ref:`@pytest.mark.parametrize <@pytest.mark.parametrize>`.
633+
634+
Example::
635+
636+
# content of test_fixture_marks.py
637+
import pytest
638+
@pytest.fixture(params=[0, 1, pytest.param(2, marks=pytest.mark.skip)])
639+
def data_set(request):
640+
return request.param
641+
642+
def test_data(data_set):
643+
pass
644+
645+
Running this test will *skip* the invocation of ``data_set`` with value ``2``::
646+
647+
$ pytest test_fixture_marks.py -v
648+
=========================== test session starts ============================
649+
platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y -- $PYTHON_PREFIX/bin/python3.5
650+
cachedir: .pytest_cache
651+
rootdir: $REGENDOC_TMPDIR, inifile:
652+
collecting ... collected 3 items
653+
654+
test_fixture_marks.py::test_data[0] PASSED [ 33%]
655+
test_fixture_marks.py::test_data[1] PASSED [ 66%]
656+
test_fixture_marks.py::test_data[2] SKIPPED [100%]
657+
658+
=================== 2 passed, 1 skipped in 0.12 seconds ====================
659+
626660
.. _`interdependent fixtures`:
627661

628662
Modularity: using fixtures from a fixture function

doc/en/warnings.rst

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ Running pytest now produces this output::
2525
platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y
2626
rootdir: $REGENDOC_TMPDIR, inifile:
2727
collected 1 item
28-
28+
2929
test_show_warnings.py . [100%]
30-
30+
3131
============================= warnings summary =============================
3232
test_show_warnings.py::test_one
3333
$REGENDOC_TMPDIR/test_show_warnings.py:4: UserWarning: api v1, should use functions from v2
3434
warnings.warn(UserWarning("api v1, should use functions from v2"))
35-
35+
3636
-- Docs: http://doc.pytest.org/en/latest/warnings.html
3737
=================== 1 passed, 1 warnings in 0.12 seconds ===================
3838

@@ -45,17 +45,17 @@ them into errors::
4545
F [100%]
4646
================================= FAILURES =================================
4747
_________________________________ test_one _________________________________
48-
48+
4949
def test_one():
5050
> assert api_v1() == 1
51-
52-
test_show_warnings.py:8:
53-
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
54-
51+
52+
test_show_warnings.py:8:
53+
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
54+
5555
def api_v1():
5656
> warnings.warn(UserWarning("api v1, should use functions from v2"))
5757
E UserWarning: api v1, should use functions from v2
58-
58+
5959
test_show_warnings.py:4: UserWarning
6060
1 failed in 0.12 seconds
6161

0 commit comments

Comments
 (0)