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.

Dart (3.13) Module 13

Testing and code quality interview questions

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

7 questions: 3 warm-ups and 4 with model answers · Levels: Fresher to senior

Warm-up questions

Short questions with short answers, each answered in full.

  1. Fresher

    What do test, group and expect each do in package:test?

    Answered in the lesson Unit testing with package:test

  2. Fresher to mid

    Why create test objects in setUp rather than once at the top of a test file?

    Answered in the lesson Unit testing with package:test

  3. Fresher to mid

    Why must a test of asynchronous code wait for its futures?

    Answered in the lesson Testing async code, time and mocks

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

    Your team wants 90 % line coverage on every merge, and the package is at 62 %. How do you get there without tests that only raise the number, and what would you leave out of the count?

    What it tests
    Knowing what line coverage measures and what it cannot, choosing tests by risk and boundaries, and keeping generated or unreachable code from distorting the target.
    First step
    Read the coverage report as a list of untested behaviour, not a score: sort the files with lines that ran 0 times by how much harm a bug there would do, and start with the boundaries and error paths of the most important ones.
    Follow-ups

    From the lesson Unit testing with package:test

    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. Mid to senior

    A one-time code service allows a resend only 30 seconds after the last code, expires each code after 5 minutes and allows 3 attempts. How would you test all three rules in a unit test suite that takes seconds, never minutes, and sends no real SMS?

    What it tests
    Controlling time in tests, choosing the moments at each boundary, making the service take its clock and its SMS gateway from outside, and checking the messages it would send.
    First step
    List the moments that decide each rule: just before and exactly at 30 seconds, just before and exactly at 5 minutes, the third and the fourth attempt. Each moment becomes one test case, and none of them should cost real waiting.
    Follow-ups

    From the lesson Testing async code, time and mocks

    Model answer, Pro.

    In the answer:

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

    A search box sends its query 300 ms after the last keystroke. Its test types a word, waits 350 ms with Future.delayed and expects one query to have been sent. The test fails in about one CI run in twenty and never on laptops. Why, and how do you rewrite it?

    What it tests
    Recognising tests that depend on real timers and machine load, replacing real waiting with controlled time, and covering the behaviour a debounce promises.
    First step
    Ask what the 50 ms margin depends on: the 300 ms timer fires when the machine gets to it, and a loaded CI runner can be later than that. A test whose result depends on the load of the machine needs a different notion of time.
    Follow-ups

    From the lesson Testing async code, time and mocks

    Model answer, Pro.

    In the answer:

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

    A checkout module has 40 tests, each stubbing six mocks with any() and ending with verify(...).called(1). Every test is green, yet a refactor that charged customers the wrong amount reached production. What is wrong with these tests, and how would you change the suite?

    What it tests
    Telling checks of interactions from checks of effects, choosing between real objects, fakes and mocks, and keeping stand-ins honest about the dependencies they replace.
    First step
    Read one test and write down what it would still accept: a charge of any amount, to any order, as long as one call was made. Then ask which effect at the payment boundary a customer would notice, and whether any test checks it.
    Follow-ups

    From the lesson Testing async code, time and mocks

    Model answer, Pro.

    In the answer:

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

All Dart (3.13) interview questions · The Dart (3.13) 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.