Test Coverage

Learn the importance of test coverage and usage of pytest-cov plugin.

Introduction to test coverage

Test coverage plays a crucial role in ensuring the effectiveness and reliability of software testing. It measures the extent to which the source code of a software application is tested by test cases. High test coverage indicates that a significant portion of the code has been exercised by tests, reducing the risk of undetected bugs and improving overall software quality.

Test coverage measures the coverage of test cases in terms of code execution and identification of untested areas. It helps identify gaps in testing and ensures that all code paths and logic are tested. It is typically measured as a percentage, indicating the proportion of code executed by tests.

Test coverage helps ensure that the code is thoroughly tested, reducing the risk of undetected bugs. It provides confidence in our codebase and allows for easier maintenance and refactoring. Test coverage metrics serve as a quantitative measure of code quality and can help in identifying areas for improvement.

Types of test coverage metrics

There are a lot of different types of test coverage metrics. We will look into some popular metrics used in development.

Line coverage

Line coverage measures the percentage of lines of code that are executed by tests. It determines whether each line of code in the tested codebase has been executed at least once by the test suite. This metric helps identify lines that have not been covered and might indicate potential areas where bugs could exist.

def add_numbers(a, b):
result = a + b
return result
def subtract_numbers(a, b):
result = a - b
return result
Line coverage

If the test suite only executes the add_numbers function, the line coverage would be 50% because only half of the lines in the code snippet have been executed by tests.

Branch coverage

Branch coverage measures the percentage of decision points (branches) that are executed by tests. It determines whether each possible branch of a decision point has been taken at least once during test execution. Decision points include conditions, loops, and other control flow structures.

def is_even(number):
if number % 2 == 0:
return True
else:
return False
Branch coverage

To achieve 100% branch coverage for this code, the test suite needs to execute both branches of the if-else statement, ensuring that the code is tested with both even and odd numbers.

Statement coverage

...