使用 C/C++ xUnit API 编写测试
使用 C 或 C++ 宏编写测试、将测试分组到套件中、参数化测试
您可以使用 Polyspace® Test™ API 为 C/C++ 代码编写测试。该 API 遵循用于测试的 xUnit 框架的规范,并支持具有常见设置和拆解函数的测试用例和测试套件等功能。
Polyspace Test xUnit API 由类似函数的宏(C 和 C++)和测试类(仅 C++)组成,这些宏和类在 Polyspace Test 安装提供的文件中定义。要使用该 API 测试 C/C++ 代码,请执行以下操作:
编写调用这些宏或从这些类继承的测试。
通过使用现有工具链编译测试和源代码以及 Polyspace Test 宏或类定义文件。您也可以将源代码和测试添加到 Polyspace 平台工程中,并通过该工程编译源代码和测试并进行执行。
系统命令
polyspace-test -generate | (System Command) Generate test registration function from a set of Polyspace Test C/C++ test files |
主题
快速入门
- Set Up C/C++ Testing and Code Profiling Using Self-Managed Builds
Define environment variables that point to locations of Polyspace Test files required for compiling xUnit tests or code profiling macros. - 使用 Polyspace Test xUnit API 编写 C/C++ 测试并通过命令行运行测试
使用 Polyspace Test API 中的预定义宏编写基于 xUnit 的 C/C++ 测试快速入门。
创建测试
- Writing Tests Using Polyspace Test xUnit API for C/C++ Code
Look up the various test creation macros that you can use in tests written with the Polyspace Test API. - Configuration Macros in Polyspace Test API for C/C++ Code
Look up the configuration macros that you can use to configure the behavior of the test executable. - Write C/C++ Tests with Known Errors or Failures to Support Test-Driven Development
Write tests with expected failures or errors by including specific build-configuration-dependent macros.
编写评估
- Assessment Macros in Polyspace Test API for C/C++ Code
Look up the various assessment macros that you can use in tests written with the Polyspace Test API. - Choose Assessment Macro Variant in Polyspace Test xUnit API
Choose betweenPST_VERIFY,PST_VERIFY_EQandPST_VERIFY_EQ_MSGmacros.
测试分组
- Group C/C++ Tests into Suites with Common Setup and Teardown Code
Avoid duplication and improve consistency in test setup with setup code that runs for all tests in a suite. - Write Test Fixtures Using Polyspace Test C++ API
Use a test fixture to share test context between tests.
测试参数化
- Write Parameterized Tests for C/C++ Functions Using Polyspace Test xUnit API
Test a function for several combinations of input values. - Test Multi-Input C/C++ Functions Using Multiple Parameters and Parameter Combination Strategies
Test multi-input functions using parameterized tests with multiple parameters, run through parameter value combinations sequentially or exhaustively.
范围设定
- Range Specification Macros in Polyspace Test xUnit API
Look up the various range specification macros that you can use in tests written with the Polyspace Test API. - Test Functions and Constrain Polyspace Code Prover Analysis for Ranges of Inputs and Outputs
Test functions over one or more ranges of input and verify if the output is within a valid range using range specification macros. Run static analysis on the function and associated tests using the specified ranges as external constraint. - Test Functions That Operate on Arrays Using Range Specification Macros
Create array inputs for functions that operate on arrays and test if elements of the output array are within a specified range.
添加到 Polyspace 平台工程
- 在 Polyspace 平台用户界面中向工程添加 C/C++ xUnit 测试
使用 Polyspace Test xUnit API 编写测试并从 Polyspace 平台用户界面执行这些测试。 - 在 Polyspace 平台用户界面中将 C/C++ 测试链接到需求
创建从测试和测试步骤到在外部编写的需求的链接。