Unit Test or Not to Unit Test

That is the question!

or is it?

Testing is for Wimps

Who needs Unit Tests?

Don't leave testing till the end

Testing isn't just for QA

  • Think about how to Verify your code works before writing the first line
    • How will you know it works?
    • Writing code that is Unit Testable is structured differently
      • Must minimize dependencies.
      • Smaller functions
      • Eliminate/minimize side effects
    • Context setup/teardown needs to be thought about
    • Use dependency injection

Tips for Unit tests

  • Small functions that do one thing are easier to test.
  • Things to assert
    • Assert that expected inputs give expected outputs
    • Assert that unexpected inputs (null values for example) behave as expected
    • Test edge conditions behave as expected
      • Empty arrays
      • Null values
      • 0 or '' (empty quotes)
      • large arrays
      • duplicate values
    • Verify that results are consistent between calls
  • Watch out for functions that have side effects or do not return the same result for the same input.

Attitude

  • Take ownership of ensuring your code works
  • Take ownership of what happens when your code is misused.
  • Code that works by accident
  • Don't rely on "Running it and hoping for the Best" (R&HB)

To Test or Not to Test

By Jason Dent

To Test or Not to Test

  • 894