QA Interview Hub · Knowledge base · Quizzes · Українська

QA Fundamentals › Quality metrics

How does requirements coverage differ from code coverage? Is 100% needed?

Requirements coverage is the share of requirements that have at least one test; code coverage is the share of code the tests execute. Neither measures the quality of the checks: tests with no asserts can still reach 100% code coverage. So 100% is not a goal: near-100% branch coverage is justified for critical logic, mutation testing shows whether checks react, and the real target is risk coverage.

QA Fundamentals · Quality metrics

Code coverage is 100%, yet there is a bug in production. How, and what coverage is really needed?

The CI report shows a green line: code coverage 100%. A week later a bug is found in production in a function that was "fully covered". The analysis shows the test called the function but checked nothing. This lesson is about two different things called "coverage": requirements coverage and code coverage, what each shows and does not show, and why the right answer to "is 100% needed" depends on risk.

How it works and why. Requirements coverage is the share of requirements or acceptance criteria that have at least one test. It is visible from a traceability matrix (requirement → test) or from links in Jira and Zephyr/Xray. It answers "do we check what was promised at all?". It says nothing about whether the tests passed: that is a separate metric of execution progress.

Code coverage is the share of code that tests execute: statements, decision branches, sometimes functions. It is measured by tools (JaCoCo for Java, Istanbul or c8 for JavaScript, coverage.py for Python), most often during unit and integration tests. It answers "which part of the code do tests touch at all?". The key word is "execute": coverage does not check that a result is correct, only that a line was run. So it reliably shows untested code and unreliably shows that code is tested well. Branch coverage is stricter than statement coverage: a line with a condition can be "covered" even if the false path was never checked.

What each coverage knows and does not know

Requirements

Requirements coverage

Question: is there a test for every promise? Measured with a traceability matrix. Does not see: how deep the tests are, or code that is absent from the requirements.

Code

Code coverage

Question: what do tests execute? Measured by a tool. Does not see: whether the result is verified, or a requirement that is not implemented in the code.

Figure 1. Two axes of measuring: they answer different questions and do not replace each other.

Together they give a diagnostic grid. If at least one axis shows "bad", there is something to discuss; if both show "good", it is still no guarantee, because neither axis measures the quality of assertions.

Requirements and code together

Code executedCode not executedrequirement testedFine if theassertions are realrequirement testedCode with norequirement or checkrequirement untestedCode runs, therequirement is unverifiedrequirement untestedBlind spot:nothing is verified
Figure 2. The highlighted cells are situations where one metric masks a problem the other one sees.

Example: a hundred percent coverage that guarantees nothing. A discount function and a test that "covers" it:

export function discount(total: number, vip: boolean) {
  if (vip && total >= 1000) return total * 0.9;
  return total;
}

// 100% line and branch coverage, nothing is checked
test('discount runs', () => {
  discount(1000, true);
  discount(500, false);
});

The tool will show 100% of lines and both branches, but if someone replaces 0.9 with 0.09 or >= with >, the test stays green. The test ran the code and checked nothing. The fix: tests with expected values at the boundary of the rule:

test('VIP at the 1000 boundary gets 10%', () => {
  expect(discount(1000, true)).toBe(900);
});
test('VIP below the boundary pays in full', () => {
  expect(discount(999.99, true)).toBe(999.99);
});
test('non-VIP gets no discount', () => {
  expect(discount(5000, false)).toBe(5000);
});

Coverage here stays 100%, but now changing >= to > breaks the first test, and changing the factor breaks it too. The quality of checks can be measured by mutation testing (Stryker for JavaScript, PIT for Java): the tool makes small changes in the code (mutants) and checks whether at least one test fails. The share of "killed" mutants shows that tests really react to changes and do not just execute lines. It is more expensive than ordinary coverage, so it is used selectively, on critical logic.

Limits and alternatives. "Is 100% needed" is a wrongly posed question. For critical logic (commission calculation, access rights) branch coverage close to 100% is justified, because the price of a mistake is high. For generated code, configuration and trivial getters, chasing the percentage gives tests nobody will read. Practical rules: a threshold on new changes (for example, not below the threshold for code in a PR) is more convenient than a global percentage, because it does not force rewriting the old code base; and the threshold must not be a personal target (see the lesson about harmful metrics). Requirements have their own limit: 100% requirements coverage means only "each has at least one test", and the depth is decided by test design (equivalence classes, boundaries) and risk. So the real orientation is risk coverage: critical scenarios are covered in depth (several levels, positive and negative), secondary ones enough.

Common mistakes.

  • Treating 100% code coverage as a guarantee of quality: tests without assertions produce it.
  • Making the coverage percentage a team target: tests written for the line appear, while real checks are not added.
  • Counting requirements coverage without depth: one positive case per requirement is marked "covered" though negatives and boundaries are missing.
  • Measuring code coverage with unit tests only and drawing conclusions about e2e, or the other way round: different levels cover different things.
  • Ignoring branch coverage and looking at lines only: the false path of a condition stays unchecked.

Check yourself

In a PR the coverage of new lines is 100%, but the tests are just function calls with no expect. Which metric do you add and what will it show?

Answer

Mutation testing for this module (Stryker or PIT), or at least a check that tests contain assertions. It will show surviving mutants: code changes after which all tests stayed green. It is a direct measure of whether checks react to changes and not only execute lines.

A traceability matrix shows that 100% of requirements have tests. Does this mean the product is tested well? What else do you check?

Answer

No. It only means each requirement has at least one test. I check depth (are there negative scenarios and boundaries by test design technique), whether those tests passed, and whether there is code or behaviour that the requirements do not describe. I add risk coverage: are the most critical scenarios checked at several levels.

In an interview. Junior: Requirements coverage shows what share of requirements have tests. Code coverage shows what share of code is executed by tests. A hundred percent is not a goal for me: a test can run a line and check nothing. I look at risk coverage: critical scenarios are covered in depth. Middle/Senior: Code coverage is good at showing untested code but does not prove the quality of checks, so for critical logic I add mutation testing and look at branch coverage. For new code I set a threshold in the PR instead of a global percentage, and I do not turn it into a personal target. I read requirements coverage together with the depth of test design and with risk.

More articles in this chapter