Code coverage

Measuring what the tests execute: statement, branch, condition and path coverage, reading a coverage rate, and why 100% does not prove the code is right.

  • 27 cards
  • 6 topics
  • Free

The outline, topic by topic

Each topic is weighted the way the exam weighs it.

  1. Concepts
  2. Questions
  3. Sort and link
  4. The numbers
  5. Scenarios
  6. Understand

A few cards from the deck

  • Code coverage: what is it?

    Show the answer

    The share of the source code that running the tests actually went through, measured by a tool and given as a percentage.

    It tells what the tests execute, never what they check: code that runs without any assertion still counts as covered.

  • The only test calls remise(120, True). What coverage does it give?

    ```python
    def remise(montant, membre):
    if montant > 100:
    montant = montant - 10
    if membre:
    montant = montant * 0.9
    return montant
    `

    • 100% of statements, but 2 branches out of 4
    • 100% of statements and 100% of branches
    • 2 statements out of 5 and 2 branches out of 4
    • 100% of statements, but 1 branch out of 4
    Show the answer

    100% of statements, but 2 branches out of 4

    The call goes through both if on the true side, so every statement runs. Both false sides (montant <= 100 and membre false) are never taken: 2 branches out of 4.

  • An if without else has … branches: one where the condition is true, and one where it is false and execution goes straight past the block.

    Show the answer

    An if without else has two branches: one where the condition is true, and one where it is false and execution goes straight past the block.

    The false branch exists even with no code written for it: it is the one a test forgets most often.

  • Statement coverage: what is it?

    Show the answer

    The proportion of the program's statements run at least once by the whole set of tests.

    It is the weakest criterion: an if without else is fully executed when its condition is true, even if the false case was never tried.