Use @pytest.mark.only to run a single test
Project Links
Meta
Author: Zach Kanzler
Requires Python: >=3.8,<4.0
Classifiers
Development Status
- 5 - Production/Stable
Framework
- Pytest
License
- OSI Approved :: MIT License
Programming Language
- Python
- Python :: 3
- Python :: 3.8
- Python :: 3.9
- Python :: 3.10
- Python :: 3.11
- Python :: 3.12
Topic
- Software Development :: Testing
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
Install the test/dev requirements using Poetry
poetry installRun the tests
py.testRun the tests on all currently-supported platforms
tox
2.1.2
May 27, 2024
2.1.1
Mar 09, 2024
2.1.0
Mar 08, 2024
2.0.0
Jun 14, 2022
1.2.2
Jan 19, 2020
1.2.1
Jan 10, 2019
1.2.0
Dec 24, 2018
1.1.0
Mar 28, 2017
1.0.1
Mar 28, 2017
1.0.0
Mar 28, 2017
Wheel compatibility matrix
Files in release
Extras:
None
Dependencies:
pytest
(<9,>=3.6.0)