Your country

Tools that support it use your country for local currency, number formats, units and paper size. Your choice is saved only in this browser.

Type a name or a two-letter code. Use the up and down arrow keys to move through the countries, Enter to choose one and Escape to close.

Django Module 12

Testing Django applications interview questions

Interview questions on testing Django applications: short warm-ups, then what each harder question tests, the first step of a good answer and its follow-ups.

7 questions: 2 warm-ups and 5 with model answers · Levels: Fresher to staff

Warm-up questions

Short questions with short answers, each answered in full.

  1. Fresher

    What is the difference between setUp() and setUpTestData()?

    Answered in the lesson Testing with Django's test framework

  2. Fresher

    What does @pytest.mark.django_db do?

    Answered in the lesson pytest-django, factories and coverage

Questions with model answers

What each question tests, the first step of a good answer and the follow-ups to expect.

  1. Mid to senior

    A suite of 1,400 Django tests takes 11 minutes in CI. Most tests create a resident with a password and twenty notices in setUp(), then sign in with client.login(). Make it fast without making the tests depend on each other. What do you change first, and how do you show it helped?

    What it tests
    Measuring where test time goes, setUpTestData() and the isolation it keeps, the cost of password hashing, force_login(), and running tests in parallel safely.
    First step
    Measure before changing anything: run manage.py test with --timing and --durations 20, and profile one slow class. Then count what each test pays before its first assertion: here a password hash in create_user(), another in client.login(), and 21 rows, 1,400 times over.
    Follow-ups

    From the lesson Testing with Django's test framework

    Model answer, Pro. Model answers open with any pass; prices are in the notice on this page.

    Opening the model answers with your pass…

    Model answers open with any MySmartCoPilot pass.

    • Pro pass · 1 week₹79$2.99
    • 30 days₹179$5.99
    • 1 year₹1,699$45

    One payment; a pass never renews. The price shown is the total you pay: no fees or charges are added at checkout.

    It also opens every other in-depth part in Learn and every Pro tool. Sales are final, except where the law gives you a right to withdraw, for example in the EU and the UK (refund policy).

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  2. Fresher to senior

    A view saves a booking inside atomic() and sends the confirmation email with transaction.on_commit(). Its TestCase test expected one email and failed, so a teammate changed the view to send the email directly, and the test passed. What is wrong with that, and how should the test be written?

    What it tests
    How TestCase's transactions interact with on_commit(), why side effects wait for a commit, and the tools that emulate a commit in tests.
    First step
    Explain why the test failed before touching the code: TestCase runs every test inside a transaction that is rolled back and never committed, so on_commit() callbacks never run there. The view was right and the test was wrong, so the change has to be undone.
    Follow-ups

    From the lesson Testing with Django's test framework

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  3. Mid to senior

    Every billing test calls BillFactory(), whose SubFactory chain writes a society, an owner, a flat and the bill. The suite has 3,000 such tests and takes 9 minutes. Make it faster without making the tests harder to read.

    What it tests
    Seeing the hidden cost of sub-factories, choosing between build and create, sharing rows deliberately, and measuring before and after.
    First step
    Count what one test writes before guessing: capture the queries of a single BillFactory() call. Then sort the 3,000 tests by what they really need: saved rows, or only objects to read. Tests of pure logic need no database at all.
    Follow-ups

    From the lesson pytest-django, factories and coverage

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  4. Mid to staff

    The billing app has 96 % coverage, and still a late-fee bug reached production: bills a few days late were charged double. How did the tests miss it, how do you use coverage to find such gaps, and what rule would you set for the team?

    What it tests
    The difference between code that ran and code that was checked, reading branch reports, assertions and mutation testing, and coverage rules that do not reward empty tests.
    First step
    Look at the report's missing column for the file of the bug, with branch coverage on, before discussing the percentage: a decision that only ever went one way shows there as a partial branch, even when every line ran.
    Follow-ups

    From the lesson pytest-django, factories and coverage

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  5. Mid to senior

    The suite passes when run in one process. With pytest -n 8 a few tests fail, different ones on each run, and they pass when run alone. Find the cause and fix it.

    What it tests
    How parallel workers split a suite, the kinds of shared state that break isolation, and tools that make order dependence visible.
    First step
    Treat it as order dependence, not as a bug of pytest-xdist: run the suite in one process with a random or reversed order and see whether the same tests fail. Then list what tests share outside the database, which each worker gets on its own.
    Follow-ups

    From the lesson pytest-django, factories and coverage

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes

All Django interview questions · The Django track

Quick answers and tool search

Type to search tools or to get a quick answer, for example 18% of 2500. Use the up and down arrow keys to move through the results, Enter to choose, and Escape to close.