What makes a good unit test?

What is a unit test?

  • trying to isolate the individual parts of the software -> units
  • concerned with the functional correctness of each unit
  • typically written and run by software engineers

What to test in a unit test?

  • functional correctness and completeness
  • error handling
  • checking parameters
  • correctness of return data

What defines a good unit test?

A TRIP

  • Automatic
  • Thorough
  • Repeatable
  • Independent
  • Professional

automatic

  • tests should be called automatic
  • results of the test PASS/FAIL should be available automically

thorough

  • test all key paths and scenarios
  • there are coverage tools that help you with this

repeatable

  • tests should produce the same outcome every time
  • setup and teardown

independent & isolated

  • test only one thing at a time
  • when failing it should tell you the exact problem
  • they should not rely on other tests (setup & teardown)
  • clean up your mess

professional

  • as in the production code mind good coding standards:
    • test-names
    • readable

AAA

  • Arrange
  • Act
  • Assert

What does not fit in any acronym? 

  • keep your tests as small as possible
  • prefer mock objects over setting up to much of the environment
  • do not test someone else code
  • use long descriptive method name

When to write unit tests?

  • try writing your tests before the code
  • think of your test as a specification for your code

all or none

  • write tests for all of your code and projects
  • all team members must write tests

Either we all live in a decent world, or nobody does

- George Orwell

further reading

  • http://stackoverflow.com/questions/61400/what-makes-a-good-unit-test
  • http://www.slideshare.net/suhasreddy1/unit-testing-24134336
Made with Slides.com