language agnostic - What Makes a Good Unit Test? -


i'm sure of writing lots of automated tests , have run common pitfalls when unit testing.

my question follow rules of conduct writing tests in order avoid problems in future? more specific: properties of unit tests or how write tests?

language agnostic suggestions encouraged.

let me begin plugging sources - pragmatic unit testing in java junit (there's version c#-nunit too.. have one.. agnostic part. recommended.)

good tests should a trip (the acronymn isn't sticky enough - have printout of cheatsheet in book had pull out make sure got right..)

  • automatic : invoking of tests checking results pass/fail should automatic
  • thorough: coverage; although bugs tend cluster around regions in code, ensure test key paths , scenarios.. use tools if must know untested regions
  • repeatable: tests should produce same results each time.. every time. tests should not rely on uncontrollable params.
  • independent: important.
    • tests should test 1 thing @ time. multiple assertions okay long testing 1 feature/behavior. when test fails, should pinpoint location of problem.
    • tests should not rely on each other - isolated. no assumptions order of test execution. ensure 'clean slate' before each test using setup/teardown appropriately
  • professional: in long run you'll have test code production (if not more), therefore follow same standard of good-design test code. factored methods-classes intention-revealing names, no duplication, tests names, etc.

  • good tests run fast. test takes on half second run.. needs worked upon. longer test suite takes run.. less run. more changes dev try sneak between runs.. if breaks.. take longer figure out change culprit.

update 2010-08:

  • readable : can considered part of professional - can't stressed enough. acid test find isn't part of team , asking him/her figure out behavior under test within couple of minutes. tests need maintained production code - make easy read if takes more effort. tests should symmetric (follow pattern) , concise (test 1 behavior @ time). use consistent naming convention (e.g. testdox style). avoid cluttering test "incidental details".. become minimalist.

apart these, of others guidelines cut down on low-benefit work: e.g. 'don't test code don't own' (e.g. third-party dlls). don't go testing getters , setters. keep eye on cost-to-benefit ratio or defect probability.


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -