Unit test best practices

While unit testing best practices are somewhat subjective, there are a number of the following standards most engineers agree upon:

  • Test only one code unit at a time
  • Mock out all the external components and dependencies
  • Make each test independent of the others
  • Avoid situations where tests must run in a specific order, or are otherwise interdependent
  • Have one, and only one test to verify each behavior. It is counter-productive to have multiple test failures indicate the same defect.
  • Only make one logical assertion per test. The assertion should, specifically, verify the expected behavior under test, not any additional observations about the code behavior
  • Name unit tests clearly and consistently
  • Avoid unnecessary preconditions ...

Get Serverless computing in Azure with .NET now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.