1. Junior

    What is the difference between unit tests and integration tests?

    Unit tests exercise a small unit (a function or class) in isolation from external dependencies and run fast, while integration tests exercise several real parts working together, such as code with a database or HTTP.

    Read the full explanation
  2. Junior

    What is the role of fixtures in pytest?

    A fixture prepares a dependency or data a test needs, which the test requests by name as a parameter, and it controls its own lifetime (scope) and cleanup after yield.

    Read the full explanation
  3. Junior

    How do you test many cases without repeating code?

    Use pytest.mark.parametrize to run the same test over several inputs and expected results, so each case appears as an independent test in the report.

    Read the full explanation
  4. Mid-level

    How do you use mock and patch correctly?

    Apply patch to the name where the code under test uses it, not where it is defined, and use autospec so the mock fails if it is called with the wrong signature.

    Read the full explanation
  5. Mid-level

    How do you test asynchronous (async) code?

    Use IsolatedAsyncioTestCase in unittest, or a plugin such as pytest-asyncio or anyio in pytest, so the test runs inside an event loop and the coroutine is actually awaited.

    Read the full explanation
  6. Junior

    How do you verify that a function raises the correct exception?

    Use pytest.raises (or assertRaises in unittest) as a context manager around only the line expected to fail, and check the type and the important fields or part of the message through match.

    Read the full explanation
  7. Mid-level

    How do you keep time and randomness from making tests flaky?

    Pass the clock and the source of randomness as replaceable dependencies instead of calling datetime.now() or random directly inside domain logic, then pin them in the test.

    Read the full explanation
  8. Mid-level

    How do you test HTTP code without relying on a real network?

    Test the client through a fake transport or fake server, simulating failure cases (timeouts, 5xx, invalid payloads), then add a limited number of contract or integration tests against the real service.

    Read the full explanation
  9. Senior

    Does a high coverage percentage mean the code is well tested?

    No; coverage measures the lines or branches that ran during the tests, not the quality of the assertions or the coverage of risky cases. It is a useful signal but not a goal in itself.

    Read the full explanation
  10. Senior

    What is a contract test and when is it useful?

    A contract test verifies that two parties (such as a service and its client, or an adapter and its interface) agree on the shape and semantics of the data, without running the whole system end to end.

    Read the full explanation