Mindfulness

Quality of Tests Cheat Sheet: Key Metrics

Updated · By the Relaxmid editors

Reading time
5 min
Sections
7
Topic
Mindfulness

Quality of tests is measured by how well they catch real problems without wasting time: good tests are reliable, fast, and focused on behavior that matters. Most people get this wrong by chasing coverage percentage alone, which tells you little about whether the tests actually catch bugs. The real cheat sheet is a short list of properties to check on every test you write or review.

What Makes a Test Good

A good test is a signal, not a ritual. It fails when the code is broken and passes when the code works. It runs quickly, so you run it often. It is stable, so it does not fail randomly or for reasons unrelated to the code change.

Here are the core qualities to remember:

  • Reliable: The test fails only when there is a real defect. No flaky failures from timing, network, or order dependence.
  • Fast: A test that takes seconds is run constantly; one that takes minutes is run rarely. Speed is a feature.
  • Focused: Each test checks one behavior. When it fails, you know exactly what broke.
  • Readable: A test is code too. Another person (or you in six months) should understand what it verifies without digging into the implementation.
  • Meaningful: It tests behavior that matters to the user or the system, not internal details that change often.

Rule of thumb: if a test fails and you cannot tell what behavior is broken, the test is not good enough.

Coverage: What It Does and Does Not Tell You

Coverage measures which lines of code were executed during the test run. It is a useful low-level signal: if a line never runs, it is never tested. But high coverage does not mean good tests.

You can have 100% line coverage and still miss the bug that matters, because coverage says nothing about whether the test asserts the right outcome. A test that calls a function and never checks the result still counts as covered.

Use coverage as a floor, not a goal. A reasonable floor is 70-80% for most projects, but the number depends on your domain. What matters more is that the critical paths—the ones that handle money, safety, or core user flows—are tested thoroughly, even if overall coverage is lower.

Instead of chasing a percentage, ask: what are the top five things that could go wrong in this module? Write tests that would catch each one. That is more valuable than adding tests to reach a number.

Test Doubles: Mocks, Stubs, and Fakes

Test doubles replace real dependencies (like databases, APIs, or time) with controlled stand-ins. They make tests fast and isolated, but they also introduce risk: if the double does not behave like the real thing, the test passes while production fails.

Use doubles for things that are slow, non-deterministic, or hard to set up. Do not use them for everything. A test that mocks the database and the HTTP client and the file system may be testing only the mock, not your code.

Prefer fakes—simple in-memory implementations that behave like the real thing—over mocks that verify specific calls. Fakes are more realistic and less brittle. Save mocks for when you need to assert that a particular interaction happened (e.g., an email was sent).

Rule of thumb: if your test has more than two or three mocks, step back. The test may be too coupled to implementation details.

Readable Test Names and Structure

A test name should read like a sentence that states the expected behavior. Avoid generic names like test1 or testFunction. Instead, use a pattern like:

should_return_discounted_price_when_customer_is_member

Or in a behavior-driven style:

given_member_customer_when_calculate_total_then_apply_discount

This makes failures easier to diagnose because the name tells you what scenario broke. Structure each test in three parts: arrange (set up), act (call the code), assert (check the result). Use blank lines or comments to separate them if the language does not enforce it.

Keep each test short. If you need more than 10-15 lines, consider splitting it. Long tests are hard to read and often test multiple things at once.

Common Test Smells to Avoid

Certain patterns signal that a test is weak, even if it passes. Learn to spot them:

  • Asserting implementation details: Checking that a method was called with specific arguments when the behavior is what matters. This makes tests break on refactoring that changes internals but not behavior.
  • Testing multiple things in one test: If one assertion fails, you do not know which behavior broke. Split into separate tests.
  • Sleeping or waiting for time: Tests that wait for a fixed time are slow and flaky. Use fake timers or inject a controllable clock.
  • Ignoring exceptions: A test that catches an exception and passes anyway is hiding bugs. Let the test fail loudly.
  • Testing the framework: Asserting that a library works as documented is wasted effort. Trust the library; test your code.

If you see these, refactor the test. A test that is hard to read or slow to run will be skipped or deleted eventually.

How to Review a Test

When you review a test, ask these questions in order:

  1. What behavior is this test verifying? Can you state it in one sentence?
  2. If this test fails, what is the most likely cause? Would that cause be a real bug?
  3. Is the test fast and deterministic? Does it depend on external services or time?
  4. Does it assert something meaningful, or just that the code ran without throwing?
  5. Would this test catch a regression if someone changed the code tomorrow?

If you cannot answer the first question, the test is not good. If the answer to the last is no, the test is not earning its keep. A test that does not catch regressions is dead weight—it adds maintenance cost without protection.

Remember: the goal of testing is not to have tests. It is to have confidence that the code works. Every test you write should increase that confidence. If it does not, remove it or rewrite it.

Related

General information for everyday wellbeing, not medical advice. If something affects your health, sleep or mood for more than a short while, talk to a qualified professional. Read more

Spotted an error? Tell us and we will correct it (corrections policy).