主要内容

Generate C/C++ Tests from Requirements

R2026b
Since R2026b

If you have a requirements set created in the Requirements Editor and you link functions in your C/C++ source code to the requirements, you can generate tests for those functions. You can generate either a starter test and fill in the inputs and assessments manually, or you can generate a functional test using Polyspace® Copilot based on the requirement text.

This topic outlines the steps for generating C/C++ tests from requirements in the Requirements Editor and verifying requirements by running the generated tests.

Example Files

To follow the steps in this tutorial, load a demo project in the Polyspace Platform user interface:

  1. On the Start Page pane in the Polyspace Platform user interface, in the Examples section, click the link Create and run functional tests.

    This action loads the example project, Demo_C_PS_Test.psprjx, on the Projects pane.

  2. On the toolstrip, select Parse Code.

In this tutorial, you generate a test for the function checkAgainstSpeedLimit(uint32_t, uint32_t) from a requirement.

Link C/C++ Function in Source to Requirement

Link the function checkAgainstSpeedLimit(uint32_t, uint32_t) to a requirement in the Requirements Editor. To create this link:

  1. On the Polyspace Platform toolstrip, select Requirements Editor to open the Requirements Editor interface.

  2. Double-click checkAgainstSpeedLimit in the project to open the function definition. Select the function name in the function definition.

  3. Right-click a requirement in the requirements set in the Requirements Editor interface and select Link from selected Polyspace Test element.

    Content of an example requirement in the Requirements Editor.

    After you create the link, the Implemented column becomes green for the linked requirement, indicating that the requirement has a source implementation link.

    The Implemented column shows green status for the linked requirement.

    When you select the requirement, the Links section on the Properties pane shows that the requirement is implemented by the function.

    The Links section on the Properties pane shows the requirement is implemented by the function.

For more information on linking sources and tests to requirements, see Link C/C++ Sources and Tests to Requirements in Requirements Toolbox (Polyspace Test).

Generate Starter Test from Requirement

After linking the function to a requirement, you can generate a starter test directly from the requirement:

  1. In the Requirements Editor, select the linked requirement.

  2. Select Add Test > Generate a starter test for the selected requirement.

    Add Test menu with option to generate a starter test for the selected requirement.

    This action creates a starter test. The Verified column, which was previously empty, is now orange.

    The Verified column shows orange status after generating a starter test.

    The Links section shows a Verified by entry followed by the test name.

    The Links section shows the requirement is verified by the generated test.

  3. In the Polyspace Platform user interface, you see the newly added test with a default name Test in the default suite pst_default_suite.

  4. Rename the test, and add input and assessment values as usual. Generating a starter test from the Requirements Editor interface has the same result as right-clicking a function in the Polyspace Platform user interface and selecting Add Test Case.

    For more information on naming tests and adding inputs and assessments, see Write C/C++ Unit Tests in Polyspace Platform User Interface (Polyspace Test).

Generate Copilot Test from Requirement

Before generating a test using Polyspace Copilot, ensure that you have test generation using Polyspace Copilot set up and working in the Polyspace Platform user interface. For setup steps, see Generate Functional Tests Using Polyspace Copilot (Polyspace Test).

To generate a test using Polyspace Copilot from a requirement:

  1. In the Requirements Editor, select the linked requirement.

  2. Select Add Test > Generate a test using Copilot for the selected requirement.

    A message appears stating Test generation started in Polyspace Copilot Chat.

  3. Follow the progress of test generation in the Polyspace Copilot Chat window in the Polyspace Platform user interface.

    Once the test generation completes, you see a test in the default suite pst_default_suite that is ready to run. The Verified column, which was previously empty, is now orange.

    The Verified column shows orange status after generating a starter test.

    The Links section shows a Verified by entry followed by the test name.

    The Links section shows the requirement is verified by the generated test.

  4. Review the generated tests before use, and ensure that the tests exercise the functional behavior that you want to qualify.

    Polyspace Copilot attempts to generate a test that verifies the requirement without any additional input from you. However, the generated tests might contain errors or incorrect code and might not correctly exercise the source code that implements the requirement.

  5. To run the test from the requirement, right-click the requirement and select Run Tests.

    After a successful run, the Verified column is green. For information on running tests from requirements, see Link C/C++ Sources and Tests to Requirements in Requirements Toolbox (Polyspace Test).

See Also

Apps

Topics