We write automated tests to...
- Check the handling of known corner cases
- Check that code still compiles
- Ensure old bugs are still fixed
- Prove the absence of bugs
Click to show the answer
1 & 3: Automated tests require developers to provide concrete inputs and sequences of operations to run, thus they can test known corner cases and known bugs. However, they cannot prove the absence of bugs in general, and are not needed to check that code still compiles.
Ideally, we would test...
- All public functions
- All private functions
- All modules
- The whole program end-to-end
Click to show the answer
1 & 2 & 4: These are all good targets to test, though in practice software engineers must prioritize what to test given their time budget. One should not test private implementation details, since the tests would break even if the code remained correct!
A regression test must...
- Be automated
- Fail before fixing the bug
- Be deleted after fixing the bug
- Be written after the program is released
Click to show the answer
2: A regression test must fail before its corresponding bug is fixed, otherwise it is not actually testing that bug. However, it does not have to be automated if doing so is too difficult, especially in complex end-to-end scenarios, should be written after a bug is found regardless of release date, and definitely must not be deleted.
Test-Driven Development...
- Makes it easy to change requirements during development
- Gives developers instant feedback on their implementation
- Involves writing one test per public method
- Helps ensure tests do not "over-fit" the implementation
Click to show the answer
2 & 4: Using TDD, developers must think about the behavior of their code early, rather than the specific implementation, and obtain feedback as soon as they have written said implementation thanks to the tests. However, requirements are harder to change since tests have already been written, and there is no specific number of tests per method since one test might cover multiple methods.
Which of these dependencies should one inject?
- A user interface
- An HTTP client
- A regular expression parser
- A file reader
Click to show the answer
1 & 2 & 4: Anything that involves communicating with the environment, such as users, the network, or disks. A regular expression parser is a pure function, thus there is no point in injecting it and using a "fake" version in tests as this fake would have to mimic the real one in every way.