Blog Post

When Should You Merge Tests?

December 4, 2023 Tom Hu

Testing is an important part of software development. Developers spend a considerable amount of time and effort creating tests to ensure their code behaves as expected. It’s often assumed that everyone will want to run these tests, but is that always the case? Let’s explore the question of whether we should merge all of our tests or take a more discerning approach.

Why Do We Merge All Tests?

Writing tests is a time-consuming task. It involves creating test cases that verify various aspects of your code’s behavior. The primary motivations behind merging all tests are:

  • Behavior Verification: Tests are designed to verify that the code behaves correctly and consistently. Merging them ensures that this behavior is confirmed by all team members.
  • Preventing Future Regressions: By having everyone run the tests, you minimize the risk of introducing regressions when making changes to the codebase.

However, let’s take a moment to consider whether merging all tests is always the right approach.

The Case Against Merging in All of Your Tests

While the motivations for merging tests are valid, there are several drawbacks to merging every test without careful consideration:

  • Maintainability: As your codebase grows, so does the number of tests. Merging them all can make your test suite unwieldy and difficult to maintain. Future developers may have a hard time identifying whether a test has been written or whether tests need to be updated.
  • Ambiguity: When tests are merged indiscriminately, it becomes challenging to discern their purpose. No one knows for sure if a particular test is critical or merely an artifact of past development.
  • Cost: End-to-end tests can be expensive to run and maintain. Merging them all can lead to longer build times and increased resource usage.

Ensure that Merged Tests Are Useful

To strike the right balance between thorough testing and maintainability, consider the following strategies:

  • Avoid Testing Default Behavior: Don’t waste time writing tests for default behaviors like setters and getters unless custom logic is involved. Focus on testing the unique and critical aspects of your code.
  • Mutation Testing: Use mutation testing tools to evaluate the effectiveness of your tests. These tools introduce small changes (mutations) into your code and check if your tests can detect them. This helps weed out weak tests.
  • Automated Test Selection: Instead of running all of your tests in a test suite, only run tests that are relevant to a code change. Codecov’s Automated Test Selection (open beta) compares which tests run which lines of code to compile a list of necessary tests to run.

The decision to merge all tests or not should be made with careful consideration. While it’s generally useful to have a comprehensive suite of tests, it’s equally important to ensure the quality of those tests. Avoid the temptation to merge tests indiscriminately, as it can lead to maintainability issues and ambiguity.

Remember that tests should serve the purpose of verifying critical functionality and preventing regressions. By focusing on quality over quantity, you can maintain a robust testing suite that supports your development efforts effectively. Ultimately, the quality of your tests is what truly matters in the quest for reliable software.

Before we redirect you to GitHub...
In order to use Codecov an admin must approve your org.