Contenuto principale

polyspace.test.ProfilingResults Class

Namespace: polyspace.test

(Python) Review code profiling results

Since R2024b

Description

This Python® class contains code profiling results obtained from executing C/C++ tests.

Creation

Description

profilingResults = testResultsObj.Profiling loads code profiling results:

profilingResults = polyspace.test.ProfilingResults("/path/to/profiling_results.psprof") loads code profiling results:

  • From a file profiling_results.psprof.

  • To a polyspace.test.ProfilingResults object profilingResults.

profilingResults = polyspace.test.ProfilingResults(coverageResults) loads code coverage results:

  • From a polyspace.test.CoverageResults object coverageResults.

  • To a polyspace.test.ProfilingResults object profilingResults that has:

    • The Coverage property is set to coverageResults.

    • The Execution and Stack properties set to None.

Properties

expand all

Start date and time of test execution specified as a datetime.datetime object.

End date and time of test execution specified as a datetime.datetime object.

Coverage results specified as a polyspace.test.CoverageResults object, if you set the code profiling mode to code coverage prior to test execution. Otherwise, the value is None.

To specify code coverage calculation on a polyspace.project.Project object polyspaceProject, execute the polyspace.test.run() method as follows:

testResults = polyspace.test.run(
        polyspaceProject, 
        ProfilingSelection=polyspace.test.ProfilingSelection.COVERAGE
)
profilingResults = testResults.Profiling
coverageResults = profilingResults.Coverage

Execution time results specified as a polyspace.test.ExecutionProfilingResults object, if you set the code profiling mode to execution profiling prior to test execution. Otherwise, the value is None.

To specify execution profiling on a polyspace.project.Project object polyspaceProject, execute the polyspace.test.run() method as follows:

testResults = polyspace.test.run(
         polyspaceProject, 
         ProfilingSelection=polyspace.test.ProfilingSelection.EXECUTION
)
profilingResults = testResults.Profiling
coverageResults = profilingResults.Execution

Stack profiling results specified as a polyspace.test.StackProfilingResults object, if you set the code profiling mode to stack profiling prior to test execution. Otherwise, the value is None.

To specify stack profiling on a polyspace.project.Project object polyspaceProject, execute the polyspace.test.run() method as follows:

testResults = polyspace.test.run(
         polyspaceProject, 
         ProfilingSelection=polyspace.test.ProfilingSelection.STACK
)
profilingResults = testResults.Profiling
coverageResults = profilingResults.Stack

Methods

expand all

Examples

collapse all

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.

  1. Import the required modules:

    import polyspace.project, 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 MCDC in the active test configuration of the project:

    coverageMetricLevel = polyspace.project.CoverageMetricLevel.MCDC
    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. Parse 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)
    

Version History

Introduced in R2024b