polyspace.test.CoverageResults Class
Namespace: polyspace.test
Description
This Python® class contains code coverage results obtained from executing C/C++ tests.
Creation
Description
coverageResults = profilingResults.Coverage loads coverage results:
From a
polyspace.test.ProfilingResultsobjectprofilingResults.To a
polyspace.test.CoverageResultsobjectcoverageResults.
Properties
List of files associated with the coverage results, specified as a list of polyspace.test.CoverageFile objects with the following properties:
| Property | Description |
|---|---|
FullPath | Full path to file |
Path | Shortest unique file path. This is the shortest path to the file that allows it to be distinguished from another file in the list with the same name. For instance, suppose there are two files with the same name on these paths: /common/folder/a/path/to/file1.c /common/folder/other/path/to/file1.c a/path/to/file1.c other/path/to/file1.c |
Functions | Functions in the file, specified as a list of
|
To drill down on profiling results for a specific metric and file, use the getCoverageInfo() method. See Methods.
Whether compact mode was enabled for code coverage calculation, specified as a boolean. The compact mode is disabled by default. To enable this mode, set the CoverageOptions.Compact option in the polyspace.project.TestConfig object associated with your project.
Coverage filters associated with the coverage results object, specified as a polyspace.test.CoverageFiltersView object. To inspect the filter rules, index into the Rules property of this polyspace.test.CoverageFiltersView object. To modify the list of filters associated with the coverage results object, use the importFilters and resetFilters methods documented below.
Methods
| Method | Description |
|---|---|
getCoverageInfo() | Extract coverage results for a specific coverage metric: metricResults = coverageResults.getCoverageInfo(
metric
) can be:
You can also extract metric-specific coverage results:
Depending on the metric you specified, the method
And so on. |
applyfilters() | Mass-justify missing coverage results using one of the following:
|
importFilters() | Import filters from a covRes.importFilters(filters) |
resetFilters() | Reset the covRes.resetFilters() |
Examples
This example shows how to see an overview of code coverage results after test execution.
In general, you generate and manage Polyspace
Test™ results by using classes from the polyspace.project and polyspace.test modules. Before starting, make sure you can import these modules on a Python shell or in a Python script without errors. For more information, see Set Up Python API for Polyspace.
Import the required modules:
import polyspace.project import polyspace.test import osAdd source files and xUnit test files to a project. This example uses some example source and test files available with a Polyspace Test installation. Instead, you can use your own sources and tests.
examples_path = os.path.join(polyspace.__install_path__, "polyspace", "examples", "pstest", "Getting_Started_Example") polyspaceProject = polyspace.project.Project("newProject") polyspaceProject.Code.Files.add(os.path.join(examples_path, "sources", "utils.c")) polyspaceProject.IncludePaths.add(os.path.join(examples_path, "includes")) polyspaceProject.Tests.Files.add(os.path.join(examples_path, "tests", "test.c"))Set the coverage metric level to
MCDCin the active test configuration of the project:The other possible values instead ofcoverageMetricLevel = polyspace.project.CoverageMetricLevel.MCDC polyspaceProject.ActiveTestConfiguration.CoverageOptions.Level = coverageMetricLevelMCDCareSTATEMENT,DECISION,CONDITION_DECISION, orNONE. For an explanation of the coverage metric levels, seeCoverage metrics (-cov-metric-level).Run the tests added to the project with code coverage computation enabled.
res = polyspace.test.run( polyspaceProject, ProfilingSelection=polyspace.test.ProfilingSelection.COVERAGE )Extract the coverage results and view which coverage metric passes a specific threshold, for example, 50%.
# Read coverage results profilingResults = res.Profiling coverageResults = profilingResults.Coverage coverage_metrics = ["decision","condition","mcdc","statement","function call", \ "function", "function exit"] # Show pass/fail status of coverage results based on coverage percentage print("Coverage Threshold is 50%") for metric in coverage_metrics: metric_details = coverageResults.getCoverageInfo(metric, "utils.c") if metric_details.TotalCount: pct = (metric_details.CoveredCount + metric_details.JustifiedCount) / metric_details.TotalCount print(f"{metric} coverage is {pct:.2%}\n ", " (FAIL)" if pct < 0.5 else " (PASS)") else: print(f"{metric} coverage not calculated\n")If you use the example source and test files, you see an output like this:
decision coverage is 50.00% (PASS) condition coverage is 40.00% (FAIL) mcdc coverage is 10.00% (FAIL) statement coverage is 57.14% (PASS) function call coverage is 100% (PASS) function coverage is 66.67% (PASS) function exit coverage is 42.86% (FAIL)
In the previous example, in the source file utils.c, the function isGreaterThanSpeedLimit contains an unreachable branch because the if condition in this function is always true. In this example, you apply a filter to justify all missing code coverage results in this function.
Get coverage results using the steps in the previous example:
import polyspace.project import polyspace.test import os # Add sources and tests examples_path = os.path.join(polyspace.__install_path__, "polyspace", "examples", "pstest", "Getting_Started_Example") polyspaceProject = polyspace.project.Project("newProject") polyspaceProject.Code.Files.add(os.path.join(examples_path, "sources", "utils.c")) polyspaceProject.IncludePaths.add(os.path.join(examples_path, "includes")) polyspaceProject.Tests.Files.add(os.path.join(examples_path, "tests", "test.c")) # Configure coverage computation coverageMetricLevel = polyspace.project.CoverageMetricLevel.MCDC polyspaceProject.ActiveTestConfiguration.CoverageOptions.Level = coverageMetricLevel # Run tests with coverage enabled res = polyspace.test.run( polyspaceProject, ProfilingSelection=polyspace.test.ProfilingSelection.COVERAGE ) # Read coverage results profilingResults = res.Profiling coverageResults = profilingResults.CoverageInspect the decision coverage results for the
isGreaterThanSpeedLimitfunction.result_isGreaterThanSpeedLimit = coverageResults.getCoverageInfo("decision", "utils.c","isGreaterThanSpeedLimit") print(result_isGreaterThanSpeedLimit)Only one of the two decision outcomes of the
ifstatement is covered by the tests.{ TotalCount: 2 CoveredCount: 1 JustifiedCount: 0 Details: [<DecisionCoverageDetail>] }Define a filter to justify all missing code coverage results in the
isGreaterThanSpeedLimitfunction. Apply this filter to thecoverageResultsobject to obtain a filtered objectfilteredCoverageResults.filters = polyspace.test.CoverageFilters() filters.Rules.createForFunction("isGreaterThanSpeedLimit", "utils.c", Rationale="Unreachable code", Status="justified") filteredCoverageResults = coverageResults.applyFilters(filters)Instead of using the
createForFunctionmethod, you can also use:The
createForFilemethod to create a rule that applies to an entire fileThe
createmethod to create a rule that filters a specific decision or a specific decision outcome
For more information on these methods, see the description of the
Rulesproperty of thepolyspace.test.CoverageFiltersclass.Inspect the filtered decision coverage results for the
isGreaterThanSpeedLimitfunction.filteredResult_isGreaterThanSpeedLimit = filteredCoverageResults.getCoverageInfo("decision", "utils.c","isGreaterThanSpeedLimit") print(filteredResult_isGreaterThanSpeedLimit)The previously uncovered decision outcome has now been marked as justified.
{ TotalCount: 2 CoveredCount: 1 JustifiedCount: 1 Details: [<DecisionCoverageDetail>] }
Version History
Introduced in R2024bThe polyspace.test.CoverageResults class has a new property Filters and three new methods applyFilters, importFilters, and resetFilters. Use these methods to justify missing code coverage by importing and applying predefined filters that are instances of the polyspace.test.CoverageFilters class.
The polyspace.test.CoverageResults class has a new property Compact that indicates whether the compact mode was enabled for code coverage calculation.
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)