Software Engineering at Google Chapter #13 - Test Doubles(3 of 3)

  • If you find yourself stpped through the system under test in order to understand why certian functions in the test are stubbed then you should not be using a stub
  • Stubbing leaks implementation details of code into your tests, making them more brittle
  • When stubbing tests become less accurate because they won't mirror real implementations 100%
  • You can't store or change state with stubbing (because they are hard coded answers, nothing more)
  • State testing verifies things were returned correctly or that things are in the correct state
  • Interaction testing validates how a function is called without actually calling the implementation of the function
  • Prefer state testing over interaction testing
  • Interaction test: putItemIntoDB(fooitem) assumes the item is put into the DB when it could fail in dozens of ways
  • State test: doesItemExistInDB(fooitem) checks the DB to see if the item exists, checks the DB's state
  • Interaction testing downside: It utilizes the implementation details (instead of the public API) thus making it inherently brittle
  • Instead of a small scoped interaction test, consider a medium scoped state-based test instead. Prefer state based tests
  • Interaction testing shines when order or number of function calls vary (think underlying caches, stress testing)
  • Use interaction testing when you can't use a real implementation and no fake is available
  • Generally, you should only perform interaction testing on state changing functions (less brittle)
  • A test method should focus on verifying one behavior rather than multiple behaviors in a single test
  • Well-specified interaction tests should only call what is needed and not more
  • Example: if an API requires 3 arguments and you only need to test 1, don't hard code in junk data for the other two.
  • The main reason interaction tests should be avoided is because the test itself must use implementation details and that is never preferred (prefer public API interaction to hide implementation details and simulate user behavior)
< BACK NEXT >
Tweet


   


   

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