Python tutorials > Testing > pytest > How to run pytest?
How to run pytest?
Pytest is a powerful testing framework for Python. This tutorial covers various ways to execute your pytest tests, along with explanations and examples.
Basic Execution
The simplest way to run pytest is by executing the pytest
command in your terminal. Ensure you are in the root directory of your project or a directory containing your test files. Pytest will automatically discover and run all files matching the test_*.py
or *_test.py
naming convention.
pytest
Running Tests in a Specific Directory
To run tests located in a specific directory, provide the directory path to the pytest
command. For example, pytest tests/
will execute all tests within the 'tests' directory and its subdirectories.
pytest tests/
Running a Specific Test File
You can run tests from a specific file by providing the file path to the pytest
command. In this case, pytest tests/test_example.py
will execute all tests defined in the test_example.py
file.
pytest tests/test_example.py
Running a Specific Test Function
To run a specific test function within a file, use the double colon (::
) to specify the function name. For example, pytest tests/test_example.py::test_function
will only execute the test_function
within the test_example.py
file.
pytest tests/test_example.py::test_function
Running Tests Based on Keywords
The -k
option allows you to run tests based on keywords found in the test name. For example, pytest -k 'login'
will run all tests whose names contain the word 'login'.
pytest -k 'keyword'
Verbose Output
The -v
flag enables verbose output, providing more detailed information about each test that is run, including the test name and its status (passed, failed, skipped, etc.). This is helpful for debugging and understanding the test execution flow.
pytest -v
Stopping After the First Failure
Use the -x
flag to stop the test execution after the first failure. This can be useful when you want to quickly identify the initial issue and fix it before running the entire test suite.
pytest -x
Showing Local Variables in Tracebacks
The --showlocals
option includes local variables in the traceback output when a test fails. This can be invaluable for debugging as it allows you to inspect the values of variables at the point of failure.
pytest --showlocals
Running Tests in Parallel (pytest-xdist)
To speed up test execution, you can use the pytest-xdist
plugin to run tests in parallel. Install it with pip install pytest-xdist
. Then use the -n
flag followed by the number of workers (or auto
to let pytest determine the optimal number). For example, pytest -n auto
will run tests using multiple CPUs.
pytest -n auto
Concepts behind the snippet
Pytest leverages the concept of test discovery to automatically find and execute tests based on naming conventions. It uses assertions (e.g., assert a == b
) to verify expected outcomes. Pytest also provides extensive plugin support, allowing you to extend its functionality with features like parallel execution, code coverage reporting, and more.
Real-Life Use Case Section
Imagine you are developing a web application with user authentication. You would use pytest to write tests to verify that:
Each of these scenarios would be implemented as a separate test function, and pytest would be used to run these tests to ensure the authentication system is functioning correctly.
Best Practices
Interview Tip
Be prepared to discuss your experience with pytest, including:
Also, be ready to provide examples of tests you have written and explain the reasoning behind your testing strategy.pytest-xdist
, pytest-cov
).
When to use them
Use pytest for any Python project that requires automated testing. This includes:
Use pytest from the beginning of your project to catch bugs early and ensure code quality.
Alternatives
While pytest is a popular and powerful testing framework, other alternatives exist:
Pros
Cons
FAQ
-
How do I install pytest?
You can install pytest using pip:
pip install pytest
-
How do I write a simple test with pytest?
Create a file named
test_example.py
and add a function that starts withtest_
. For example:
def test_addition(): assert 1 + 1 == 2
-
How do I see code coverage with pytest?
Install the
pytest-cov
plugin:pip install pytest-cov
. Then run your tests with the--cov
flag:pytest --cov=.