Increase Test Efficiency

 

Code Coverage

 
 
 

Code coverage analyzes the source code executed during runtime, e.g. the code triggered by test cases or during normal program execution. It is a crucial metric in software testing, serving as a measure of testing completeness and providing evidence of the unit's correctness.

 

What is Code Coverage?

Code coverage measures the proportion of executable code that testing runs.

100% Coverage

Achieving 100% code coverage means that every executable part of the code has been run during testing. The most basic measure is statement coverage. Statement coverage ensures every line of code is executed. Other types like function coverage, condition coverage and branch coverage evaluate function calls, conditions, and decision points. 

It is possible to achieve 100% code coverage. But exceptions may occur, e.g. in places with defensive coding to deal with very improbable conditions or where the coverage cannot be measured due to infinite loops.

What Constitutes Good Code Coverage?

Good code coverage depends on the quality and relevance of the tests executed, ideally based on requirements. High coverage suggests thorough requirement fulfillment, while uncovered code needs justification for why it remains untested.

How Do Code Coverage Tools Work?

Code coverage tools use different methods to measure and calculate coverage. The most accurate approach inserts additional statements into the source code through instrumentation. When the code runs, the tool records these statements, marking the corresponding lines in the original source as covered.

Alternative methods include indirect measurement, which estimates coverage based on analyzing executed statements without modifying the source code.

Code Coverage Measurement Tools

Various tools and methods are available for measuring code coverage. One approach integrates source code instrumentation directly into the compiler, as seen with gcc using gcov. Another method involves incorporating source code instrumentation into unit testing tools. Tools like VectorCAST/C++ analyze code, build an instrumented test harness, and manage stubs and mocks. Additionally, specialized tools like VectorCAST/QA instrument the source code for coverage measurement independently of the compiler used.

Many modern code coverage tools support multiple programming languages. These tools are designed to support various programming languages, accommodating different software development environments. VectorCAST offers language support for C/C++ and Ada, which remain the most important languages for safety-critical applications.

C: Widely used in safety-critical systems, especially in aerospace, automotive, and medical devices. Its simplicity and extensive tool support make it a preferred choice.

Ada: Designed specifically for safety-critical and real-time systems, Ada offers strong typing, modularity, and support for concurrent programming.

C++: While more complex than C, C++ is used in safety-critical systems with strict guidelines and subsets like MISRA C++ to ensure safety and reliability

Why Choose VectorCAST for Code Coverage Measurement?

While many tools offer code coverage measurement, VectorCAST stands out with its comprehensive and versatile approach. Unlike basic tools that integrate source code instrumentation directly into the compiler (such as gcc with gcov), VectorCAST/C++ and VectorCAST/QA provide a more robust solution.

Key Advantages of Using VectorCAST

Integrated Test Environment

VectorCAST/C++ not only measures code coverage but also automates the creation of a complete test harness. This includes building instrumented test environments, managing stubs and mocks, and providing a streamlined workflow for unit testing.

Independent of Compiler

VectorCAST/QA offers a unique advantage by instrumenting the source code for coverage measurement independently of the compiler. This allows you to use your preferred compiler while still gaining detailed coverage insights, making it ideal for diverse development environments.

Support for Multiple Languages

VectorCAST supports a variety of programming languages, making it adaptable to different software development projects. This flexibility enables teams to use a single tool for coverage measurement across multiple languages and platforms.

Enhanced Accuracy and Detail

By leveraging source code instrumentation within the testing framework, VectorCAST delivers more accurate and detailed coverage data. This helps identify untested code segments and ensures comprehensive test coverage, ultimately improving software quality.

With these capabilities, VectorCAST not only simplifies the code coverage process but also provides a deeper understanding of your code's quality, making it an essential tool for any development team focused on delivering reliable and high-quality software.

Why Code Coverage Matters

Compliance with all Industry Standards

Many industry standards use code coverage as a measure of test completeness, with varying coverage types depending on industry and criticality.

With VectorCAST, we ensure reliable measurement and compliance across all industry requirements.

White-Box Coverage

When testing integrated software, black-box tests focus on evaluating the software's behavior using only external information and interfaces without visibility into the internal source code. While this approach effectively tests the end product, it can make troubleshooting difficult and time-consuming when a test fails, as the underlying cause remains hidden.

White-box coverage addresses this challenge by directly instrumenting the source code before running tests. This approach tracks which parts of the code are executed during testing. If a test fails, it pinpoints the exact code segments involved, providing immediate insight into the root cause, significantly speeding up the troubleshooting process, and enhancing overall software quality.

Data and Control Coupling

Data and control coupling are architectural properties that define dependencies in data flow and control flow between elements. Verifying these dependencies ensures that the implementation aligns with the intended design. Both data and control couples must be identified, tested, and shown to be complete, including the absence of unintended couples.

 

Data Couples: Capture the order of access for each couple

Control Couples: Record all control couples when called to ensure they are tested.

VectorCAST’s Component and Coupling Coverage Reports provide proof by detecting data couple read/write actions and capturing control couple calls with VectorCAST/Coupling.

Covered-by-Analysis

In safety-critical sectors, it is essential to prove that software does not contain unintended behavior or functionality, which requires achieving full requirements and structural coverage. Some code sections may not reach full coverage through testing alone, and any shortfalls must be justified and documented. Tools that measure code coverage help identify these areas.

VectorCAST allows you to document and store reasons for coverage shortfalls directly alongside the coverage data to simplify this process.

Ensure Code Coverage with VectorCAST

Ensure completeness, compliance, and quality in your software testing with VectorCAST. Explore our solutions to optimize and debug your code efficiently.

FAQs

Code Coverage vs. Test Coverage

Both code coverage and test coverage assess the completeness of software development activities. Test coverage measures how well test cases cover requirements, ensuring they address all aspects.

Code coverage, while independent, supports claims of testing completeness by identifying any uncovered code during test execution. Achieving full code coverage implies no code is left untested. When tests are requirement-based, this also suggests the absence of unintended functionality and unused code. However, the value of code coverage depends on the methods used to achieve it.

What is code coverage analysis?

Code coverage analysis evaluates which parts of the code are not covered by tests and why. Reasons for uncovered code might include missing tests, incomplete requirements, or code existing for specific purposes such as defensive programming or compatibility. Unreachable code could also indicate unnecessary sections that should be removed.

It's crucial to keep code coverage analysis separate from test development to prevent creating tests solely to cover specific lines of code. This approach ensures that high code coverage does not come at the expense of test quality and relevance.

What are different coverage types of code coverage?

There are various types of code coverage, such as structural, requirements, and branch coverage. For example, branch coverage measures how many decision points (e.g., if-else statements) have been executed during testing. Learn more about these coverage types and their relevance on our industry standards page.

Connect with Our Experts

Whether you're seeking advice, looking for solutions, or just curious about our services, we're here to help.
Our team of experienced developers is here to provide the support, and technical expertise you need. Contact us to schedule your consultation and start your journey with us. 

Subscribe To Our Newsletter

Are you passionate about software development?
Want to stay updated with the latest, tools, and best practices we offer?

Join our community by subscribing today!