Skip to main content

Only run tests marked with @pytest.mark.only. If none are marked, all tests run as usual.

Borrowed from mocha.

Installation

pip install pytest-only

Usage

Use it on functions

import pytest

def test_that_will_not_run():
    assert 0

@pytest.mark.only
def test_that_will_run():
    assert 1
$ py.test -v test_example.py

============================= test session starts ==============================
platform linux -- Python 3.6.1, pytest-3.0.7, py-1.4.33, pluggy-0.4.0 -- /tmp/example/bin/python3.6
cachedir: .cache
rootdir: /tmp/example, inifile:
plugins: only-1.0.0
collected 2 items

test_example.py::test_that_will_run PASSED

=========================== 1 passed in 0.00 seconds ===========================

Or use it on classes

import pytest

class TestThatWillNotRun:
    def test_that_will_not_run(self):
        assert 0


@pytest.mark.only
class TestThatWillRun:
    def test_that_will_run(self):
        assert 1
$ py.test -v test_example.py

============================= test session starts ==============================
platform linux -- Python 3.6.1, pytest-3.0.7, py-1.4.33, pluggy-0.4.0 -- /tmp/example/bin/python3.6
cachedir: .cache
rootdir: /tmp/example, inifile:
plugins: only-1.0.0
collected 2 items

test_example.py::TestThatWillRun::test_that_will_run PASSED

=========================== 1 passed in 0.00 seconds ===========================

Or use it on modules

# test_example.py
import pytest

pytestmark = pytest.mark.only

def test_that_will_run():
    assert 1
# test_example2.py
def test_that_will_not_run():
    assert 0
$ py.test -v test_example.py test_example2.py

============================= test session starts ==============================
platform linux -- Python 3.6.1, pytest-3.0.7, py-1.4.33, pluggy-0.4.0 -- /home/they4kman/.virtualenvs/tmp-53d5944c7c78d28/bin/python3.6
cachedir: .cache
rootdir: /home/they4kman/.virtualenvs/tmp-53d5944c7c78d28, inifile:
plugins: only-1.0.0
collected 2 items

test_example.py::test_that_will_run PASSED

=========================== 1 passed in 0.00 seconds ===========================

Disable for single test run

To run all the tests, regardless of whether @pytest.mark.only is used, pass the --no-only flag to pytest:

$ py.test --no-only

If --no-only has already been passed (perhaps by way of addopts in pytest.ini), use the --only flag to re-enable it:

$ py.test --no-only --only

Pylint checker

If you use pylint, you can avoid committing stray only marks with the bundled plugin. Just enable the pylint checker in your plugins and enable the unexpected-focused rule.

[MASTER]
load-plugins=pytest_only.ext.pylint

[MESSAGES CONTROL]
enable=unexpected-focused
$ cat test_ninja.py
import pytest

@pytest.mark.only
def test_ninja():
    pass

$ pylint test_ninja.py
************* Module mymain
test_ninja.py:3:0: W1650: Unexpected focused test(s) using pytest.mark.only: def test_ninja (unexpected-focused)

Development

  1. Install the test/dev requirements using Poetry

    poetry install
  2. Run the tests

    py.test
  3. Run the tests on all currently-supported platforms

    tox

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest-only-2.0.0.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_only-2.0.0-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file pytest-only-2.0.0.tar.gz.

File metadata

  • Download URL: pytest-only-2.0.0.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.10.4 Linux/5.13.0-41-generic

File hashes

Hashes for pytest-only-2.0.0.tar.gz
Algorithm Hash digest
SHA256 0467eefac714a920b2df2e3c1d2fbf61cf2cd65583ccd2e3233f107862a5fc77
MD5 d0abb90457b8e23bf0e268ae55a2d70e
BLAKE2b-256 f11d2162c1516bef8ecb4ded0153a230359782d9d2fc67756b66e46170feefce

See more details on using hashes here.

File details

Details for the file pytest_only-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: pytest_only-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.10.4 Linux/5.13.0-41-generic

File hashes

Hashes for pytest_only-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1f344d15d6495ca108b341ca12ca90eb1695bebb3bd3978c8efd4a0e121bdb8f
MD5 5003be875bc18a3ffe6abdbf033db553
BLAKE2b-256 7373204b6cfec954bf64e08bffac545e6f0c97d09829294a64a74c86b0d9a166

See more details on using hashes here.

Release history Release notifications | RSS feed

2.1.2

2 files

2.1.1

2 files

2.1.0

2 files

This release

2.0.0 This release

2 files

1.2.2

1 file

1.2.1

1 file

1.2.0

1 file

1.1.0

1 file

1.0.1

1 file

1.0.0

1 file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page