Running pytest
in the terminal, executes ALL the tests which not only takes a long time, but is also inefficient.
So how do you filter tests in Pytest? How can you tell Pytest to run only the tests you’re interested in? Something like a regular expression?
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.
Understanding the Pytest -k Option
Imagine you have a large test suite with hundreds or thousands of tests, each covering a different aspect of the application - security, login, auth, checkout, etc.
How can you group or filter tests, running only essential tests?
That’s where Pytest -k
option comes into play.
Pytest -k
is a command-line option that enables you to filter tests based on their names.
With Pytest -k
, you can filter tests by specifying a substring, or a Python expression.
Specifying a Substring:
Pytest -k
allows you to execute groups of tests that contain a specific substring in their names.
pytest -k http
Using Python Expressions:
Pytest -k
also supports using Python expressions such as and
, or
, and not
. This allows you to exclude specific tests from the execution list.
pytest -k "not slow and not performance"
Why Use Pytest -k Options?
-
Selective Testing:
-
Continuous Integration:
-
Debugging:
-
Integration Testing:
-
Group Tests:
-
Using Python Expressions:
pytest -v -k 'cart and not core'
This command will run all the tests containing the keyword cart
but exclude those containing the keyword core
.
Excluding Tests with Markers
You can easily exclude unnecessary tests by applying markers like @pytest.mark.skip
or @pytest.mark.xfail
.
Here, the @pytest.mark.skip
decoration skips the test, while @pytest.mark.xfail
marks a test as expected to fail (perhaps due to a pending feature or bug fix).
Best Practices when Using Pytest -K Options
Let’s explore some best practices and hints when using Pytest -k
options.
-
Using Meaningful and Descriptive Test Names:
-
Using Config Files: If you use filters often, it’s recommended to use Config files. This approach allows you to easily run tests selectively without having to use
-k
options every time.
For example, you can add the following configuration to your pytest.ini
file to run only the tests containing the keyword core
:
[pytest]
adopts =
-k core
-
Use Markers
-
Group Tests by Functionality: