polyspace.project.ScriptedTestStep Class
R2026bNamespace: polyspace.project
Description
This Python® class contains information about a scripted test step that you author using the Polyspace® Test™ Python API. This class includes:
The body of the step that contains the code this step executes
Inputs supplied to the step body
Step observables
Assessments against which the success of the test is evaluated
Note
You can only assign strings to the Value property of an input, assessment, or observable. Therefore, put quotes around the values you want to assign. For example:
To specify the integer
42, assign the string"42"to theValueproperty.To specify the double
3.14, assign the string"3.14"to theValueproperty.To specify the string
"My String", assign the string""My String""to theValueproperty.To specify the address of a variable
var, assign the string"&var"to theValueproperty.
When generating code for your tests, Polyspace Test uses the content of the string to reconstruct the value of the input, assessment, or observable.
Creation
Syntax
Description
scriptedStep = testCase.TestSteps.createScripted(
creates a scripted test step with name stepName)stepName in a polyspace.project.TestCase or polyspace.project.OwnedTestCase object testCase.
scriptedStep = testCase.TestSteps.createFrom(
creates a scripted test step with name existingScriptedStep, stepName)newName by copying an existing
scripted test step existingScriptedStep.
Input Arguments
Existing scripted test step to copy, specified as a
polyspace.project.ScriptedTestStep object. The existing step can
be from any test case.
Name of test step specified as a string. This name is assigned to the Name property of the newly created step.
Properties
Name of the test step, specified as a string. You specify this name when you create the scripted test step using either the createScripted or createFrom method.
Description associated with the test step, specified as a string.
Whether this step is executed or not when you run your project, specified as either True or False. The default value is True.
List of inputs associated with the test step, specified as a polyspace.project.StepInputList object. Each individual input contained in this list is a polyspace.project.StepInput object that contains these properties:
Name— Name of the input variable in your source code, specified as a string. This property is read-only.Scope— Scope of the input variable such as"local", specified as a string. This property is read-only.Type— Data type of the input, specified as a string. This property is read-only.Value— Value of the input for this test step, specified as a string,polyspace.project.TestDataobject, orpolyspace.project.TestParameterobject. By default, the input values are initialized to"0".
This table summarizes the various ways you can access or modify a test inputs list object scriptedStep.Inputs, where scriptedStep is a polyspace.project.ScriptedTestStep object.
| Action | Command |
|---|---|
| Access individual inputs |
|
| Assign value to an input | Assign a string, |
| Add new input for a primitive type | Suppose that |
| Delete an input |
|
| Delete all inputs |
|
The code that the scripted test step executes, specified as a string. The following code snippet is an example of a step body specification.
# Step body that invokes saturate_and_cache multiple times.
scriptedStep.Body = r"""
out1 = saturate_and_cache(in);
out2 = saturate_and_cache(in+3);
out3 = saturate_and_cache(in+5);
"""List of observables associated with the test step, specified as a polyspace.project.StepObservableList object. Each individual observable contained in this list is a polyspace.project.StepObservable object that contains these properties:
Name— Name of the observable variable in the step body, specified as a string. This property is read-only.Type— Data type of the observable variable, specified as a string. This property is read-only.
This table summarizes the various ways you can access or modify an observable list object scriptedStep.Observables, where scriptedStep is a polyspace.project.ScriptedTestStep object.
| Action | Command |
|---|---|
| Access individual observables |
|
| Add new observable for a primitive type | Suppose that When you create a new observable, an assessment is automatically created for that observable. For example, to assign a value to the assessment that is automatically created for |
| Delete an observable |
|
| Delete all observables |
|
List of assessments associated with the test step, specified as a polyspace.project.StepAssessmentList object. Each individual assessment contained in this list is a polyspace.project.StepAssessment object that has these properties:
Comparator— Comparator for the assessment, specified as apolyspace.project.AssessmentComparatorenum class object. The enum values areEQUAL,GREATER,GREATER_EQUAL,LESS,LESS_EQUAL,NONE, andNOT_EQUAL.Enabled— Option to use this assessment to determine if the test passes, specified asTrueorFalse.Name— Name of the assessment variable, specified as a string. This property is read-only.Scope— Scope of the assessment variable such as"local"or"fcn_call", specified as a string. This property is read-only.Tolerance— Tolerance of the assessment, specified as a string. This property is read-only.Type— Data type of the assessment variable, or"call count"for function call count assessments, specified as a string. This property is read-only.Value— Value of the assessment, specified as a string or apolyspace.project.TestDataobject. By default, the assessment values are initialized to"0", or"0u"for function call count assessments.
This table summarizes the various ways you can access or modify a step assessment
list object scriptedStep.Assessments, where
scriptedStep is a
polyspace.project.ScriptedTestStep object.
| Action | Command |
|---|---|
| Access individual assessments |
|
| Assign value to an assessment | Assign a string or a |
| Add new assessment |
|
| Add new function call count assessment | Create a function call count assessment and verify that a function is called at least once: Comparator and Value properties:To add a function call count assessment for a function, the function must be supported for mocking. For more information see Limitations in |
| Delete assessment |
|
| Delete all assessments | Use the |
Since R2026b
Function call sequence assessment for the test step, specified as a polyspace.project.CallSequenceAssessment object. Use this property to verify
that specific functions are called in a defined order during test execution. Optionally,
enable input parameter assessments for each function call to verify that the functions
in the call sequence were called with the expected input or range of input
values.
For more information, see polyspace.project.CallSequenceAssessment.
Mocks applied to the step specified as a polyspace.project.ActiveMockSet object. Use the following methods to populate the set:
To apply a mock described by a
polyspace.project.Mockobjectmock, use theadd()method:For more information, seescriptedStep.ActiveMocks.add(mock)polyspace.project.Mock.To remove a
polyspace.project.Mockobjectmockfrom the step, use theremove()method:scriptedStep.ActiveMocks.remove(mock)To remove all mocks, use the
clear()method:scriptedStep.ActiveMocks.clear()
Examples
This example shows how to add a scripted test step in a graphical test case.
Before starting, make sure you can import the polyspace.project and
polyspace.test modules on a Python shell or in a Python script without errors. For more information, see Set Up Python API for Polyspace. The source
file used in this python API example is available with a Polyspace installation.
Create a project, add source files, and include the source folder which contains the header file. Create a test case with preamble to include the header file containing the class definition. Then, create a scripted step and enter the step body that invokes the constructor and member functions.
# Import the required modules.
import polyspace.project, polyspace.test
import os
# Create the project, add the source files and add the source folder as an include path.
proj = polyspace.project.Project("newProject")
example_path = os.path.join(polyspace.__install_path__, "polyspace",
"examples", "doc_pstest", "scripted_tests")
proj.Code.Files.add(os.path.join(example_path, "src", "Position.cpp"))
proj.IncludePaths.add(os.path.join(example_path, "src"))
# Set the configuration language to C++ and parse source code.
proj.ActiveBuildConfiguration.Language = "cpp"
codeInfo = polyspace.project.parseCode(proj)
# Create a test suite and add a test case to the suite.
mySuite = proj.TestSuites.create("positionTestSuite")
myTestCase = mySuite.TestCases.create("positionScriptedTest")
# Set the test preamble to include the header with the class definition.
myTestCase.Preamble = '#include "Position.hpp"'
# Create a scripted step and set the step body of the scripted step to invoke the constructor and member functions.
scriptedStep = myTestCase.TestSteps.createScripted("testPositionConstructor")
scriptedStep.Body = """
Position currentPosition(xPos, yPos);
xMeasured = currentPosition.getX();
yMeasured = currentPosition.getY();
"""
# Get the int type from parsed code.
intType = codeInfo.getType("int")
# Add inputs for the constructor arguments.
scriptedStep.Inputs.create("xPos", intType)
scriptedStep.Inputs.create("yPos", intType)
scriptedStep.Inputs["xPos"].Value = "1"
scriptedStep.Inputs["yPos"].Value = "2"
# Add observables for the values returned by getX() and getY().
scriptedStep.Observables.create("xMeasured", intType)
scriptedStep.Observables.create("yMeasured", intType)
# Set assessments for the observables.
scriptedStep.Assessments["xMeasured"].Value = "1"
scriptedStep.Assessments["yMeasured"].Value = "2"
# Run the test.
res = polyspace.test.run(proj)
For more information on how to write scripted tests using the Polyspace Platform user interface, see Test C/C++ Functions by Using Scripts in Graphical Tests.
This example shows how to create a mock for a callee function and
apply the mock to a scripted test step. This example extends the previous example by
mocking Position::keepValueWithinRange(int), which is called from the
constructor Position::Position(int, int).
Before starting, make sure you can import the polyspace.project and
polyspace.test modules on a Python shell or in a Python script without errors. For more information, see Set Up Python API for Polyspace. The source
file used in this python API example is available with a Polyspace installation.
Create the project, parse code, and set up the scripted test as in the previous
example. Then, create a mock for keepValueWithinRange and apply it to
the scripted
step.
# Import the required modules.
import polyspace.project, polyspace.test
import os
# Create the project, add the source files and Add the source folder as an include path.
proj = polyspace.project.Project("newProject")
example_path = os.path.join(polyspace.__install_path__, "polyspace",
"examples", "doc_pstest", "scripted_tests")
proj.Code.Files.add(os.path.join(example_path, "src", "Position.cpp"))
proj.IncludePaths.add(os.path.join(example_path, "src"))
# Set the configuration language to C++ and parse source code.
proj.ActiveBuildConfiguration.Language = "cpp"
codeInfo = polyspace.project.parseCode(proj)
# Create a test suite and add a test case to the suite.
mySuite = proj.TestSuites.create("positionTestSuite")
myTestCase = mySuite.TestCases.create("positionScriptedTest")
# Set the test preamble to include the header with the class definition.
myTestCase.Preamble = '#include "Position.hpp"'
# Create a scripted step and set the step body of the scripted step to invoke the constructor and member functions.
scriptedStep = myTestCase.TestSteps.createScripted("testPositionConstructor")
scriptedStep.Body = """
Position currentPosition(xPos, yPos);
xMeasured = currentPosition.getX();
yMeasured = currentPosition.getY();
"""
# Get the int type from parsed code.
intType = codeInfo.getType("int")
# Add inputs for the constructor arguments.
scriptedStep.Inputs.create("xPos", intType)
scriptedStep.Inputs.create("yPos", intType)
scriptedStep.Inputs["xPos"].Value = "1"
scriptedStep.Inputs["yPos"].Value = "2"
# Add observables and assessments.
scriptedStep.Observables.create("xMeasured", intType)
scriptedStep.Observables.create("yMeasured", intType)
scriptedStep.Assessments["xMeasured"].Value = "1"
scriptedStep.Assessments["yMeasured"].Value = "2"
# Create a mock for keepValueWithinRange.
funcToMock = codeInfo.getFunctionBySignature("int Position::keepValueWithinRange(int)")
mockKeepValue = proj.Mocks.create(funcToMock)
# Apply the mock to the scripted step.
scriptedStep.ActiveMocks.add(mockKeepValue)
# Run the test.
res = polyspace.test.run(proj)
For more information on how to write scripted tests using the Polyspace Platform user interface, see Test C/C++ Functions by Using Scripts in Graphical Tests.
Version History
Introduced in R2025aUse the CallSequenceAssessment property to create function call
sequence assessments in a scripted test step. For more information, see .polyspace.project.CallSequenceAssessment
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)