Skip to content

Add note on sharing the slow marker in the basic examples #2653

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/2653.doc
Original file line number Diff line number Diff line change
@@ -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.
23 changes: 13 additions & 10 deletions doc/en/example/simple.rst
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ Control skipping of tests according to command line option
.. regendoc:wipe

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

Expand All @@ -136,7 +136,16 @@ line option to control skipping of ``slow`` marked tests:
import pytest
def pytest_addoption(parser):
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:

Expand All @@ -146,17 +155,11 @@ We can now write a test module like this:
import pytest


slow = pytest.mark.skipif(
not pytest.config.getoption("--runslow"),
reason="need --runslow option to run"
)


def test_func_fast():
pass


@slow
@pytest.mark.slow
def test_func_slow():
pass

Expand All @@ -170,7 +173,7 @@ and when running it will see a skipped "slow" test::

test_module.py .s
======= 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 ========

Expand Down