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.
Warm-up questions
Short questions with short answers, each answered in full.
-
Fresher
What do test, group and expect each do in package:test?
-
Fresher to mid
Why create test objects in setUp rather than once at the top of a test file?
-
Fresher to mid
Why must a test of asynchronous code wait for its futures?
Questions with model answers
What each question tests, the first step of a good answer and the follow-ups to expect.
-
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
-
- Why can 100 % line coverage still miss bugs?
- How do you keep generated code from distorting the number?
- How would you check that the tests would actually catch a bug?
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.
Passes can’t be bought right now, so the model answers can’t be opened at the moment. If you have a pass, enter your licence key.
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
-
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
-
- Why must the service read the time through a clock it is given?
- How do you test the exact moment a code expires?
- What should the stand-in for the SMS gateway record?
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
-
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
-
- Why does the test fail only in CI?
- Would waiting 1,000 ms instead of 350 ms fix it?
- Which cases should a test of a debounce cover?
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
-
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
-
- Why did the wrong amount get through every test?
- Which of the six mocks would you replace, and with what?
- How do you keep a fake honest about the real gateway?
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