The Wayback Machine - https://web.archive.org/web/20240822105526/https://www.geeksforgeeks.org/code-coverage-testing-in-software-testing/
Open In App

Code Coverage Testing in Software Testing

Last Updated : 19 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Every Software Developer follows the Software Development Life Cycle (SDLC) for the development of any software application. Testing is one of the important phases that is performed to check whether the developed software application is fulfilling the requirements or not.

Different types of software testing are there which are performed based on various metrics/testing parameters.

What is Code Coverage?

Code Coverage is a metric used in Software Testing that quantifies the extent to which the source code of a program is tested. It measures the percentage of code executed by the test suite, helping developers identify untested parts of an application.

As we know at last of the development each client wants a quality software product as well and the developer team is also responsible for delivering a quality software product to the customer/client. Where this quality refers to the product’s performance, functionalities, behavior, correctness, reliability, effectiveness, security, and maintainability. Where Code Coverage metric helps in determining the performance and quality aspects of any software. The formula to calculate code coverage is

Code Coverage = (Number of lines of code executed)/(Total Number of lines of code in a system component) * 100

Code Coverage Criteria

To perform code coverage analysis various criteria are taken into consideration. These are the major methods/criteria which are considered.

1. Statement Coverage/Block coverage

The number of statements that have been successfully executed in the program source code.

Statement Coverage = (Number of statements executed)/(Total Number of statements)*100.

2. Decision Coverage/Branch Coverage

The number of decision control structures that have been successfully executed in the program source code.

Decision Coverage = (Number of decision/branch outcomes exercised)/(Total number of decision outcomes in the source code)*100.

3. Function coverage

The number of functions that are called and executed at least once in the source code.

Function Coverage = (Number of functions called)/(Total number of function)*100.

4. Condition Coverage/Expression Coverage

The number of Boolean condition/expression statements executed in the conditional statement.

Condition Coverage =(Number of executed operands)/(Total Number of Operands)*100.

Types of code coverage

Understanding the different types of code coverage helps in ensuring comprehensive testing of your software. Here are the main types:

Types-of-code-coverage

Types of code coverage

1. Statement Coverage

  • Definition: Measures whether each line of code is executed.
  • Purpose: Ensures that all code statements are tested at least once.
  • Example: If a function has ten lines of code, statement coverage ensures each line is executed during testing.

2. Branch Coverage (Decision Coverage)

  • Definition: Measures whether each possible branch (true/false) from each decision point is executed.
  • Purpose: Ensures that all branches of control structures (like if-else statements) are tested.
  • Example: If a program has an if-else statement, both the if and the else branches should be tested.

3. Function Coverage

  • Definition: Measures whether each function or subroutine in the code is called.
  • Purpose: Ensures that all functions in the code are invoked during testing.
  • Example: In a class with multiple methods, function coverage ensures each method is called at least once.

4. Condition Coverage (Predicate Coverage)

  • Definition: Measures whether each Boolean sub-expression is evaluated to both true and false.
  • Purpose: Ensures that each condition in a decision has been tested for both true and false outcomes.
  • Example: In a compound condition like if (A && B), both A and B should be tested for true and false values.

5. Path Coverage

  • Definition: Measures whether all possible paths through the code are executed.
  • Purpose: Ensures that all possible execution paths are tested.
  • Example: For a function with multiple nested loops and conditionals, path coverage would ensure all possible execution routes are tested.

6. Line Coverage

  • Definition: Measures the number of executed lines divided by the total number of lines.
  • Purpose: Provides a straightforward metric of how much of the codebase is executed.
  • Example: If a file has 100 lines and 80 lines are executed during testing, line coverage would be 80%.

7. Loop Coverage

  • Definition: Measures whether loops are executed and how many times.
  • Purpose: Ensures that loops are tested with zero, one, and multiple iterations.
  • Example: For a loop that processes items in a list, loop coverage ensures the loop is tested with an empty list, a single-item list, and a multiple-item list.

Each type of code coverage focuses on different aspects of the code, helping to ensure thorough testing and improving overall software quality.

Tools For Code Coverage

Below are a few important code coverage tools

  • JaCoCo
  • Cobertura
  • Emma
  • Clover
  • Codecov
  • Coveralls
  • Istanbul
  • SonarQube
  • NCover
  • BullseyeCoverage
  • Visual Studio Code Coverage
  • dotCover
  • OpenCover
  • gcov
  • Testwell CTC++

Advantages of Using Code Coverage

  • It helps in determining the performance and quality aspects of any software.
  • It helps in evaluating quantitative measures of code coverage.
  • It helps in the easy maintenance of the code base.
  • It helps in accessing the quality of the test suite and analyzing how comprehensively a software is verified.
  • It helps in the exposure of bad, dead, and unused code.
  • It helps in creating extra test cases to increase coverage.
  • It helps in developing the software product faster by increasing its productivity and efficiency.
  • It helps in measuring the efficiency of test implementation.
  • It helps in finding new test cases that are uncovered.

Disadvantages of Using Code Coverage

  • Sometimes it fails to cover code completely and correctly.
  • It can not guarantee that all possible values of a feature are tested with the help of code coverage.
  • It fails to ensure how perfectly the code has been covered.
  • For some test files, additional mock data is required to execute the test cases.
  • Sometimes it is difficult to cover the test cases handling functions.
  • Lot of test cases are required to increase the test coverages for complex code.
  • A developer must have strong knowledge of writing unit test cases to cover all possible scenarios in the developed code.

Conclusion

Code coverage is a crucial metric in software testing, providing insights into the extent to which the source code is tested. It helps developers identify untested parts of an application, ensuring the performance, functionality, and reliability of the software. Various types of code coverage, including statement, branch, function, condition, path, line, and loop coverage, offer comprehensive testing approaches to enhance software quality. By using code coverage tools like JaCoCo, Cobertura, and SonarQube, teams can efficiently maintain their code base, improve test suite quality, and ensure thorough verification of the software.

Frequently Asked Questions on Code Coverage Testing

How do Code Coverage tools work?

Code coverage tools instrument the source code to track and report which parts are executed during testing, helping identify untested areas for improved test coverage.

What is the Criteria for selecting a code coverage tool?

When selecting a code coverage tool, consider language support, integration capabilities, reporting features, ease of use, performance impact, open source vs. commercial options, community and support, cost, provided metrics, and scalability

Which code coverage tool should you choose?

JaCoCo



Previous Article
Next Article

Similar Reads

Test Coverage vs Code Coverage
Test Coverage and Code coverage are the most mainstream philosophies for estimating the viability of the code. In spite of the fact that these terms are at times utilized reciprocally since their fundamental standards are the equivalent. In any case, they are not as comparative as you may might suspect. Ordinarily, I have seen the testing group and
6 min read
What is Branch Coverage in Unit Testing?
Unit Testing is the process of writing the test cases for each developed code file. This testing is carried out by developers. Once the coding part is done, the developers will write the test cases to cover each scenario in the developed file. While running the test suites or test files, we can see there are four parts of coverages. They are Branch
8 min read
What is Code Driven Testing in Software Testing?
Prerequisite - TDD (Test Driven Testing) Code Driven Testing is a software development approach in which it uses testing frameworks that allows the execution of unit tests to determine whether various sections of the code are acting accordingly as expected under various conditions. That is, in Code Driven Testing test cases are developed to specify
2 min read
Principles of software testing - Software Testing
Software testing is an important aspect of software development, ensuring that applications function correctly and meet user expectations. In this article, we will go into the principles of software testing, exploring key concepts and methodologies to enhance product quality. From test planning to execution and analysis, understanding these princip
10 min read
PEN Testing in Software Testing
Pen testing, a series of activities taken out in order to identify the various potential vulnerabilities present in the system which any attack can use to exploit the organization. It enables the organization to modify its security strategies and plans after knowing the currently present vulnerabilities and improper system configurations. This pape
3 min read
Load Testing Basics, Tools & Practices in Software Testing
Introduction: Load testing is a type of performance testing that simulates a real-world load on a system or application to see how it performs under stress. The goal of load testing is to identify bottlenecks and identify the maximum number of users or transactions the system can handle. Some popular tools for load testing include Apache JMeter, Lo
6 min read
Basis Path Testing in Software Testing
Prerequisite - Path Testing Basis Path Testing is a white-box testing technique based on the control structure of a program or a module. Using this structure, a control flow graph is prepared and the various possible paths present in the graph are executed as a part of testing. Therefore, by definition, Basis path testing is a technique of selectin
5 min read
Decision Table Based Testing in Software Testing
What is a Decision Table : Decision tables are used in various engineering fields to represent complex logical relationships. This testing is a very effective tool in testing the software and its requirements management. The output may be dependent on many input conditions and decision tables give a tabular view of various combinations of input con
3 min read
Difference between Error Seeding and Mutation Testing in Software Testing
1. Error Seeding :Error seeding can be defined as a process of adding errors to the program code that can be used for evaluating the number of remaining errors after the system software test part. The process works by adding the errors to the program code that one can try to find & estimate the number of real errors in the code base with the he
3 min read
Response Testing in Software Testing
Software applications are developed to provide some specific service to the customers. When an end-user uses a software product/application between the software and the user, a request-response interaction takes place. This request-response interaction is one method of communication between users and systems or multiple systems in a network. When o
8 min read
Random Testing in Software Testing
Random testing is software testing in which the system is tested with the help of generating random and independent inputs and test cases. Random testing is also named monkey testing. It is a black box assessment outline technique in which the tests are being chosen randomly and the results are being compared by some software identification to chec
4 min read
Overview of Conversion Testing in Software Testing
Conversion Testing :Every software development process follows the Software Development Life Cycle (SDLC) for developing and delivering a good quality software product. In the testing phase of software development, different types of software testing are performed to check different check parameters or test cases. Where in each software data is an
6 min read
Data Integrity Testing in Software Testing
Every software development process follows the Software Development Life Cycle (SDLC) for the development and delivery of a good quality software product. In the testing phase of software development, different types of software testing are performed to check different check parameters or test cases. Where in each software data is an important part
5 min read
Incremental Testing in Software Testing
Incremental Testing :Like development, testing is also a phase of SDLC (Software Development Life Cycle). Different tests are performed at different stages of the development cycle. Incremental testing is one of the testing approaches that is commonly used in the software field during the testing phase of integration testing which is performed afte
3 min read
Internationalization Testing in Software Testing
Prerequisite: Software Testing Software testing is an important part of the software development life cycle. There are different types of software testing are performed during the development of a software product/service. Software testing ensures that our developed software product/service is bug-free and delivers fulfilling the desired requiremen
4 min read
Types of Regression Testing in Software Testing
Regression Testing :Regression testing is done to ensure that enhancements or defect fixes made to the software work properly and do not affect the existing functionality. It is generally done during the maintenance phase of SDLC. Regression Testing is the process of testing the modified parts of the code and the parts that might get affected due t
4 min read
Post Deployment Testing in Software Testing
Post-deployment testing is a type of testing in which the software is tested after it is deployed to production. This testing may help us find those problems that were not detected before its deployment in the production and despite all the planning and testing carried out before the final deployment, obtaining user opinion is important for the imp
3 min read
Challenges in Manual Testing - Software Testing
Manual testing is a type of software testing technique that is used to document tests, produce test guides based on data queries, provide temporary structures to help run tests, and measure the results of the tests. Manual testing is considered to be costly and time-consuming. In manual testing, a tester carries out tests on the software by followi
4 min read
Factors controlling the Performance Testing - Software Testing
Prerequisite: Performance Testing | Software Testing Performance Testing is conducted to discover the response time, throughput, etc., and also to execute its required functions by doing a comparison with different versions of the same product or different competitive products. It is a type of software testing that ensures that software application
3 min read
Testing Documentation - Software Testing
Testing documents are prepared at different stages. These documents are discussed as follows. 1. Before Testing: Since testing begins with the generation of the test cases. The following documents are required for reference – SRS document - Functional Requirements document. Test Policy document - It means the product must be tested far before relea
2 min read
API Testing - Software testing
API testing, or application programming interface testing, is a type of software testing that focuses on the testing of individual API methods and the interactions between different APIs. This type of testing is typically performed at the integration level, after unit testing is completed, and before user interface testing begins. It is used to val
7 min read
Mutation Testing - Software Testing
Mutation Testing is a type of Software Testing that is performed to design new software tests and also evaluate the quality of already existing software tests. Mutation testing is related to modification a program in small ways. It focuses to help the tester develop effective tests or locate weaknesses in the test data used for the program.  Histor
3 min read
Sanity Testing Vs Smoke Testing - Software Engineering
Smoke Testing is a type of testing that is done to ensure that the acute functionalities of the program are working fine. It is also known as a subset of acceptance testing, and it is used to test all over the function of the system/product. On the other hand. Sanity Testing is done to check the bugs have been fixed after the build. And it is also
5 min read
Stability Testing - Software Testing
Stability Testing is a type of Software Testing to check the quality and behavior of the software in different environmental parameters. It is defined as the ability of the product to continue to function over time without failure.  It is a Non-functional Testing technique that focuses to stress the software component to the maximum. Stability test
3 min read
Scalability Testing - Software Testing
Scalability Testing is a type of non-functional testing in which the performance of a software application, system, network or process is tested in terms of its capability to scale up or scale down the number of user request load or other such performance attributes. It can be carried out at a hardware, software or database level. Scalability Testi
6 min read
Scenario Testing - Software Testing
Scenario testing helps testers to know how the software will exactly work when end user will use it. As the scenario testing tests the business process flow of the software so it helps in figure out a lot of defects which cannot be found with the help of other testing. Scenario testing is carried out by creating test scenarios which copy the end us
4 min read
Dynamic Testing - Software Testing
Dynamic testing is a type of software testing that involves executing the software and evaluating its behavior during runtime. It is also known as functional testing, as it focuses on testing the software's functionality and how it behaves under different inputs and conditions. In this article, we'll learn about its objectives, levels, processes, a
6 min read
Portability Testing - Software Testing
Software testing that assesses a program's ability to function properly in many environments without requiring major changes is known as portability testing. The goal is to guarantee that the program may be transferred or ported to different platforms, operating systems, browsers, and configurations and remains functional, flexible, and compatible.
5 min read
Sandwich Testing - Software Testing
Sandwich Testing combines the bottom-up and top-down approaches, so it uses the advantage of both the bottom-up and top-down approaches. Initially, it uses the stubs and drivers where stubs simulate the behavior of a missing component. It is also known as Hybrid Integration Testing. Purpose of Sandwich TestingEntire Coverage: Sandwich testing tests
3 min read
Comparison between Regression Testing and Re-Testing - Software Engineering
This article focuses on discussing the comparison between Regression Testing and Re-Testing. What is Regression Testing?Regression Testing is a type of software testing, which is used to verify that modifications in the software or the environment have not caused any unintended adverse side effects. This technique involves retesting the entire syst
4 min read
three90RightbarBannerImg