Contenido principal

polyspace.test.ScriptedTestGenerationConfiguration Class

R2026b

Namespace: polyspace.test

(Python) Configure script-based automatic test generation

Since R2026b

Description

Use this Python® class to configure automatic test generation for scripts that call one or more C/C++ functions in a Polyspace® Platform project. Provide optional preamble, setup, and teardown code; supply a test script body; and manage test generation inputs using the Inputs property.

Creation

Description

scriptConfig = polyspace.test.ScriptedTestGenerationConfiguration() creates a configuration object for script-based test generation with default settings.

example

Properties

expand all

Collection of inputs used during test generation, specified as a polyspace.test.ScriptedTestGenerationInputList object. Each individual input contained in this list is an instance of the polyspace.test.TestGenerationInput class that contains these properties:

  • Name — Name of the input, specified as a string. This property is read-only.

  • Scope — Scope of the input, specified as a string. This property is read-only.

  • Type — Type of the input, specified as a string. This property is read-only.

  • Value — Fixed value of the input in generated tests, specified as a string. By default, the input values are initialized to "".

  • Min — Minimum value of the input in generated tests, specified as a string. By default, the minimum values are initialized to "".

  • Max — Maximum value of the input in generated tests, specified as a string. By default, the maximum values are initialized to "".

Note

You can only assign strings to the Value, Max, or Min property of an input object. Therefore, put quotes around the values you assign. For example:

  • To specify the integer 42, assign the string "42" to the Value property.

  • To specify the double 3.14, assign the string "3.14" to the Value property.

  • To specify the string "My String", assign the string '"My String"' to the Value property.

When generating code for your tests, Polyspace Test™ uses the content of the string to reconstruct the constraints on the function input.

This table summarizes the various ways you can modify a script input list object scriptConfig.Inputs, where scriptConfig is a polyspace.test.ScriptedTestGenerationConfiguration object.

ActionCommand
Access individual inputs
  • Index using numeric location. For example, scriptConfig.Inputs[2].

  • Index using the name of the input variable varName. For example, scriptConfig.Inputs["varName"].

Add constraints

To apply constraints on the value of an input in the generated tests, set the Value, Min, and Max properties. For example:

scriptConfig.Inputs[0].Value = "23"
scriptConfig.Inputs[1].Min = "0"
scriptConfig.Inputs[1].Max = "100"

Add new input for a primitive type

Suppose that codeInfo is the polyspace.project.CodeInfo object that you obtain after parsing your source code. Also assume that your test script contains a variable myInput of int type that you want to specify as an input. Run this command:

dataType = codeInfo.getType("int")
scriptConfig.Inputs.create("myInput",dataType)

Delete an input
  • Remove an input by name:

    scriptConfig.Inputs.pop("myInput")
  • Remove the input at index 3:

    scriptConfig.Inputs.pop(3)
  • Remove the last input in the list:

    scriptConfig.Inputs.pop()
Delete all inputs

To delete all inputs, use the clear method:

scriptConfig.Inputs.clear()

Optional C/C++ code compiled into the generated test harness prior to executing tests, specified as a string.

For example, you can use this property to specify include statements or helper declarations:

scriptConfig.Preamble = '#include "decls.h"'

Optional C/C++ code executed before each generated test. For example, you can use this property to specify code that performs initialization of a global state.

Optional C/C++ code executed after each generated test. For example, you can use this property to specify code that performs cleanup actions after each test is executed.

C/C++ code that forms the body of the scripted test. For example, use this property to specify code that exercises functions that you want to test.

Examples

collapse all

Generate script-based tests to achieve a coverage objective while constraining the values of the function inputs.

Create the project, add your source files, and parse the code. Then, specify coverage objectives for test generation. Next, specify test script body that exercises the checkAgainstSpeedLimit function. Next, specify preamble, script inputs, and constraints. Finally, generate tests that achieve the coverage objectives.

## Import modules
import polyspace.project
import polyspace.test
import os

## Create project
examples_path = os.path.join(polyspace.__install_path__, "polyspace", 
                            "examples", "doc_pstest", "coverage_tests")

proj = polyspace.project.Project("myProject.psprjx")

## Add source file and include path
proj.Code.Files.add(os.path.join(examples_path, "src","helpers.c"))
proj.IncludePaths.add(os.path.join(examples_path, "src"))

## Parse code - returned object contains list of functions and other source code data
codeInfo = polyspace.project.parseCode(proj)

## Get function
func = codeInfo.getFunctionBySignature("bool checkAgainstSpeedLimit(uint32_t, uint32_t)")

## Set coverage objective to DECISION for test generation
cOpts = polyspace.test.CoverageTestOptions()
cOpts.Level = polyspace.project.CoverageMetricLevel.DECISION

## Specify preamble and test script
## Add inputs and set constraints
cfg = polyspace.test.ScriptedTestGenerationConfiguration()
cfg.Preamble = '#include "decls.h"'
cfg.TestScriptBody = 'bool res = checkAgainstSpeedLimit(speed, limit);'

cfg.Inputs.create("speed",codeInfo.getType("unsigned int"))
cfg.Inputs["speed"].Min = "20u"
cfg.Inputs["speed"].Max = "250u"

cfg.Inputs.create("limit",codeInfo.getType("unsigned int"))
cfg.Inputs["limit"].Value = "70u"

# Generate tests
testGenResults = polyspace.test.generateTests(proj, cfg, cOpts)

Version History

Introduced in R2026b