Featurecore
Testing with pytest
Also called pytest, plain assert, assertion rewriting, pytest fixtures, conftest.py, pytest.mark.parametrize, pytest.raises, tmp_path, monkeypatch, skipif, xfail.
pytest runs plain functions named test_* as tests. A bare assert reports the values that failed, fixtures arrive as arguments, parametrize repeats a test over a table, and marks skip or xfail tests.
- Language
- Python
Synopsis
def total(prices, discount=0):
return round(sum(prices) * (1 - discount), 2)
def test_total():
assert total([1.5, 2.5]) == 4.0
def test_discount():
prices = [10, 20]
assert total(prices, discount=0.1) == 28.0
import pytest
code = pytest.main(["-qq", "-rA", "-p", "no:cacheprovider", "test_cart.py"])
print("exit code:", int(code))
.F [100%]
=================================== FAILURES ===================================
________________________________ test_discount _________________________________
def test_discount():
prices = [10, 20]
> assert total(prices, discount=0.1) == 28.0
E assert 27.0 == 28.0
E + where 27.0 = total([10, 20], discount=0.1)
test_cart.py:11: AssertionError
==================================== PASSES ====================================
=========================== short test summary info ============================
PASSED test_cart.py::test_total
FAILED test_cart.py::test_discount - assert 27.0 == 28.0
exit code: 1
A test is a plain function whose name starts with test, and a check is a plain assert. The second test is wrong on purpose. pytest shows the failing line, the two values it compared, and the call that produced 27.0. The usual command is pytest in the project directory; this page calls pytest.main from a script so that the report can be shown, and the pytest page explains the options.
Semantics
r1. "pytest will run all files of the form test_*.py or *_test.py in the current directory and its subdirectories."1 In those files it collects the functions whose names start with test. There is no base class to subclass and no runner call at the end of the file.
r2. "pytest allows you to use the standard Python assert for verifying expectations and values in Python tests."2 When an assert fails, pytest shows "the values of the most common subexpressions including calls, attributes, comparisons, and binary and unary operators".2 It gets them by changing the test code: "Reporting details about a failing assertion is achieved by rewriting assert statements before they are run."2 "pytest only rewrites test modules directly discovered by its test collection process, so asserts in supporting modules which are not themselves test modules will not be rewritten."2
r3. pytest.raises(ExceptionType) used as a context manager checks that the block raises. Its match argument is a regular expression that is "matched with the re.search() function" against the string of the exception.2 "Note that pytest.raises will match the exception type or any subclasses (like the standard except statement)."2
r4. A fixture is a function marked @pytest.fixture. "At a basic level, test functions request fixtures they require by declaring them as arguments."3 pytest "looks at the parameters in that test function’s signature, and then searches for fixtures that have the same names as those parameters."3 A fixture can request other fixtures the same way. tmp_path is a built-in one: "Before the test runs, pytest creates a unique-per-test-invocation temporary directory".1
r5. A fixture that uses yield instead of return cleans up after the test: "Any teardown code for that fixture is placed after the yield."3 A fixture in a conftest.py file is shared, "so that tests from multiple test modules in the directory can access the fixture function".3 The test files do not import it.
r6. "The builtin pytest.mark.parametrize decorator enables parametrization of arguments for a test function."4 The decorator takes the argument names and a list of value tuples, and pytest runs the test once per tuple. Each run has its own id in brackets, made from the values or from the ids argument.
r7. Marks attach metadata to tests. The builtin ones include "skip - always skip a test function", "skipif - skip a test function if a certain condition is met" and "xfail - produce an “expected failure” outcome if a certain condition is met".5 Custom marks "are commonly used to select tests on the command-line with the -m option."5 "Marks can only be applied to tests, having no effect on fixtures."5
r8. The monkeypatch fixture sets or deletes attributes, dictionary items and environment variables for one test. "All modifications will be undone after the requesting test function or fixture has finished."6
Examples
Fixtures, a conftest.py and parametrize
import pytest
@pytest.fixture
def notes(tmp_path):
path = tmp_path / "notes.txt"
path.write_text("first\n", encoding="utf-8")
yield path
print("cleanup", path.name)
import pytest
def test_read(notes):
assert notes.read_text(encoding="utf-8") == "first\n"
def test_append(notes):
with notes.open("a", encoding="utf-8") as f:
f.write("second\n")
assert notes.read_text(encoding="utf-8").splitlines() == ["first", "second"]
@pytest.mark.parametrize(
"text, words",
[("a b", 2), ("", 0), (" x ", 1)],
ids=["two", "empty", "padded"],
)
def test_word_count(text, words):
assert len(text.split()) == words
def test_bad_int():
with pytest.raises(ValueError, match="invalid literal"):
int("ten")
import pytest
code = pytest.main(["-qq", "-rA", "-p", "no:cacheprovider", "test_notes.py"])
print("exit code:", int(code))
...... [100%]
==================================== PASSES ====================================
--------------------------- Captured stdout teardown ---------------------------
cleanup notes.txt
--------------------------- Captured stdout teardown ---------------------------
cleanup notes.txt
=========================== short test summary info ============================
PASSED test_notes.py::test_read
PASSED test_notes.py::test_append
PASSED test_notes.py::test_word_count[two]
PASSED test_notes.py::test_word_count[empty]
PASSED test_notes.py::test_word_count[padded]
PASSED test_notes.py::test_bad_int
exit code: 0
test_read and test_append each get a new file in their own tmp_path, so the line that test_append adds is not seen by test_read. The code after yield runs once per test, which is why "cleanup notes.txt" appears twice. test_word_count runs three times, with the ids from ids.
Marks and monkeypatch
import os
import sys
import pytest
def greeting():
return "hello " + os.environ.get("APP_USER", "guest")
def test_default(monkeypatch):
monkeypatch.delenv("APP_USER", raising=False)
assert greeting() == "hello guest"
def test_from_env(monkeypatch):
monkeypatch.setenv("APP_USER", "ada")
assert greeting() == "hello ada"
@pytest.mark.skipif(sys.platform != "win32", reason="Windows only")
def test_windows():
assert os.sep == "\\"
@pytest.mark.xfail(reason="float rounding")
def test_round():
assert round(2.675, 2) == 2.68
import pytest
code = pytest.main(["-qq", "-rA", "-p", "no:cacheprovider", "test_marks.py"])
print("exit code:", int(code))
..sx [100%]
==================================== PASSES ====================================
=========================== short test summary info ============================
PASSED test_marks.py::test_default
PASSED test_marks.py::test_from_env
SKIPPED [1] test_marks.py:21: Windows only
XFAIL test_marks.py::test_round - float rounding
exit code: 0
s is a skipped test and x an expected failure; neither makes the run fail. The two monkeypatch tests change APP_USER in opposite ways, and neither change leaks into the other test or into the rest of the process.
Pitfalls
- Asserts in a helper module are not rewritten (r2). A failed
assertinhelpers.pygives a bareAssertionError. Keep the asserts in the test files, or have the helper return a value that the test asserts on. pytest.raises(Exception)passes for any subclass (r3), so it also passes when the code fails for an unrelated reason, such as aTypeErrorfrom a typo. Name the narrowest exception, and addmatch.- Every parameter of a test function is a request for a fixture of that name (r4). A misspelled parameter asks for a fixture that does not exist, and an extra parameter that was meant to have a default asks pytest for a fixture too.
- Code before
yieldin a fixture is setup. The pytest docs warn about a fixture that does many setup steps: "if any of those steps in the setup raise an exception, none of the teardown code will run."3 Split such setup into several small fixtures, one per resource. - Changing
os.environor a module attribute directly in a test leaks into the tests that run after it. Usemonkeypatch, which undoes the change (r8).
Comparison
Go's testing package also uses plain functions found by name: go test "automates execution of any function of the form func TestXxx(*testing.T)".7 Go has no assert statement and no rewriting; a test compares values itself and reports with t.Errorf, as in "Within these functions, use T.Error, T.Fail or related methods to signal failure."7 Go has no fixtures injected by name; setup is ordinary code at the top of the test. Table-driven tests play the part of parametrize: "The T.Run and B.Run methods allow defining subtests and sub-benchmarks, without having to define separate functions for each. This enables uses like table-driven benchmarks and creating hierarchical tests."7
-
How to write and report assertions in tests, pytest documentation. ↩︎ ↩︎ ↩︎ ↩︎ ↩︎ ↩︎
-
How to parametrize fixtures and test functions, pytest documentation. ↩︎
-
How to mark test functions with attributes, pytest documentation. ↩︎ ↩︎ ↩︎
-
How to monkeypatch/mock modules and environments, pytest documentation. ↩︎
References
- Get Started, pytest documentation, Run multiple tests; Request a unique temporary directory for functional tests
- How to write and report assertions in tests, pytest documentation, Asserting with the assert statement; Assertions about expected exceptions; Assertion introspection details
- How to use fixtures, pytest documentation, Requesting fixtures; conftest.py; Teardown/Cleanup, yield fixtures
- How to parametrize fixtures and test functions, pytest documentation, pytest.mark.parametrize
- How to mark test functions with attributes, pytest documentation, builtin markers; Registering marks
- How to monkeypatch/mock modules and environments, pytest documentation, helper methods
- Package testing, Go standard library, package overview; Subtests and Sub-benchmarks
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W3 standard |
|
Tag 51AWVE Edit this page History Report an error Markdown