polyspace.project.TestCase Class
R2026bNamespace: polyspace.project
Description
This Python® class contains information about a graphical test case that is saved separately
from a Polyspace® Platform project in a .pstestd file. When you add a reference
to this .pstestd file from a project, it is called a test reference. Saving
your graphical test cases in .pstestd files and referencing those files
from one or more projects enables you to create a modular project structure that improves
sharing and version control workflows. For more information about project structure, see Modularize Project by Using External Configurations, Test References, and External Stub Files.
To work with test references in the Polyspace
Python API, use this class, which contains the test case definition, together with the
polyspace.project.TestCaseRef class, which contains the name of the graphical test
case and the path to the .pstestd file where it is saved. You can also work
with test references in the Polyspace Platform user interface.
To create and manage test cases that are owned by the project, use the polyspace.project.OwnedTestCase class.
Creation
Description
referencedTestCase = myTestCaseRefObj.get() returns the
polyspace.project.TestCase object referenced by the
polyspace.project.TestCaseRef object
myTestCaseRefObj. For more information on managing referenced test
cases from a test suite, see polyspace.project.TestCaseRef.
Properties
Absolute or relative path to the .pstestd file where the
referenced test case is saved, returned as a string. Relative paths are considered
relative to the project location. This property is read-only. To manage the name and
path of a referenced test case, use the polyspace.project.TestCaseRef class.
Description of the test case, specified as a string.
Code to run as the preamble for the test case, specified as a string. This code runs before the actual setup phase.
Code to run during the setup phase of the test case, specified as a string. This code sets up the test environment.
Code to run during the teardown phase of the test case, specified as a string. This code performs cleanup actions such as deallocating resources after the test has been executed.
List of test data associated with the test case, specified as a polyspace.project.TestDataList object. Each individual test data contained in this list is an instance of the polyspace.project.TestData class that contains these properties:
Name— Name of the test data, specified as a string. This property is read-only.Type— Type of the test data, specified as a string. This property is read-only.Value— Value of the test data, specified as a string. By default, the test data values are initialized to"0".Note
You can only assign strings to the
Valueproperty of a test data object. Therefore, put quotes around the values you 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 test data or parameter.
This table summarizes the various ways you can modify a test data list object testCase.TestData, where testCase is a polyspace.project.OwnedTestCase object or a polyspace.project.TestCase object.
| Action | Command |
|---|---|
| Add new test data |
The For example, to create a test data array of dimension 4 that contains the integers -1, 1, 3, and 5, run these commands. Here To access members of an aggregate such as a structure or union, use the member name as index. For instance, suppose that you define a structure type intValue of a test data of this type as follows: |
| Delete test data |
|
| Delete all test data |
|
List of test parameters, iteration strategy, and whether to load parameters at run time, specified as a polyspace.project.TestParameters object. This object has these properties:
Parameters— List of test parameters, specified as apolyspace.project.TestParameterListobject. Each individual test parameter contained in this list is an instance of thepolyspace.project.TestParameterclass that has these properties:Name— Name of the test parameter, specified as a string. This property is read-only.Type— Data type of the test parameter, specified as a string. This property is read-only.Value— Values of the test parameter elements, specified as strings. By default, the test parameter element values are initialized to"0".Note
You can only assign strings to the
Valueproperty of a test parameters object. Therefore, put quotes around the values you 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 test data or parameter.
This table summarizes the various ways you can modify a test parameter list object
params.Parameters, whereparamsis apolyspace.project.TestParametersobject.Action Command Add new test parameter testParam = params.Parameters.create("myTestParam",<dataType>)adds a test parameter object whoseNameproperty is set to"myTestParam". Here,<dataType>is apolyspace.project.Typeobject.The
testParamobject contains values of typedataTypethat are initialized to"0". To update these values, set theValueproperty of the elements oftestParam.For example, to create a test data array of dimension 4 that contains all integers from -50 through 50, run these commands. Here
codeInfois thepolyspace.project.CodeInfoobject that you obtain after parsing source code that contains theintdata type.testParam = params.Parameters.create("myParam", codeInfo.getType("int[101]")) for k in range(101): testParam[k].Value = str(k-50)To access members of an aggregate such as a structure or union, use the member name as index. For instance, suppose that you define a structure type
Numberas follows:You can access the membertypedef struct Number { int intValue; double dblValue; } Number;intValueof a two-value test parameter of this type as follows:type = codeInfo.getType("Number[2]") testParam = params.Parameter.create("my_param", type) testParam[0]["intValue"].Value = "1" testParam[1]["intValue"].Value = "2"Delete a test parameter params.Parameters.pop("myTestParam")removes the test parameter with name"myTestParam"from the list.params.Parameters.pop(0)removes the test parameter at index0from the list.params.Parameters.pop()removes the last test parameter in the list.
Delete all test parameters params.Parameters.clear()removes all test parameters from the list.Iteration— Parameter iteration strategy, specified as apolyspace.project.ParameterIterationModeenum class object. The enum values areEXHAUSTIVEandSEQUENTIAL.LoadAtRunTime— Whether to load the parameter values at run time instead of embedding them in the generate code, specified as a boolean.
For more information on the Iteration and LoadAtRunTime properties, see Decide Parameterization Strategy.
List of tabular and scripted test steps associated with the test case, specified as a
polyspace.project.TestStepList object. Each individual test
step contained in this list is an instance of either the polyspace.project.TabularTestStep class or the polyspace.project.ScriptedTestStep class.
This table summarizes the various ways you can modify a test steps list object testCase.TestSteps, where testCase is a polyspace.project.OwnedTestCase or polyspace.project.TestCase object.
| Action | Command |
|---|---|
| Add a new tabular test step |
For example, to add a tabular test step for a function with signature |
| Add a new scripted test step |
|
| Add copy of an existing test step |
Here
|
| Delete a test step |
|
| Delete all test steps |
|
Methods
save() | Save changes to the referenced Example:
|
saveCopy() | Save a copy of the test case file with a new name or in a new location. Example:
|
close() | Close the referenced test case and unload it from memory. The test case object, methods, and properties become stale after you unload the test case. Example:
|
Examples
This example shows you how to modularize your code by:
Creating a test case reference object in one script
Accessing and modifying this test reference in a second script
Normally these two actions would be performed by two different people to
share .pstestd files.
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 simple test for the
getDifferenceOfValuesfunction that supplies input values 2 and 2 to the function and checks the output against the assessment value 0. Convert your test case to a test reference to be used in other scripts.# Import the required modules. import polyspace.project, polyspace.test import os # Create the project and add a source file. proj = polyspace.project.Project("project1") example_path = os.path.join(polyspace.__install_path__, "polyspace", "examples", "doc_pstest", "parametrized_tests_ui") proj.Code.Files.add(os.path.join(example_path, "src", "example_differ.c")) # Parse source code. codeInfo = polyspace.project.parseCode(proj) functionDiff = codeInfo.getFunctionBySignature("int32_t getDifferenceOfValues(int32_t arg1, int32_t arg2)") # Create a test suite and add a test case to the suite. mySuite = proj.TestSuites.create("diffTestSuite") myTestCase = mySuite.TestCases.create("diffSimpleTest") # Create a test step for your test case. simpleStep = myTestCase.TestSteps.createTabular("diffSimpleStep", functionDiff) # Update inputs and assessments. simpleStep.Inputs["arg1"].Value = "2" simpleStep.Inputs["arg2"].Value = "2" simpleStep.Assessments["pst_call_out"].Value = "0" # Convert the myTestCase to a test case reference. mySuite.TestCaseRefs.moveAsRef(myTestCase, "testRefFile.pstestd", "simpleTestRef")In a separate script, access your test reference from the
.pstestdfile and create a test case object. Modify your test case object by adding a new step and saving the test case. Notice the test reference contains the step with inputs and assessments created in part 1.For more information on authoring tests using Python API, see Author Graphical Tests Using Python API for Polyspace.# Import the required modules. import polyspace.project, polyspace.test import os # Create the project and add a source file. proj = polyspace.project.Project("project2") example_path = os.path.join(polyspace.__install_path__, "polyspace", "examples", "doc_pstest", "parametrized_tests_ui") proj.Code.Files.add(os.path.join(example_path, "src", "example_differ.c")) # Parse source code. codeInfo = polyspace.project.parseCode(proj) functionDiff = codeInfo.getFunctionBySignature("int32_t getDifferenceOfValues(int32_t arg1, int32_t arg2)") # Create a test suite mySuite = proj.TestSuites.create("diffTestSuite") # Add the .pstestd as a test reference, which already has a test step with inputs and assessments. testCaseRefObject = mySuite.TestCaseRefs.add(r"path\to\testRefFile.pstestd","simpleTestRef") testCaseObject = testCaseRefObject.get() # Add second test to your test case object newStep = testCaseObject.TestSteps.createTabular("newStep", functionDiff) newStep.Inputs["arg1"].Value = "3" newStep.Inputs["arg2"].Value = "3" newStep.Assessments["pst_call_out"].Value = "0" # Save your test case object to add it to the project. testCaseObject.save() # Run the project. res = polyspace.test.run(proj)
This example shows you how to modularize your code by:
Creating a parameterized test case reference object in one script
Accessing and modifying this test reference in a second script
Normally these two actions would be performed by two different people to
share .pstestd files.
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 parameterized test for the
getDifferenceOfValuesfunction with input values forarg1ranging from -10 to 10. Convert your test case to a test reference to be used in other scripts.# Import the required modules. import polyspace.project, polyspace.test import os # Create the project and add a source file. proj = polyspace.project.Project("project1") example_path = os.path.join(polyspace.__install_path__, "polyspace", "examples", "doc_pstest", "parametrized_tests_ui") proj.Code.Files.add(os.path.join(example_path, "src", "example_differ.c")) # Parse source code. codeInfo = polyspace.project.parseCode(proj) functionDiff = codeInfo.getFunctionBySignature("int32_t getDifferenceOfValues(int32_t arg1, int32_t arg2)") # Create a test suite and add a test case to the suite "diffTests" suiteDiff = proj.TestSuites.create("diffTests") parameterizedCase = suiteDiff.TestCases.create("diffParameterizedTest") parameterizedStep = parameterizedCase.TestSteps.createTabular("multiplyParameterizedStep",functionDiff) # Create integer test parameters that range from -10 to 10. params = parameterizedCase.TestParameters inputArg1Param = params.Parameters.create("myParam", codeInfo.getType("int[21]")) outputParam = params.Parameters.create("outputParam", codeInfo.getType("int[21]")) for k in range(21): inputArg1Param[k].Value = str(k) outputParam[k].Value = str(abs(k)) # Update the automatically created input and assessment. Add parameterized assessments. parameterizedStep.Inputs["arg1"].Value = inputArg1Param parameterizedStep.Inputs["arg2"].Value = "0" parameterizedStep.Assessments["pst_call_out"].Value = outputParam # Set the iteration strategy to sequential. parameterizedCase.TestParameters.Iteration = (polyspace.project.ParameterIterationMode.SEQUENTIAL) # Convert parameterizedCase to a test case reference suiteDiff.TestCaseRefs.moveAsRef(parameterizedCase, "testRefFile.pstestd", "parameterizedTestRef")In a separate script, access your parameterized test reference from the
.pstestdfile and run the project. Modify your test case object by adding a new step and saving the test case. Notice the test reference contains the parameterized test with inputs and assessments created in part 1.For more information on authoring tests using Python API, see Author Graphical Tests Using Python API for Polyspace.# Import the required modules. import polyspace.project, polyspace.test import os # Create the project and add a source file. proj = polyspace.project.Project("project2") example_path = os.path.join(polyspace.__install_path__, "polyspace", "examples", "doc_pstest", "parametrized_tests_ui") proj.Code.Files.add(os.path.join(example_path, "src", "example_differ.c")) # Parse source code. codeInfo = polyspace.project.parseCode(proj) functionDiff = codeInfo.getFunctionBySignature("int32_t getDifferenceOfValues(int32_t arg1, int32_t arg2)") # Create a test suite. mySuite = proj.TestSuites.create("diffTestSuite") # Add the .pstestd as a test reference, which already has the parameterized test case with inputs and assessments. testCaseRefObject = mySuite.TestCaseRefs.add(r"path\to\testRefFile.pstestd","parameterizedTestRef") testCaseObject = testCaseRefObject.get() # Add another test to your test case object newStep = testCaseObject.TestSteps.createTabular("newStep", functionDiff) newStep.Inputs["arg1"].Value = "3" newStep.Inputs["arg2"].Value = "3" newStep.Assessments["pst_call_out"].Value = "0" # Save your test case object to add it to the project. testCaseObject.save() # Run the project. res = polyspace.test.run(proj)
Version History
Introduced in R2025aIn R2026a, the polyspace.project.TestCase class was renamed to
polyspace.project.OwnedTestCase.
Use the
polyspace.project.TestCaseclass to edit test references. A test reference is a graphical test case that is saved in a.pstestdfile and referenced by one or more projects. To manage your test references, use thepolyspace.project.TestCaseRefclass.Use the
polyspace.project.OwnedTestCaseclass to author or edit test cases owned by the project and saved in the.psprjxfile.
See Also
polyspace.project.CodeInfo | polyspace.project.Project | polyspace.project.TestCaseRef | polyspace.project.OwnedTestCase | polyspace.project.ScriptedTestStep | polyspace.project.TabularTestStep
Topics
- Author Graphical Tests Using Python API for Polyspace
- Create Easily Shareable Projects for Version Control
- Modularize Project by Using External Configurations, Test References, and External Stub Files
- Modularize Existing Project by Using Polyspace Python API
- Create Project Dynamically by Using Polyspace Python API
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.
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)