When is a Test not a Unit-test?

后端 未结 8 1339
感情败类
感情败类 2020-11-30 19:48

I am looking for rules like:

A test is not a unit-test if:

  • it communicates with a database
  • it cannot run in parallel with other tests
8条回答
  •  日久生厌
    2020-11-30 20:29

    A test is not an Unit Test when:

    • it tests more than one thing at once (i.e. it tests how two things work together) - then it is an integration test

    Checklist for good unit tests:

    • they are automated
    • they are repeatable
    • they are easy to implement
    • they remain for future use, once written
    • they can be run by anyone
    • they can be run by the push of a button
    • they run quickly

    Some more best practices (in no particular order of importance):

    • tests should be separated from integration tests (which are slower), so that they can be run fast as frequently as possible
    • they should not comprise too much logic (preferably, no control structures)
    • every test should test only one thing (thus, they should contain only one assert)
    • the expected values used in assertions should be hard-coded and not computed at test run-time
    • external dependencies (filesystem, time, memory etc.) should be replaced by stubs
    • test should recreate the initial state at test shutdown
    • in assertions, it is better to use a "contains..." policy, rather than "is strictly equal..." policy (i.e. we expect certain values in a collection, certain characters in a string etc.)

    This is part of the knowledge I have extracted from Roy Osherove's book - The Art of Unit Testing

提交回复
热议问题