How can I ensure tests with a marker are only run if explicitly asked in pytest?

Stefano Borini

I have some tests I marked with an appropriate marker. If I run pytest, by default they run, but I would like to skip them by default. The only option I know is to explicitly say "not marker" at pytest invocation, but I would like them not to run by default unless the marker is explicitly asked at command line.

hoefling

A slight modification of the example in Control skipping of tests according to command line option:

# conftest.py

import pytest


def pytest_collection_modifyitems(config, items):
    keywordexpr = config.option.keyword
    markexpr = config.option.markexpr
    if keywordexpr or markexpr:
        return  # let pytest handle this

    skip_mymarker = pytest.mark.skip(reason='mymarker not selected')
    for item in items:
        if 'mymarker' in item.keywords:
            item.add_marker(skip_mymarker)

Example tests:

import pytest


def test_not_marked():
    pass


@pytest.mark.mymarker
def test_marked():
    pass

Running the tests with the marker:

$ pytest -v -k mymarker
...
collected 2 items / 1 deselected / 1 selected
test_spam.py::test_marked PASSED
...

Or:

$ pytest -v -m mymarker
...
collected 2 items / 1 deselected / 1 selected
test_spam.py::test_marked PASSED
...

Without the marker:

$ pytest -v
...
collected 2 items

test_spam.py::test_not_marked PASSED
test_spam.py::test_marked SKIPPED
...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Can I explicitly specify multiple tests in PyTest?

How can I ensure that only one if a kind of Jenkins job is run?

How can I ensure that pytest fixture function is executed only once irrespective of howmany calls within same module

How can I run cleanup method only after tagged tests?

How can I run google death tests in debug only?

How can I make test data files accessible to pytest tests when run with tox?

How to get pytest-django to only run tests for a single app

How to fix pytest marker not picking up tests

How can I ignore fixtures when I run Pytest --collect-only?

How can I make Mix run only specific tests from my suite of tests?

How can i run only the unit tests not the integration tests on build phase with maven

pytest how to run tests / django

In Pytest, how can I view the names of my tests as they are running?

How can I create suite/aggregate tests with pytest

How can I ensure that arguments have same type without listing the types explicitly?

How to ensure background jobs run in integration tests?

Can tests with pytest fixtures be run interactively?

Can pytest run tests within a test class?

How can I run kotlintest tests with gradle?

How can I run tests with a headless browser?

How can I run testfx tests in series?

How can I run Jasmine tests with Deno?

How can I ensure my cronjob will run at specified time?

How can I use Maven to run only previously failed JUnit 5 tests?

How can I ensure a value is only incremented once in Firestore?

How can I ensure that a Rust vector only contains alternating types?

How can I safely ensure a variable contains only a valid filename?

When pytest run all tests in a directory, how can it decide which test to run last?

In IntelliJ IDEA, can I run only tests matching a regex pattern?