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:
- This program has 12 lines of code.
- If we ran a test suite that only exercised the add function, then our test coverage would be 8/12, or 67%.
- However, if we ran a test suite that exercised both the add and subtract functions, then our test coverage would be 12/12, or 100%.
Test Design Coverage in Software Testing
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.