Successfully reported this slideshow.
Your SlideShare is downloading. ×

Unit Testing Done Right

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Upcoming SlideShare
Best practices unit testing
Best practices unit testing
Loading in …3
×

Check these out next

1 of 39 Ad
Advertisement

More Related Content

Viewers also liked (15)

Advertisement

Similar to Unit Testing Done Right (20)

Recently uploaded (20)

Advertisement

Unit Testing Done Right

  1. 1. Unit testing done right
  2. 2. whoami • Brian Fenton • Bigcommerce • @brianfenton • Mostly PHP and APIs
  3. 3. What is a unit test? • Small • Fast • Tests a single unit <- Here there be dragons
  4. 4. Unit?
  5. 5. It depends…
  6. 6. “A single piece of a class’s public-facing functionality”
  7. 7. History lesson
  8. 8. Goals of unit testing over time • Find bugs • Find regressions • Improve design
  9. 9. Philosophies • Traditional testing • TDD • XP • BDD
  10. 10. Behavior vs. Methods
  11. 11. https://twitter.com/sarahmei/status/633895726993440768
  12. 12. How did we get here?
  13. 13. Code coverage
  14. 14. "When a measure becomes a target, it ceases to be a good measure.” - Goodhart’s Law
  15. 15. Different kinds of coverage • Line • Function/Method • Class/Trait
  16. 16. Gresham’s Law /ht @noahsussman
  17. 17. Coverage tricks • if (false) this_function_call_is_covered(); • // @codeCoverageIgnore • multiple checks on the same line
  18. 18. Line coverage can only prove code wasn’t executed
  19. 19. Opcodes
  20. 20. Example for  ($i  =  0;  $i  >  10;  $i++)  {          if  ($i  %  2  ===  0)  {                  echo  "Even:  ";          }  else  {                  echo  "Odd:  ";          }          echo  $i,  "n";   }
  21. 21. More kinds of coverage • Opcode • Branch • Path
  22. 22. Testing your privates • Don’t! • Extract functionality to a collaborator or don’t test it
  23. 23. Don’t test your implementation
  24. 24. Flaky tests
  25. 25. Flaky tests • Randomness • Time/environment • Network/services
  26. 26. Randomness • Can the randomness come from outside? • Do you need to know the exact value? • Can you just verify “randomness”?
  27. 27. Namespace trick namespace  MyNamespace;   function  random()   {          return  mt_rand();   }  
  28. 28. Namespace trick namespace  MyNamespace  {          function  random()  {  return  7;  }   }   namespace  MyTests;   …   function  testRandomNumberGenerator()   {          $this-­‐>assertSame(7,  random());   }
  29. 29. Environment • Time • Shell vars/ini settings/config values • Filesystem
  30. 30. Network/Services
  31. 31. Good unit tests • Single reason to fail • Clearly named • testGuestCanRegisterWithValidInformation • testProcessData
  32. 32. Good unit tests • Easy to read • Clean • Arrange, Act, Assert
  33. 33. Good unit tests • Fast • No side effects
  34. 34. Listening to your tests
  35. 35. Test smells • Too many mocks • Lots of setup required • Can’t test w/o database/network/etc… • Can’t inject mocks where needed
  36. 36. Test smells • Mocks returning mocks • Flaky results • Unrelated code changes breaking tests • Testing the implementation
  37. 37. Questions? https://joind.in/14721

×