You can use -k option to run test cases with different patterns:

py.test tests_directory/foo.py tests_directory/bar.py -k 'test_001 or test_some_other_test'

This will run test cases with name test_001 and test_some_other_test deselecting the rest of the test cases.

Note: This will select any test case starting with test_001 or test_some_other_test. For example, if you have test case test_0012 it will also be selected.

Answer from supamaze on Stack Overflow
🌐
pytest
docs.pytest.org › en › stable › how-to › usage.html
How to invoke pytest - pytest documentation
The example above will run TestMyClass.test_something but not TestMyClass.test_method_simple. Use "" instead of '' in expression when running this on Windows ... Pass the module filename relative to the working directory, followed by specifiers like the class name and function name separated ...
Top answer
1 of 12
808

You can use -k option to run test cases with different patterns:

py.test tests_directory/foo.py tests_directory/bar.py -k 'test_001 or test_some_other_test'

This will run test cases with name test_001 and test_some_other_test deselecting the rest of the test cases.

Note: This will select any test case starting with test_001 or test_some_other_test. For example, if you have test case test_0012 it will also be selected.

2 of 12
515

Specifying tests / selecting tests

Pytest supports several ways to run and select tests from the command-line.

Run tests in a module

pytest test_mod.py

Run tests in a directory

pytest testing/

Run tests by keyword expressions

pytest -k "MyClass and not method"

This will run tests which contain names that match the given string expression, which can include Python operators that use filenames, class names and function names as variables. The example above will run TestMyClass.test_something but not TestMyClass.test_method_simple.

Run tests by node ids

Each collected test is assigned a unique nodeid which consist of the module filename followed by specifiers like class names, function names and parameters from parametrization, separated by :: characters.

To run a specific test within a module:

pytest test_mod.py::test_func

Another example specifying a test method in the command line:

pytest test_mod.py::TestClass::test_method

Run tests by marker expressions

pytest -m slow

Will run all tests which are decorated with the @pytest.mark.slow decorator.

For more information see marks.

Run tests from packages

pytest --pyargs pkg.testing

This will import pkg.testing and use its filesystem location to find and run tests from.

Source: https://docs.pytest.org/en/latest/usage.html#specifying-tests-selecting-tests

🌐
pytest
docs.pytest.org › en › 6.2.x › usage.html
Usage and Invocations — pytest documentation
Example: to disable loading the plugin doctest, which is responsible for executing doctest tests from text files, invoke pytest like this: ... this acts as if you would call “pytest” from the command line. It will not raise SystemExit but return the exitcode instead. You can pass in options and arguments: ... # content of myinvoke.py import pytest class MyPlugin: def pytest_sessionfinish(self): print("*** test run reporting finishing") pytest.main(["-qq"], plugins=[MyPlugin()])
🌐
freeCodeCamp
freecodecamp.org › news › how-to-use-pytest-a-guide-to-testing-in-python
How to Use pytest: A Simple Guide to Testing in Python
July 10, 2025 - This command would run all the ... a specific class: To run a specific test inside a specific class, use pytest test_example.py::TestClass::test_addition....
🌐
Continuously Merging
articles.mergify.com › pytest-run-specific-test
pytest run specific test: Quick & Effective Commands
June 27, 2025 - For example, if you have a test function named test_addition parameterized with two values, x and y, you can run a specific test case using a command like pytest -k 'test_addition[1-2]'. This executes only the instance of test_addition where ...
🌐
Pytest with Eric
pytest-with-eric.com › introduction › pytest-run-single-test
How To Run A Single Test In Pytest (Using CLI And Markers) | Pytest with Eric
September 4, 2023 - To run a specific test, you can use the test’s node ID, which is essentially its path in the syntax filename.py::test_function_name. For example, to run the test_add_negative_numbers function in the test_functions.py file, you can use the ...
🌐
pytest
docs.pytest.org › en › 7.1.x › how-to › usage.html
How to invoke pytest — pytest documentation
The example above will run TestMyClass.test_something but not TestMyClass.test_method_simple. ... Each collected test is assigned a unique nodeid which consist of the module filename followed by specifiers like class names, function names and parameters from parametrization, separated by :: ...
🌐
GitHub
github.com › Pytest-with-Eric › pytest-run-single-test-example
GitHub - Pytest-with-Eric/pytest-run-single-test-example: How To Run A Single Test In Pytest (Using CLI And Markers) · GitHub
This repo contains the sample code for the article - How To Run A Single Test In Pytest (Using CLI And Markers)
Author   Pytest-with-Eric
Find elsewhere
🌐
Scientific Python Development
learn.scientific-python.org › development › guides › pytest
Testing with pytest - Scientific Python Development Guide
Here’s an advanced example, which also uses monkeypatch, which is a great way for making things hard to split into units into unit tests. Let’s say you wanted to make a test that “tricked” your code into thinking that it was running on different platforms: import platform import pytest @pytest.fixture(params=["Linux", "Darwin", "Windows"], autouse=True) def platform_system(request, monkeypatch): monkeypatch.setattr(platform, "system", lambda _: request.param)
🌐
pytest
docs.pytest.org › en › stable › getting-started.html
Get Started - pytest documentation
$ pytest =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-9.x.y, pluggy-1.x.y rootdir: /home/sweet/project collected 1 item test_sample.py F [100%] ================================= FAILURES ================================= _______________________________ test_answer ________________________________ def test_answer(): > assert func(3) == 5 E assert 4 == 5 E + where 4 = func(3) test_sample.py:6: AssertionError ========================= short test summary info ========================== FAILED test_sample.py::test_answer - assert 4 == 5 ============================ 1 failed in 0.12s ============================= The [100%] refers to the overall progress of running all test cases.
🌐
Continuously Merging
articles.mergify.com › pytest-run-single-test
pytest run single test: Boost Your Testing Workflow
April 6, 2025 - One of the simplest ways to run a single test is by specifying the file path and the test function name. For example, to run the test_addition function inside the test_calculations.py file, use: pytest tests/test_calculations.py::test_addition.
🌐
TutorialsPoint
tutorialspoint.com › pytest › pytest_quick_guide.htm
Pytest - Quick Guide
Suppose, we want to run only a specific set of tests; how do we go about it? Pytest provides two ways to run the subset of the test suite. Select tests to run based on substring matching of test names. Select tests groups to run based on the markers applied. We will explain these two with examples in subsequent chapters.
🌐
Jaketrent
jaketrent.com › post › run-single-pytest-test
Run Single pytest Test - Jake Trent
May 23, 2022 - def test_one(): # ... def test_two(): # ... def test_three(): # ... And you only want to run test_two, you can use the -k parameter to match on function name substring:
🌐
JetBrains
jetbrains.com › guide › pytest › tips › run-single-test
Run Single Test - JetBrains Guide
February 17, 2023 - With the cursor anywhere in the test you want to focus on, right-click and choose to run that in the test runner
🌐
pytest
docs.pytest.org › en › stable › example › simple.html
Basic patterns and examples - pytest documentation
Let’s run this without supplying our new option: $ pytest -q test_sample.py F [100%] ================================= FAILURES ================================= _______________________________ test_answer ________________________________ cmdopt = 'type1' def test_answer(cmdopt): if cmdopt ...
🌐
qavalidation
qavalidation.com › home › python › pytest options – how to…
Pytest options - how to skip or run specific tests - qavalidation
January 21, 2021 - This above command will run all the test methods, but will not print the output to console. To print the output, we have to use -s along with pytest ... To print the console output along with specific test names, we can use the pytest option -v [verbose]
🌐
pytest
docs.pytest.org › en › stable › how-to › unittest.html
How to use unittest-based tests with pytest - pytest documentation
This is a shortcut for using a @pytest.mark.usefixtures("initdir") marker on the class like in the previous example. ... … gives us one passed test because the initdir fixture function was executed ahead of the test_method. ... unittest.TestCase methods cannot directly receive fixture arguments as implementing that is likely to inflict on the ability to run general unittest.TestCase test suites.
🌐
Pynerds
pynerds.com › testing › how-to-run-tests-with-pytest
How to run tests with Pytest
February 21, 2025 - The pytest command allows you to specify the paths of the specific file you want to be executed. This way, only the specified test files will get executed, the rest will not. ... For example, to execute only test_2.py, we can run the following ...
🌐
Better Stack
betterstack.com › community › questions › how-to-test-a-single-file-under-pytest
How to Test a Single File Under Pytest | Better Stack Community
May 15, 2024 - ... To run a single test file with pytest, use the command pytest followed by the file path: pytest tests/test_file.py To execute a specific test within that file, append :: and the test name to the fi...
🌐
O'Reilly
oreilly.com › library › view › python-testing-with › 9781680502848 › f_0014.xhtml
Python Testing with pytest - Python Testing with pytest [Book]
September 15, 2017 - Running Only One Test One of the first things you’ll want to do once you’ve started writing tests is to run just one. Specify the file directly, and add a ::test_name, like this:... - Selection from Python Testing with pytest [Book]
Author   Brian Okken
Published   2017
Pages   222