Convert GoogleTest Tests to Polyspace Test xUnit Tests
In the Polyspace Platform user interface, you can run C/C++ tests written using the Polyspace® Test™ xUnit framework or an external framework such as GoogleTest. You can review the test results directly in the user interface and collect code coverage with respect to these tests. In addition, if you have existing tests authored using GoogleTest, you can also migrate them to Polyspace Test xUnit tests. Migrating to xUnit tests unlocks additional features in Polyspace Platform such as ability to execute individual tests, on-target execution, and links from coverage results to test information.
This topic shows how to migrate existing tests authored using GoogleTest to the Polyspace Test xUnit framework.
Example Files
Find the files for this tutorial in the folder . Copy these files to a writable location and continue the tutorial. Here, polyspaceroot\polyspace\examples\doc_pstest\google_test is the Polyspace installation folder, for instance, polyspacerootC:\Program Files\Polyspace\R2026a.
The example folder contains the following:
Source file
hUserCode.cpp, which contains two functions,return_zero()andtested_function().Header file
hUserCode.hpp, which contains declarations of the above functions.Test file
hGoogleTest.cpp, which contains tests for the functiontested_function().
To run tests authored using the GoogleTest framework, you also need GoogleTest includes and libraries. You can install all required files from the GoogleTest repository. In this topic, the term refers to the main folder containing GoogleTest files. For instance, if you run this
command:googleTestInstallFolder
git clone https://github.com/google/googletest.git -b v1.13.0
googletests subfolder. In this case, googleTestInstallFolder is the full path to the googletests subfolder.Build and Run GoogleTest Tests in Polyspace Platform Project
Before converting GoogleTest tests to xUnit tests, you must run the original tests to completion using a Polyspace Platform project. The converter leverages information from the project, in particular, the project build configuration, to identify GoogleTest macros and convert them to equivalent Polyspace Test macros.
In brief, the steps to run GoogleTest tests from a Polyspace Platform project are as follows:
Create a new project and add source files to the project.
Right-click the root node of the project and select Add External Test Files or Add External Test Folders to add GoogleTest tests to the project.
Open the project configuration. On the Build tab, set appropriate options on the External Tests node (in addition to other options you might need to set).
Build the tests in the project.
For more details on these steps, see Run GoogleTest Tests Using Polyspace Platform Projects.
Convert GoogleTest Tests in Project
Once the GoogleTest tests build successfully, you can convert the tests in the project.
Open a terminal and navigate to the project file (
.psprjxfile) location.Enter the following command to convert the tests in the project:
Here,polyspace-test -convert-external-tests -project projectName.psprjx
is the name of the project file.projectNameThis command attempts to convert all GoogleTest tests in the project to Polyspace Test xUnit tests. After conversion, for each GoogleTest test file
, you see an equivalent Polyspace Test xUnit test filetestFile.cppin the same folder as the original test. For more information on the command syntax, enter:testFile.pstest.cpppolyspace-test -convert-external-tests -help
If a macro cannot be automatically converted, the command prints the location of the macro so that you can convert these macros manually later. At the end of the conversion log, you see a message such as the following:
You can use this information to check if all files in the project were analyzed and converted, or if some files were skipped because of compilation errors.Analyzed files: 1 Converted files: 1 Files skipped because of errors: 0 Files not requiring conversion: 0Add the converted tests to the Polyspace Platform project (and optionally remove the GoogleTest tests). When you build and run the tests, you should see the same results as the original GoogleTest tests, with additional capabilities such as ability to execute a single test, and so on.