Skip to main content

Use @pytest.mark.only to run a single test

Project description

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

Development

  1. Install the test/dev requirements

    $ pip install -r dev-requirements.txt
  2. Run the tests

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

    $ tox

Project details


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-1.2.2.tar.gz (3.1 kB view details)

Uploaded Source

File details

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

File metadata

  • Download URL: pytest-only-1.2.2.tar.gz
  • Upload date:
  • Size: 3.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.6.9

File hashes

Hashes for pytest-only-1.2.2.tar.gz
Algorithm Hash digest
SHA256 90789b1f6d867f14ad2935f6499f44b66338a92d6d6aa53819203c8411c59ba3
MD5 326c151d96f5ffbe27a1b483965f2083
BLAKE2b-256 faa2686f5604869e3258558af13f593e5fcf8895c7183291004327fa69a587d2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page