Unit Test - Best Practices

A unit test must:

  • Written before the production code.
  • Target the single(not multiple) success or failure criteria of the feature.
  • Take less time in execution.
  • Follow the AAA (Arrange, Act, Assert) pattern
  • Run in isolation.
  • Able to reproducible on all environment.
  • Not dependent on other tests.
  • Match the acceptance criteria of the user story.

Which tests are not unit test

  • Not every test you can write qualifies as a Unit test.
  • Test which test infrastructure concerns. Infrastructure concerns include database or network resources or file from disk.
  • A test which throws many requests for a service that qualifies as a Smoke test and not Unit test.
  • A test where multiple components of the system.

Unit test structure

  • Arrange: Arrange the object which are required. This include creation of object(s) and setting the values into them.
  • Act: Perform required operation on object(s).
  • Assert: Verify which is expected.

How to write unit test: Unit testing with .NET

Please share this

Leave a Reply