Software Engineering at Google Chapter #12 - Unit Testing (1 of 3)

  • Size is the amount of resources consumed by a test and scope is the amount of code tested
  • The most important purpose of a test is to increase engineer's productivity
  • Unit tests generally test a single class or method
  • Small tests (unit tests) are fast, deterministic, easy to write, serve as docs/examples, are easy to fix, and so on
  • A flakey test fails non-deterministicly without any changes to the code
  • A brittle test is a test that fails due to unrelated changes to code
  • The ideal test is unchanging - it never needs to be updated when the code is updated
  • When refactoring, the purpose of the existing tests is to ensure the system's behavior does not change
  • When a new feature is added, tests should not have to change (new tests will be added, existing should not need to change)
  • Bug fixes are similar to new features (add new tests, don't change existing)
  • When you need to change behavior-based tests due to a change it means you are breaking a behaviorial contract with other systems or components that interact with it. Important for low level components (libraries).
  • Prefer to write tests from the POV of the user (test the public API as a user would, not the backend/guts/hidden parts aka implementation details)
  • If you want to test implementation details (eg: hidden API functionality), make it separate from the "user point of view" tests as what the user needs and what the maintainers need are separate
  • If something exists only to support something else (a "support library") don't test it individually, test them together
  • Test state, not interactions
  • Interaction tests care about how the test got to the end result
  • State tests check the current state to ensure it's as expected
  • State vs interaction example:
    • Interaction is a test that puts data into a DB and it could fail in multiple ways (is brittle)
    • State is a test that checks to see if the data is in the DB, a much more solid test



Thank you for your time and attention.
Apply what you've learned here.
Enjoy it all.