Test Design Coverage in Software Testing (original) (raw)

Last Updated : 2 Dec, 2022

Test coverage is the degree to which a test or set of tests exercises a particular program or system. The more code that is covered by a test, the more confidence developers have that the code is free of bugs. Measuring test coverage can be difficult because it is often hard to determine what percentage of the code is actually being executed by a test. In general, however, the more code that is covered by a test, the better.

There are many different types of test coverage, but in general, test coverage is a measure of how much of the code or functionality of a system is being tested by a particular set of tests. For example,if a set of tests only covers 50% of the code, then it has 50% coverage. There are many different ways to measure test coverage, and the level of coverage that is considered acceptable varies from organization to organization. In some cases, 100% test coverage may be required, while in others, 80% may be considered adequate.

One way to think of test coverage is as a spectrum, with lower levels of coverage being less effective and higher levels being more effective. However, it is important to remember that no single level of coverage is right for all situations, and the level of coverage that is appropriate will depend on the specific system under test and the risks involved.

Purpose of Test Coverage

The purpose of test coverage is to ensure that all code paths in a program are executed at least once during testing. This helps to ensure that all bugs and potential problems are found and fixed before the program is released. Test coverage can be measured in a number of ways, but the most common metric is line coverage, which simply measures the percentage of lines of code that are executed during testing.

Test Coverage Metrics

There are many different ways to measure test coverage, but some common metrics include:

Critical Areas and Methods for Effective Testing

Areas that may need more testing

Methods for Effective Testing

Types of Test Coverage

  1. Statement Coverage: A test suite is said to achieve statement coverage if every statement in the software has been executed at least once during testing. This type of test design coverage ensures that every statement in the software has been executed at least once. This is the most basic form of test design coverage and is often used as a starting point for more comprehensive testing.
  2. Decision Coverage: A test suite is said to achieve decision coverage if every decision (e.g. if-then-else) in the software has been taken both true and false during testing. This type of test design coverage ensures that every decision (i.e. every 'if' or 'case') in the software has been executed with all possible outcomes. This is a more comprehensive form of testing than statement coverage and can help to uncover errors that may not be obvious from simply executing every statement.
  3. Condition Coverage: A test suite is said to achieve condition coverage if every condition in the software has been evaluated as both true and false during testing. This type of test design coverage ensures that all the conditions within a decision have been executed with all possible outcomes. This is a more comprehensive form of testing than decision coverage and can help to uncover errors that may not be obvious from simply executing every decision.
  4. Multi-Condition Coverage: A test suite is said to achieve multi-condition coverage if all combinations of conditions in the software have been evaluated during testing. This type of test design coverage extends condition coverage by testing all the conditions within a decision with all possible combinations of values. This is a more comprehensive form of testing than condition coverage and can help to uncover errors that may not be obvious from simply executing every condition.
  5. Modified Condition: A test suite is said to achieve modified conditions if all modified conditions and decisions in the software have been evaluated during testing. This type of test design coverage extends decision coverage by testing all the conditions within a decision with all possible outcomes, taking into account the effect of modifying the conditions. This is a more comprehensive form of testing than decision coverage and can help to uncover errors that may not be obvious from simply executing every decision.
  6. Data Flow Coverage: A test suite is said to achieve data flow coverage if all executable paths through the software have been executed during testing. This type of test design coverage ensures that all the possible paths through the software have been executed. This is a more comprehensive form of testing than all the other types of coverage and can help to uncover errors that may not be obvious from simply executing the software.

What does Test Coverage do?

How Test Coverage Can Be Accomplished?

Test Coverage vs Code Coverage

Below are the differences between Test coverage and Code coverage:

Parameters Test coverage Code coverage
Definition Test coverage is a measure of how much testing has been done on a particular piece of code. Code coverage is a measure of how much code is covered by tests.
Measures It only measures testing. It measures both testing and code.
Various other levels It can be measured at various levels, including statement coverage, branch coverage, and path coverage. It can be measured at various levels, including line coverage, function coverage, and block coverage.
Percentage of tests It is often expressed as a percentage of the total number of tests that have been run. It is often expressed as a percentage of the total number of lines of code that have been covered by tests.
Increased It can be increased by running more tests. It can be increased by writing more tests.
Decreased It can be decreased by deleting tests. It can be decreased by deleting code.
Find Untested Code It can be used to find untested code. It can be used to find both untested and untestable code.
Assess the Quality of Testing It can be used to assess the quality of testing. It can be used to assess the quality of both testing and code.

The formula for Test Coverage

There are many factors to consider when calculating test coverage, but ultimately it boils down to this formula:

Coverage = (Number of Tests) / (Number of Lines of Code)

If there are 100 tests and 1000 lines of code, then the test coverage will be 10%. There are a few things to keep in mind when using this formula.

Example of Test Coverage

There are many different ways to calculate test coverage, but one common method is to simply count the number of lines of code that are executed when the test suite is run. This can be done by manually running the tests and counting the number of lines that are executed, or by using a tool that automatically instruments the code and tracks which lines are executed.

To see an example of how test coverage can be calculated, consider the following simple program:

int add(int a, int b) {

return a + b; }

int subtract(int a, int b) {

return a - b; }

int main() {

int x = 5, y = 3;

int sum = add(x, y);

int difference = subtract(x, y);

return 0; }

Solution:

How should a Good Test Coverage Technique be Implemented?

To implement a good test coverage technique, it is important to select the right coverage metric for the project at hand. The coverage metric should be able to identify areas of the code that are not being adequately tested so that these areas can be given more attention. Furthermore, the coverage metric should be easy to understand and use so that it can be effectively used by all members of the team.

Benefits of Test Coverage

Limitations of Test Coverage