Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Unit Testing Sucks (... and it's our fault)

Jose Armesto
October 25, 2014

Unit Testing Sucks (... and it's our fault)

Tips and tricks to make unit testing more effective.

Jose Armesto

October 25, 2014
Tweet

More Decks by Jose Armesto

Other Decks in Programming

Transcript

  1. 4 rules of Simple Design Passes its tests Minimizes duplication

    Maximizes clarity Has no superfluous parts
  2. Choose good names I don’t want to read the whole

    test case to know what is trying to test. Please. Pretty please. Problems
  3. Avoid logic inside tests Conditionals: You don’t know which path

    the test will take Loops: you could be sharing state between tests If tests have logic, who test the tests? Problems
  4. Be concise Too much information in your tests make them

    hard to read Think about what details could you leave out Assert messages help us highlight behaviour Problems
  5. Assertions Use the name of the test for explaining the

    feature that you are testing. Then use assert messages to clarify what's the example under test and why is that tested.
  6. Expressing the intent "The refactoring technique ‘Extract Method’ is not

    about saving lines of code to make it shorter, but about make the code more readable"
  7. Problems with Set Up Move important code out of sight

    Now you have to remember that the code was there Since code in setUp is generic for all test cases, you lose context on why is that code there Problems
  8. Problems with Providers Tend to make tests more complex The

    most meaningful parts of the test (input & output) are now outside the test Usually end up more complex than you expected Problems
  9. Solutions Use Explicit Set Up ™ for the important stuff

    Don’t mix different test cases in one provider Avoid logic inside providers
  10. Building same objects We depend on the stability of the

    constructor / setter methods. If they change, we are screwed. We are force to pass all the parameters, even if they are not important for the test. Problems
  11. Test Doubles Avoid test doubles for Value Objects Think twice

    before using test doubles for Entities
  12. Query Queries return a value and don’t change the observable

    state of the system Free of side effects
  13. Test Doubles Use stubs to force a query to return

    a specific value so you guide the execution path of the test
  14. Test Doubles Use stubs to force a query to return

    a specific value so you guide the execution path of the test Use mocks to assert that a command to another object has been sent
  15. How much to test Confidence & communication are the goals

    High Code coverage is not a goal: is a side effect
  16. Conclusions Testing makes you go faster. Throw some love to

    your tests. If you find yourself going slower, identify why. TDD makes all of this much easier.