You’ve written code and Unit tests, and want to make sure it works. You simply run the pytest
command in your terminal to run them the tests. Boom! some tests fail.
How do you debug it?
To debug, it’s sometimes helpful to run one test, run tests in a specific module or class, or run tests based on a marker.
Tests
The tests are stored in a separate files, typically in a directory named tests. Pytest will automatically discover these tests due to the test_
prefix in the filename and function names.
from operations.core import add_numbers, multiply_numbers
import pytest
def test_add_positive_numbers():
assert add_numbers(1, 2) == 3
@pytest.mark.end_to_end
def test_add_negative_numbers():
assert add_numbers(-1, -2) == -3
@pytest.mark.skip
def test_add_numbers_zero():
assert add_numbers(0, 0) == 0
class TestsUnit:
@pytest.mark.unit
def test_multiply_numbers(self):
assert multiply_numbers(2, 3) == 6
@pytest.mark.unit
def test_multiply_numbers_zero(self):
assert multiply_numbers(0, 0) == 0
To run all the tests from the root directory, you can use the following command:
pytest
The following output will be displayed:
You can add the -v
flag to get more verbose output:
You can enable live console logging using the pytest -s
command too. Our article on Pytest Logging covers how to write Pytest logs to console and files easily and even control the log level.
Run Tests in a Module
To run all tests in a specific file (module), use the following command:
pytest tests/unit/test_functions.py
Run Tests in a Directory
Perhaps you may decide to split your tests by unit
, integration
, end-to-end
, performance
, regression
and so on.
In these cases it’s helpful to run tests within a specific directory, and you can use:
pytest tests/unit
This will find and run all tests in the unit
directory. Similarly you can have directories containing integration
, regression
or performance
tests.
Run Tests by Node IDs
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 following command:
pytest tests/unit/test_functions.py::test_add_negative_numbers
You can find the node ID by running the tests with the -v
flag.
Run Tests of a Specific Class
You can also run all tests in a specific class. To do this, you use the ::
operator followed by the class name.
For example, to run all tests in the UnitTests
class, you can use the following command:
pytest tests/unit/test_functions.py::TestsUnit
Run Tests by Marker Expressions:
Markers are a powerful tool that can be used to control the execution of your tests.
By using markers, you can run specific groups of tests, exclude tests, and prioritize tests. This can help you to write better tests and to get more value from your test suite.
In Pytest, you can assign markers to your test functions using the @pytest.mark
decorator. You can then use these markers to run specific tests.
from operations.core import add_numbers, multiply_numbers
import pytest
def test_add_positive_numbers():
assert add_numbers(1, 2) == 3
@pytest.mark.end_to_end
def test_add_negative_numbers():
assert add_numbers(-1, -2) == -3
@pytest.mark.skip
def test_add_numbers_zero():
assert add_numbers(0, 0) == 0
class TestsUnit:
@pytest.mark.unit
def test_multiply_numbers(self):
assert multiply_numbers(2, 3) == 6
@pytest.mark.unit
def test_multiply_numbers_zero(self):
assert multiply_numbers(0, 0) == 0
Markers can be easily defined like this in your pytest.ini
file:
[pytest]
markers =
unit : unit tests
end_to_end : end to end tests
skip : slow tests
To run all tests with the unit
marker, you can use the following command:
pytest -m unit
If You Want to Skip a Test
There may be situations where you want to skip a particular test.
@pytest.mark.skip
When pytest runs, it will see the @pytest.mark.skip
line and know that it should skip this test.