Skip to the solution
MediumBackend15 min read

How do you write tests in Python with unittest and pytest?

643 views

The problem

Question presented to candidate: "Say you have just written a pricing function and a small shopping-cart class. How would you test them in Python? Show me what a test looks like in unittest and in pytest, how you give each test a clean setup, how you test errors, and how you avoid calling a real external API."

What a strong answer should cover:

  • unittest is the standard library's xUnit framework: subclass unittest.TestCase, name methods test_*, use self.assertEqual / assertRaises, and setUp / tearDown around every test; run with python -m unittest discover.
  • pytest is the third-party de facto standard: plain functions and the plain assert statement (pytest rewrites asserts to print both sides on failure), and it also runs existing unittest tests unchanged.
  • Fixtures: pytest injects them by parameter name, yield splits setup from teardown, built-ins such as tmp_path and monkeypatch; @pytest.mark.parametrize turns one test into a table of cases (unittest's nearest is subTest).
  • Testing failure paths: assertRaises / pytest.raises(..., match=...), and approximate float comparison (assertAlmostEqual, pytest.approx).
  • Test doubles with unittest.mock: Mock, return_value, side_effect, assert_called_once_with, create_autospec, and patching where the name is looked up, not where it is defined.
  • Good test hygiene: each test independent, fast and deterministic; no real network, clock or randomness without control.

Clarifying questions expected:

  • Is the project already on unittest or pytest, and is adding a dev dependency acceptable?
  • Which dependencies are external (HTTP APIs, databases, the clock) and need to be faked?

Code / implementation expected: Yes โ€” a few real tests in either framework, including one error case and one mocked dependency.

  • testing
  • unittest
  • pytest

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

15 min read ยท 11 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

Pythonunittest: TestCase, setUp isolation, assertRaises, subTest and a deliberate failure
Pythonunittest.mock: return_value, side_effect, autospec, mock_open and where to patch
PythonA toy pytest: collect test_* functions, inject fixtures by name, parametrize

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 79 of 139 in Python

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

All Python questions
Up nextMedium

How do you raise and split ExceptionGroups manually?

Continue