Main Content

polyspace.test.FunctionCoverageInfo Class

Namespace: polyspace.test

(Python) Review function coverage results

Since R2024b

Description

This Python® class contains function coverage results obtained from executing C/C++ tests. Depending on how you create an object of this class, the results can show the function coverage for an entire project, a single file, or a single function.

Creation

Description

functionCoverageInfo = coverageResults.getCoverageInfo("function") loads function coverage results for the entire project:

functionCoverageInfo = coverageResults.getCoverageInfo("function", fileName) loads function coverage results for the file fileName (specified by full path or path relative to the current working folder):

functionCoverageInfo = coverageResults.getCoverageInfo("function", fileName, functionName) loads function coverage results for the file fileName and function functionName:

Properties

expand all

Total number of functions to be covered, specified as an integer.

For more information on function coverage, see Function Coverage.

Number of functions actually covered during test execution, specified as an integer.

For more information on function coverage, see Function Coverage.

Number of uncovered functions that you justified during review, specified as an integer.

For more information:

Details of condition coverage, specified as a list of polyspace.test.FunctionCoverageDetail objects with the following properties:

PropertyDescription
FileFile containing function.
FunctionFunction name.
IsCoveredWhether the function body is entered at least once during test execution.
IsJustifiedWhether you justified the function coverage (provided the function was not covered during test execution).
RationaleRationale for justification, if you justified the function coverage.
SourceLocation

Location of the function definition, specified as a polyspace.test.SourceLocation object with the following properties:

  • StartLine – Line number where the function signature in the definition begins. For instance, for a function initOrReset defined as:

    void initOrReset
       (state* myState, int initValue) {
       //Function body
    }
    The StartLine property shows the line containing void initOrReset.

  • StartColumn – Column number where the function signature in the definition begins. In the preceding example, the StartColumn property shows the column containing the letter v of void.

  • EndLine – Line number where the function signature in the definition ends. In the preceding example, the EndLine property shows the line containing (state* myState, int initValue).

  • EndColumn – Column number where the function signature in the definition ends. In the preceding example, the StartColumn property shows the column containing the closing parenthesis.

Examples

collapse all

This example shows how to check if all functions were entered at least once during 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.

  1. Import the required modules:

    import polyspace.project
    import polyspace.test
    import os

  2. Add 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"))

  3. Set the coverage metric level to STATEMENT in the active test configuration of the project:

    coverageMetricLevel = polyspace.project.CoverageMetricLevel.DECISION
    polyspaceProject.ActiveTestConfiguration.CoverageOptions.Level = coverageMetricLevel
    For more information on coverage metric levels, see Coverage metrics (-cov-metric-level).

  4. Run the tests added to the project with code coverage computation enabled.

    res = polyspace.test.run(
          polyspaceProject,
          ProfilingSelection=polyspace.test.ProfilingSelection.COVERAGE
    )

  5. Check if all functions were entered at least once during test execution.

    # Read code coverage results
    profilingResults = res.Profiling
    coverageResults = profilingResults.Coverage
    
    # Read statement coverage results
    functionCoverageResults = coverageResults.getCoverageInfo("function")
    
    # Check if all functions were entered at least once
    if functionCoverageResults.TotalCount == functionCoverageResults.CoveredCount:
        print("All functions were covered.")
    else: 
        print("At least one function was not covered.")

Version History

Introduced in R2024b