Create Project from Build System Using Python API for Polyspace
You can manage Polyspace® Test™ workflows entirely at the command line by using the Python® API for Polyspace. In particular, you can dynamically generate a Polyspace Test configuration based on your build command and build your tests using this configuration.
In this example, you create a Polyspace Platform project from a makefile by using a Python script. The script dynamically generates the source and build information from the makefile and then attaches them to the project.
Prerequisites
Make sure you are using a supported Python version and you are able to import the polyspace.project and polyspace.test
Python modules without errors.
import polyspace.project
import polyspace.testExample Files
Source Files
The source and header files used in this example are available in the folder . Here, polyspaceroot/polyspace/examples/doc_pstest/python_api_test_authoring/src is the Polyspace installation folder.polyspaceroot
Build System
The sources in this example can be built outside a project using this makefile, which is available in the folder. This makefile uses the polyspaceroot/polyspace/examples/doc_pstest/python_api_test_authoringPOLYSPACEROOT variable that you specify when you invoke the make command.
# Compiler and flags
CC = gcc
SOURCES = $(wildcard $(POLYSPACEROOT)/polyspace/examples/doc_pstest/python_api_test_authoring/src/*.c)
TARGET = test_program
# Default target
all: $(TARGET)
# Compilation target
$(TARGET): $(SOURCES)
$(CC) -c $(SOURCES)
# Clean target
clean:
rm -f $(TARGET)Dynamically Generate Project with Source and Build Information
To execute the instructions in the makefile to dynamically create a project that contains the source and build information, do the following:
Navigate to a writable folder that will contain the build artifacts and the generated Polyspace Platform project.
Write a Python script to trace your build system (Makefile) and generate a Polyspace Platform project
myProject.psprjx. If this project already exists, sync the project with the latest sources and build information by retracing your build system. See Information Gathered From Build Systems for Polyspace Analysis and Testing.This script:
Uses the
polyspace.__install_path__function to supply thePOLYSPACEROOTvariable to themakecommand.Uses the
-foption to specify the location of the makefile to themakecommand.Invokes the
polyspace-configuresystem command to trace the build.
import os import subprocess makefileArgument = f"POLYSPACEROOT={polyspace.__install_path__}" makefilePath = os.path.join(polyspace.__install_path__, "polyspace", "examples", "doc_pstest", "python_api_test_authoring", "makefile") polyspaceConfigurePath = os.path.join(polyspace.__install_path__, "polyspace", "bin", "polyspace-configure") if os.path.isfile("myProject.psprjx"): subprocess.run([polyspaceConfigurePath, "-update-platform-project", "myProject.psprjx", "make", "-f", makefilePath, makefileArgument, "-B"]) else: subprocess.run([polyspaceConfigurePath, "-output-platform-project", "myProject.psprjx", "make", "-f", makefilePath, makefileArgument, "-B"])Load the project
myProject.psprjxin apolyspace.project.Projectobject.proj = polyspace.project.Project("myProject.psprjx")
Append Additional Options
If your system has special build requirements, you can append additional options to the generated build configuration.
Suppose that, for conditional compilation of your code, you want to mark the preprocessor macro VARIANT_1 as defined. Use the Defines.append method to add this definition to your active build configuration. To update the myProject.psprjx file, save the project.
proj.ActiveBuildConfiguration.Defines.append("VARIANT_1")
proj.save()Add Tests
To test your source code, add reusable graphical tests to the dynamically generated project:
You can add graphical tests to the project object
projusing the Python API for Polyspace. See Author Graphical Tests Using Python API for Polyspace.Once you add new graphical tests, call the
savemethod to update themyProject.psprjxfile. Then export the tests to.pstestdfiles so that they are available even after you discard the dynamically generated project file.If you already have graphical tests in existing
.pstestdfiles that your source code must pass, add references to those.pstestdfiles inmyProject.psprjx.
Build and Run Tests
Build and run the tests in myProject.psprjx in the Polyspace Platform user interface, or by using the Python API as follows:
res = polyspace.test.build(proj)See Also
polyspace.project.Project | polyspace.__install_path__ | polyspace.test.run | polyspace-configure