polyspace.test.CoverageFilters Class
Namespace: polyspace.test
Description
Create or load filters for code coverage results. You can use these filters to justify missing coverage results.
Creation
Syntax
Description
creates a coverage filters object coverageFilters = polyspace.test.CoverageFilters()coverageFilters with an empty Rules property.
loads a coverage filters object from the file coverageFilters = polyspace.test.CoverageFilters("filtersFile")"filtersFile.psprof.filter". You do not need to include the extension (.psprof.filter) in the name of the file you supply to the constructor.
Properties
List of coverage filter rules, specified as a polyspace.test.CoverageFilterRuleList object. To add a new rule to the list, use one of these polyspace.test.CoverageFilterRuleList methods:
rules.create(filterableObject, Rationale = "My Rationale", Status = "justified"|"unreviewed")rules.createForFile(fileName, Rationale = "My Rationale", Status = "justified"|"unreviewed")rules.createForFunction(functionName, fileName, Rationale="My Rationale", Status = "justified"|"unreviewed")
Notes about the creation method syntaxes:
The
filterableObjectinput of thecreatemethod can be one of these objects:polyspace.test.FunctionCoverageDetail,polyspace.test.DecisionCoverageDetail,polyspace.test.ConditionCoverageDetail,polyspace.test.CoverageOutcome,polyspace.test.MCDCCoverageOutcome.If you do not explicitly specify the
Statusinput, it is set to"unreviewed".The
Rationaleinput is optional ifStatusis set to"justified".Alternatively, you can set
Statusto one of thesepolyspace.test.CoverageFilterRuleStatusenum class objects:polyspace.test.CoverageFilterRuleStatus.JUSTIFIEDpolyspace.test.CoverageFilterRuleStatus.UNREVIEWED
To inspect the individual rules, index into the polyspace.test.CoverageFilterRuleList object. For example:
filters = polyspace.test.CoverageFilters()
rules = filters.Rules
rules.createForFunction("isGreaterThanSpeedLimit", "utils.c", Rationale="Unreachable code", Status="justified")
print(rules[0]){
File: 'D:\Getting_Started_Example\sources\utils.c'
Function: 'checkAgainstSpeedLimit'
Status: JUSTIFIED
Kind: FUNCTION
Rationale: 'Unreachable code'
}Each rule is an instance of the polyspace.test.CoverageFilterRule class which has these properties:
File | Name of the file to which this rule applies, specified as a string. |
Function | Name of the function to which this rule applies, specified as a string. |
Status | Status associated with this rule, specified as an enum class object. The values are:
|
Kind | Whether this rule is associated with a file, a function, or a certain kind of detail or outcome object, specified as an enum class object. The values are:
|
Rationale | Rationale for the rule, specified as a string. |
To remove one or more rules from the list, use these polyspace.test.CoverageFilterRuleList methods:
rules.pop(removes the rule object with indexmyRuleIdx)myRuleIdxfrom the rule list objectrules.rules.clear()removes all rules included in the rule list objectrules.
Methods
save | Save the coverage filters object to a new or existing coverageFilters.save("filter_rules")You do not need to include the file extension |
Examples
This example shows how apply a filter to your code coverage results to justify missing code coverage for an unreachable branch in a function.
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 code coverage results object
coverageResultsfrom theresobject.profilingResults = res.Profiling coverageResults = profilingResults.CoverageIn the source file
utils.c, the functionisGreaterThanSpeedLimitcontains an unreachable branch because theifcondition in this function is always true. Inspect the decision coverage results for this function.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 R2025a
See Also
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)