Skip to content

Instantly share code, notes, and snippets.

@seanwash
Last active December 28, 2022 17:13
Show Gist options
  • Save seanwash/1b391565a9968547c0d801c9b958cff5 to your computer and use it in GitHub Desktop.
Save seanwash/1b391565a9968547c0d801c9b958cff5 to your computer and use it in GitHub Desktop.
Playbook / Writing Tests

Playbook / Writing Tests

Remember, it's all about getting a good return on your investment where "return" is "confidence" and "investment" is "time." - Kent C. Dodds

People love debating what percentage of which type of tests to write, but it's a distraction. Nearly zero teams write expressive tests that establish clear boundaries, run quickly & reliably, and only fail for useful reasons. Focus on that instead. - Justin Searls

Write tests. Not too many. Mostly integration. - Guillermo Rauch

Notes

Quick Pass

  • Structure tests with Arrange, Act, Assert.
  • Write the easy tests first to hedge against testing fatigue.
  • Test the happy path first and then layer in specific scenario tests.
  • Don’t mock what you don’t own.
  • Don’t test implementation details.
  • Name the test as if you were describing the scenario to a non-programmer.
  • When I read tests for business logic, I want the intent of the test to be obvious on first glimpse. – Look out for tests that are much slower than similar tests in the suite.

In Depth

  • Methodology
    • Stick to the pyramid shape to come up with a healthy, fast and maintainable test suite: Write lots of small and fast unit tests. Write some more coarse-grained tests and very few high-level tests that test your application from end to end.
    • Static > Unit > Integration > E2E
  • What to test
    • When writing a test, try to break the method's code down and identify specific pieces that can be asserted against. For example,
      • The returned output of a method based on the input.
      • Whether a user was authenticated or logged out.
      • Whether any exceptions were thrown.
      • Whether any rows in the database changed.
      • Whether any jobs were added to the queue.
      • Whether any events were dispatched.
      • Whether any mail or notifications were sent (or queued).
      • Whether any external HTTP calls were made.
  • Naming Tests
  • Tools
    • Fakes
    • Mocks
    • Spies
    • Stubs

Resources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment