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
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

🌐
TutorialsPoint
tutorialspoint.com › pytest › pytest_file_execution.htm
Pytest - File Execution
To execute the tests from a specific file, use the following syntax − ... The above command will execute the tests only from file test_compare.py. Our result will be − · ============================ test session starts ================================ platform win32 -- Python 3.14.2, pytest-9.0.2, pluggy-1.6.0 -- C:\Users\mahes\AppData\Local\Programs\Python\Python314\python.exe cachedir: .pytest_cache rootdir: D:\Projects\python\myenv\automation collected 3 items test_compare.py::test_greater FAILED [ 33%] test_compare.py::test_greater_equal PASSED [ 66%] test_compare.py::test_less PASSED
🌐
TutorialsPoint
tutorialspoint.com › pytest › pytest_execute_subset_of_test_suite.htm
Pytest - Execute a Subset of Test Suite
In a real scenario, we will have multiple test files and each file will have a number of tests. Tests will cover various modules and functionalities. 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.
🌐
pytest
docs.pytest.org › en › stable › explanation › goodpractices.html
Good Integration Practices - pytest documentation
If you do not use an editable install and do not use the src layout (mypkg directly in the root directory) you can rely on the fact that Python by default puts the current directory in sys.path to import your package and run python -m pytest to execute the tests against the local copy directly.
🌐
GitHub
github.com › pytest-dev › pytest › issues › 3198
Ignore individual tests · Issue #3198 · pytest-dev/pytest
February 9, 2018 - If I want to run every test except for test_a.py::test_foo I will have a very long command line. I propose to accept test ids with --ignore: $ pytest tests --ignore tests/test_a.py::test_foo $ pytest tests --ignore tests/test_a.py::test_foo[param] ...
Author   uSpike
🌐
TestMu AI Community
community.testmuai.com › ask a question
How to Run Pytest Tests from a List of Test Paths - Ask a Question - TestMu AI Community
October 1, 2024 - For example, if I have a file foo.txt ... names, from different directories using pytest? While pytest -k allows for a single pattern, I need to run different combinations of tests from various files. Is there a way to specify multiple pa......
🌐
pytest
docs.pytest.org › en › stable › how-to › usage.html
How to invoke pytest - pytest documentation
This will execute all tests in all files whose names follow the form test_*.py or *_test.py in the current directory and its subdirectories. More generally, pytest follows standard test discovery rules. Pytest supports several ways to run and select tests from the command-line or from a file ...
Find elsewhere
🌐
Pytest with Eric
pytest-with-eric.com › introduction › pytest-k-options
How To Filter Tests Effortlessly With Pytest -K Options | Pytest with Eric
March 28, 2024 - The Pytest -k option (k for keyword) allows you to filter or group your tests by keyword and keyword expressions, saving your valuable time when working on large test suites, allowing you to selectively run tests based on their names.
🌐
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 == "type1": print("first") elif cmdopt == "type2": print("second") > assert 0 # to see what was printed ^^^^^^^^ E assert 0 test_sample.py:6: AssertionError --------------------------- Captured stdout call --------------------------- first ========================= short test summary info ========================== FAILED test_sample.py::test_answer - assert 0 1 failed in 0.12s
🌐
Aikchar
aikchar.dev › blog › run-pytest-tests-easily-locally-and-in-pipeline.html
Run pytest Tests Easily Locally and in Pipeline | aikchar.dev
June 2, 2025 - ifeq ($(CI_PROJECT_DIR),) MAKEFILE_PATH := $(abspath $(lastword $(MAKEFILE_LIST))) MAKEFILE_DIR := $(dir $(MAKEFILE_PATH)) ROOT_DIR := $(shell dirname $(MAKEFILE_DIR)) else ROOT_DIR := $(CI_PROJECT_DIR) endif ifeq ($(PYTHONPATH),) LIBRARY_PYTHONPATH=$(ROOT_DIR)/library FILTER_PLUGINS_PYTHONPATH=$(ROOT_DIR)/filter_plugins else LIBRARY_PYTHONPATH=$(PYTHONPATH):$(ROOT_DIR)/library FILTER_PLUGINS_PYTHONPATH=$(PYTHONPATH):$(ROOT_DIR)/filter_plugins endif .PHONY: test test: library-tests filter-plugins-tests .PHONY: library-tests library-tests: PYTHONPATH=$(LIBRARY_PYTHONPATH) pytest -vvv -rP $(ROOT_DIR)/tests/library/ .PHONY: filter-plugins-tests filter-plugins-tests: PYTHONPATH=$(FILTER_PLUGINS_PYTHONPATH) pytest -vvv -rP $(ROOT_DIR)/tests/filter_plugins/
🌐
Advancedpython
advancedpython.dev › articles › pytest-filtering
Running a subset of tests with PyTest | Advanced Python Development
July 3, 2020 - If you ever find yourself saying "I'll make myself a cup of tea while the tests run" then your tests are probably too slow. That's not to say that you shouldn't make tea, of course, but that the natural time to take a break is when your tests have passed (so you're done) or they've failed and you need a moment to think about what to do next. In Chapter 2 of the book, I describe using PyTest's mark syntax to declare tests as being slow and then filtering by markers to run only the faster tests.
🌐
Visual Studio Code
code.visualstudio.com › docs › python › testing
Python testing in Visual Studio Code
November 3, 2021 - To run tests with coverage enabled, select the coverage run icon in the Test Explorer or the Run with coverage option from any menu you normally trigger test runs from. The Python extension runs coverage using the pytest-cov plugin if you are ...
🌐
{app_name}/
camkode.com › posts › unit-test-python-code-with-pytest
How to Unit Test Python Code with pytest
First, you need to install pytest if you haven't already done so. You can install it via pip: ... Create a Python file for your tests, typically named test_<module_name>.py. In this file, write test functions using the naming convention test_<something>. # test_my_module.py def test_addition(): assert 1 + 1 == 2 def test_subtraction(): assert 5 - 3 == 2 ... ===================== test session starts ====================== platform win32 -- Python 3.12.1, pytest-8.1.1, pluggy-1.4.0 rootdir: C:\Users\kosal\code\python\test\pytest collected 2 items test_my_module.py ..
🌐
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 - Understand how to run basic pytest tests. Control what test to run. Skip tests if needed. Use markers to categorize tests and run specific groups of tests.
🌐
Medium
medium.com › @adocquin › mastering-unit-tests-in-python-with-pytest-a-comprehensive-guide-896c8c894304
Mastering Unit Tests in Python with pytest: A Comprehensive Guide
September 26, 2023 - You can finally run your tests again with the pytest command: ... By mocking the database connection, we were able to test the get_users function without having to set up a real database or worry about seeding data and cleaning up after. Here are some best practices to follow when writing tests with pytest: 1. Keep tests simple and focused: Write tests that check one specific aspect of your code to make it easier to understand and maintain them.
🌐
Medium
medium.com › engineered-publicis-sapient › testing-python-code-with-pytest-a-quickstart-guide-36f8da3d402
Testing Python code with Pytest: A QuickStart guide | by Suman Singh | Engineered @ Publicis Sapient | Medium
September 22, 2023 - One of the advantages of Pytest is its automatic test discovery. Pytest will automatically discover and run all files with names that match the pattern test_*.py or *_test.py.
🌐
Leyaa
leyaa.ai › codefly › learn › pytest › part-1 › pytest-running-tests-pytest-command › complexity
Running tests (pytest command) - Framework Patterns & Best Practices | Leyaa.ai
Pytest outputs test results in the console by default (pass/fail with details). Use plugins like pytest-html to generate HTML reports for easy reading. Integrate pytest commands in CI/CD pipelines (GitHub Actions, Jenkins) to run tests automatically on code changes.
🌐
pytest
docs.pytest.org › en › stable › how-to › unittest.html
How to use unittest-based tests with pytest - pytest documentation
It’s meant for leveraging existing unittest-based test suites to use pytest as a test runner and also allow to incrementally adapt the test suite to take full advantage of pytest’s features.