Skip to content

Software test coverage

Measure which statements and branches a test run exercises, and see how full branch coverage can miss a fee threshold bug.

Software test coverage measures how much of the code in a defined scope a test run executes. A coverage report can flag a branch that none of a trading system’s tests reach, such as the one that rejects an oversized order. The tests’ assertions determine whether reaching that branch would expose a wrong result.

Two outcomes and one boundary

Suppose a fictional backtest charges USD 1 per order below 100 shares and USD 2 for an order of 100 shares or more. Order size is a positive integer. The implementation checks size >= 100 and returns the fee for the selected tier.
A unit test at 50 shares exercises the false outcome and gets USD 1. Add a test at 150 shares and it exercises the true outcome and gets USD 2. For this one decision, the count moves from 1 of 2 (50%) to 2 of 2 (100%), even if both calls discard their results.
A check that both fees are positive would pass even if the fee amounts were swapped. Exact fee assertions at 50 and 150 would catch that swap but would still pass if a refactor changed >= to >. At exactly 100 shares, the changed code would charge USD 1 when the schedule calls for USD 2. Checking 99 and 100 against the written schedule tests both sides of that boundary. Python’s assertion documentation describes how exact comparisons turn a wrong result into a test failure.
For a fictional fee rule with a 100-share threshold, a test at 50 shares visits one of two outcomes. Tests at 50 and 150 visit both but miss a strict-comparison bug. Exact fee checks at 99 and 100 also visit both and catch the bug.
For a fictional fee rule with a 100-share threshold, a test at 50 shares visits one of two outcomes. Tests at 50 and 150 visit both but miss a strict-comparison bug. Exact fee checks at 99 and 100 also visit both and catch the bug.
The two pairs visit the same number of outcomes in the intended implementation. Only the pair that tests the threshold detects the changed comparison.

Read the denominator before comparing reports

Statement coverage counts executable statements reached. Branch coverage tracks alternatives in control flow. A conditional with no explicit else can execute every statement while leaving its false outcome unvisited. Coverage.py tracks branches as transitions between source lines. In branch mode its headline percentage counts both executable lines and branch destinations, so the worked example’s 50% applies to the fee decision alone. See the Coverage.py branch measurement guide.
Check which source files the tool measures and reports, and whether any lines or branches are excluded. Removing a module from the report changes the scope of its percentage. To follow up on missing code, inspect the exact branch and write a test that checks what the specification says should happen there. See the Coverage.py source selection guide.
The fee helper can have full branch coverage while the backtest forgets to deduct the fee from cash. That connection is a separate behaviour to check with an integration test.
The SelfMadeQuant lesson Testing and Debugging applies ordinary, boundary and invalid-input tests to a return calculation. The preview is public and the full lesson requires membership.

Sources

Last updated
  • software testing
  • branch coverage
  • quant software
← Back to the glossaryBack to top ↑