pytest and Fixtures
pytest needs almost no ceremony. A function whose name starts with test_, and a plain assert. When it fails, pytest shows you the values on both sides, which is why the bare assert is enough.
pytest.raises for the cases where the correct behavior is an exception. pytest.mark.parametrize for running the same test over a table of inputs, which turns eight near-identical tests into one readable one.
Fixtures are arranged situations, reused. A fixture is a function that builds something a test needs, and tests ask for it by naming it as a parameter.
The value is that the arrangement is written once and named, so def test_x(published_post) reads as a sentence. The trap is fixtures that build far more than the test needs, which makes tests slow and hides what they actually depend on. Build the smallest thing that lets the test run.
Django's own test tools. TestCase wraps each test in a transaction against a temporary database and rolls it back, so tests cannot affect each other or your real data. The test client makes requests without a server running.
You met both in the Django course. What that module did not cover is that TestCase also means every test starts from an identical, empty database, which is why tests must create what they need rather than assuming anything exists.
Mark this lesson complete
Signed in, your progress follows you to every device.