Merge pull request #2653 from felipedau/slow-sharing-note

Add note on sharing the `slow` marker in the basic examples
This commit is contained in:
Bruno Oliveira 2017-08-09 08:18:26 -03:00 committed by GitHub
commit e6020781f6
2 changed files with 14 additions and 10 deletions

1
changelog/2653.doc Normal file
View File

@ -0,0 +1 @@
In one of the simple examples, use `pytest_collection_modifyitems()` to skip tests based on a command-line option, allowing its sharing while preventing a user error when acessing `pytest.config` before the argument parsing.

View File

@ -127,7 +127,7 @@ Control skipping of tests according to command line option
.. regendoc:wipe .. regendoc:wipe
Here is a ``conftest.py`` file adding a ``--runslow`` command Here is a ``conftest.py`` file adding a ``--runslow`` command
line option to control skipping of ``slow`` marked tests: line option to control skipping of ``pytest.mark.slow`` marked tests:
.. code-block:: python .. code-block:: python
@ -136,7 +136,16 @@ line option to control skipping of ``slow`` marked tests:
import pytest import pytest
def pytest_addoption(parser): def pytest_addoption(parser):
parser.addoption("--runslow", action="store_true", parser.addoption("--runslow", action="store_true",
help="run slow tests") default=False, help="run slow tests")
def pytest_collection_modifyitems(config, items):
if config.getoption("--runslow"):
# --runslow given in cli: do not skip slow tests
return
skip_slow = pytest.mark.skip(reason="need --runslow option to run")
for item in items:
if "slow" in item.keywords:
item.add_marker(skip_slow)
We can now write a test module like this: We can now write a test module like this:
@ -146,17 +155,11 @@ We can now write a test module like this:
import pytest import pytest
slow = pytest.mark.skipif(
not pytest.config.getoption("--runslow"),
reason="need --runslow option to run"
)
def test_func_fast(): def test_func_fast():
pass pass
@slow @pytest.mark.slow
def test_func_slow(): def test_func_slow():
pass pass
@ -170,7 +173,7 @@ and when running it will see a skipped "slow" test::
test_module.py .s test_module.py .s
======= short test summary info ======== ======= short test summary info ========
SKIP [1] test_module.py:14: need --runslow option to run SKIP [1] test_module.py:8: need --runslow option to run
======= 1 passed, 1 skipped in 0.12 seconds ======== ======= 1 passed, 1 skipped in 0.12 seconds ========