Python intermediate: automated tests and code quality

Automated tests with assert, unittest, and pytest, plus ruff and flake8. Why you test, official docs, and 20 exercises with solutions.

Bureau de contrôle qualité avec liste de vérification, pied à coulisse et tableau de bord, illustrant les tests de code Python

AI-generated illustration

Some links on this page are affiliate links. The button opens Amazon so you can check today’s price.

Writing automated tests means capturing the expected behavior as code you can run again whenever you want (locally, or in CI before a merge). When you change a function, the tests report regressions: either the behavior changed by mistake, or you need to update the test so it matches a new business rule. Without that net, a “small fix” can break another module before you notice, sometimes only after release.

People usually separate unit tests (one isolated unit, often a function or a class with stand-in dependencies), integration tests (several components together), and end-to-end tests (the real application). This lesson focuses on the basics of unit tests and on static analysis (linters). The two complement each other: a linter finds suspicious code without running it; tests check the logic with examples.

1. assert: a quick guard

assert doubler(2) == 4

An assert condition raises AssertionError when it is false. Careful: with python -O (optimize), Python strips assert statements. Do not rely on them for security checks in production. For a serious test suite, use unittest or pytest.

2. unittest: a “test class” style

The unittest module follows xUnit conventions: a class inherits from TestCase, methods named test_* hold the scenarios, and methods such as assertEqual produce structured failure messages.

import unittest

class TestMath(unittest.TestCase):
    def test_carre(self):
        self.assertEqual(3 * 3, 9)

if __name__ == "__main__":
    unittest.main()

Discovery: unittest finds tests by name and by inheritance. Running the file executes unittest.main().

3. pytest: plain assertions and discovery

pytest is widely used in current projects: you can write test_* functions with plain assert, with no required subclass. Automatic discovery walks test_*.py files or a tests/ directory.

File test_foo.py:

def doubler(x):
    return x * 2

def test_doubler():
    assert doubler(3) == 6

Run: pytest (install it in the venv: pip install pytest).

Best practices: an explicit test_* name (test_doubler_positif); group files in tests/ so application code and test code stay apart. pytest fixtures prepare reusable data — look at those after the basics.

4. Static quality (linters)

Tools such as ruff (fast, many rules) or flake8 (PEP 8 style, imports, complexity) read the source text and report inconsistencies, unused imports, or obvious mistakes without running the whole program. That is not a substitute for tests: “clean” code can still be functionally wrong. Combine linters and tests for a workable balance of style and correctness.

External resources

Exercises (20)

The exercises alternate assert, unittest, and pytest sketches. Solutions are hidden by default.

Easy level

Exercise 1 — Write assert 1 + 1 == 2. Easy

Show the solution
assert 1 + 1 == 2

Exercise 2 — A unittest test: self.assertTrue(3 > 2). Easy

Show the solution
import unittest

class T(unittest.TestCase): def test_ok(self): self.assertTrue(3 > 2)

Exercise 3 — self.assertEqual(a, b) with a=5, b=5. Easy

Show the solution
import unittest
unittest.TestCase().assertEqual(5, 5)

Exercise 4 — A function pair(n), and a pytest test assert pair(2) is True. Easy

Show the solution
def pair(n):
    return n % 2 == 0

def test_pair(): assert pair(2) is True

Exercise 5 — self.assertRaises(ValueError, int, "abc") in unittest. Easy

Show the solution
import unittest
unittest.TestCase().assertRaises(ValueError, int, "abc")

Exercise 6 — Name two advantages of automated tests (short sentences). Easy

Show the solution
# Catch regressions after a refactor.
# Document the expected behavior with examples you can run.

Exercise 7 — Why not rely only on print to check a result? Easy

Show the solution
# You cannot replay it automatically; it is easy to forget before a commit.

Intermediate level

Exercise 8 — pytest.raises to check ZeroDivisionError on 1/0. Intermediate

Show the solution
import pytest

def test_zero(): with pytest.raises(ZeroDivisionError): 1 / 0

Exercise 9 — A simple pytest fixture: a function donnee() that returns [1,2,3] via @pytest.fixture. Intermediate

Show the solution
import pytest

@pytest.fixture def donnee(): return [1, 2, 3]

def test_len(donnee): assert len(donnee) == 3

Exercise 10 — setUp on unittest.TestCase to initialize a resource. Intermediate

Show the solution
import unittest

class T(unittest.TestCase): def setUp(self): self.x = 10

def test_x(self):
    self.assertEqual(self.x, 10)</code></pre>

Exercise 11 — Best practice: a test should check one main thing. Restate that in one sentence. Intermediate

Show the solution
# A failing test should make clear which assumption is false.

Exercise 12 — A parametrized test with pytest.mark.parametrize, inputs (0,0) and (1,2), for f(a,b)=a+b. Intermediate

Show the solution
import pytest

@pytest.mark.parametrize(“a,b,r”, [(0, 0, 0), (1, 2, 3)]) def test_add(a, b, r): assert a + b == r

Exercise 13 — The difference between a unit test and an integration test (a short answer). Intermediate

Show the solution
# Unit: one isolated function; integration: several modules or real I/O.

Exercise 14 — Why can ruff check . save time before a commit? Intermediate

Show the solution
# It catches typos, unused imports, and style issues before a full run.

Hard level

Exercise 15 — mock.patch, the idea only: what is unittest.mock for when the code under test calls the network? Hard

Show the solution
# Temporarily replace a dependency (for example requests.get) with a fake response.

Exercise 16 — The TDD strategy in three step words (a comment). Hard

Show the solution
# Red: write a test that fails. Green: the smallest code that passes. Refactor: clean up.

Exercise 17 — Coverage with pytest-cov: what does --cov measure? Hard

Show the solution
# The share of lines executed during the tests (an indicator, not proof of quality).

Exercise 18 — approx for floats: pytest.approx(0.1 + 0.2) versus 0.3. Hard

Show the solution
import pytest
assert 0.1 + 0.2 == pytest.approx(0.3)

Exercise 19 — Avoid nondeterministic tests: give one example of a bad practice. Hard

Show the solution
# assert time.time() == ... or depending on file order without sorting.

Exercise 20 — A before-PR checklist: list four items (tests, lint, docs, format). Hard

Show the solution
Tests green locally; ruff/flake8 OK; changelog or docstring updated; black formatting if the team uses it.

Next in the series

Regular expressions: search and check patterns in text.

Amazon (affiliate)

Python software-testing books:

Check today’s price on Amazon

Further reading

Was this page helpful?

Have a question about this guide or need advice? Ask us here

Torn between two models or stuck on a build? Drop us a line and we will get back to you by email.