Python
Debugging & Testing
31 flashcards · answers and spaced-repetition review in the KnowCard app
What's the core difference in how doctest vs unittest define their test cases, and one practical trade-off?
You ship an API and use this to reject bad input:
```
def withdraw(amount):
assert amount > 0, "must be positive"
...
```
Why is this a real security bug in production?
Running a script with python -O myapp.py, what happens to every assert statement in the code, and what does __debug__ equal?
What is the difference between
```
assert x == y
```
and
```
self.assertEqual(x, y)
```
in a failing unittest?
A unittest class has this method. After running it with `python -m unittest`, the report says "Ran 0 tests". Why?
```
class MyTest(unittest.TestCase):
def checkCalc(self):
self.assertEqual(fac(5), 120)
```
In unittest, when do setUp and tearDown run relative to your test methods?
You want to assert that calling `fac(-1)` raises `ValueError`. What does this do, and why is it the wrong way?
```
self.assertRaises(ValueError, fac(-1))
```
What is the cleanest way to test that a block of code raises an exception when the code isn't already wrapped in a function?
A doctest's expected output is written as:
```
>>> make_set()
{2, 1, 3}
```
Why might this test fail intermittently even though the function is correct?
Your function prints a blank line in the middle of its output. How must you represent that blank line in the doctest's expected output, and why?
What does the # doctest: +SKIP comment do to a test case, and when is it useful?
Both `doctest.testmod()` and `unittest.main()` are guarded like this. What breaks if you forget the guard?
```
if __name__ == "__main__":
unittest.main()
```
When a doctest passes, what does the test run print by default, and how do you make successes visible?
In pdb, you've stopped at a line that calls process(data). You type n (next), but a colleague says "I needed to go *into* process". Which command should they have used, and what does n do instead?
What does breakpoint() do when you put it in your code, and why is it preferred over import pdb; pdb.set_trace()?
What does a postmortem debugger let you inspect, and how does that differ from setting a breakpoint?
Your team uses pytest. A colleague says "I'll convert all our plain assert x == y into self.assertEqual for better failure messages." Why is that unnecessary in pytest?
Why does test isolation matter, and what's a concrete bug from tests that aren't isolated?
You want to profile an existing script without adding import cProfile and wrapping calls inside its source. How?
How does the trace module's coverage analysis differ from what a profiler like cProfile gives you?
A trace coverage report shows for i in range(100): executed 101 times, though the loop body ran only 100. Why the extra one?
In a cProfile report a function shows a small tottime but a large cumtime. What does that tell you, and which sort finds the real bottleneck?
In a debugger you've stepped into a function but now want to finish it and stop back in the caller. Which command does that, and how is the run command different?
In a doctest, how do you write the expected output for a call that raises an exception, without pasting the exact full traceback?
A doctest example returns a 2500-digit number (or a memory address that changes each run). Which doctest flag lets ... stand in for the variable part, and how do you switch a flag on or off?
Instead of self.assertTrue(x in items) or self.assertTrue(y is None), unittest offers assertIn, assertIsNone, assertIsInstance, assertGreater, and so on. Why bother picking the specialized one?
You want a unittest to confirm not just that ValueError is raised but that its message matches a pattern. Which method, and what tests warnings?
You benchmark two algorithms with timeit and call t.repeat(100, 10000). Why report min(...) of the returned list rather than the average?
In timeit.Timer(stmt, setup, globals), what form do stmt and setup take, how many times does .timeit() run by default, and does the return value include setup time?
You use timeit to measure code that allocates lots of short-lived objects, and the numbers look suspiciously fast. What does timeit do to garbage collection during a measurement?
Python's standard library ships both profile and cProfile. What's the practical difference, and which should you normally reach for?
Start learning today
Free to start — download the app or use it in your browser.
