The FIRST Principle in Testing

Introduction

The FIRST principle is a widely adopted set of conventions for writing tests, particularly unit tests. The FIRST acronym breaks down as follows:

  • Fast
  • Isolated/Independent
  • Repeatable
  • Self-validating
  • Timely

Details of the FIRST Principle

  1. Fast: Tests must be fast to run. Unit tests are generally run frequently, so the faster they are, the better. If a test is slow, it is likely testing more than it should, or has dependencies it should not have.

  2. Isolated/Independent: Each test must be independent of the others. This means that the order in which the tests run should not affect their results. Tests should not share state or dependencies.

  3. Repeatable: A test must produce the same result every time it is run, regardless of the environment in which it runs. This means that the test should not depend on environment-specific elements, such as particular files or a database configuration.

  4. Self-validating: A test must be able to validate itself automatically. This means it should not require manual checking to determine whether the test passed or failed. The test should end with an assertion that succeeds if the test passes and fails if the test fails.

  5. Timely: Tests must be written in a timely manner. In the context of Test-Driven Development (TDD), this means that tests should be written before the code they test.

Conclusion

The FIRST principle is a roadmap for writing quality tests. It helps us write tests that are robust, reliable, and useful for verifying that our code works correctly. It is essential that we apply these principles when writing our tests to ensure that we build software of the highest possible quality.