Workspace 6.21.5
|
Testing is an important component of software development. When you add a new functionality such as an operation to the plugin, you may want to create a unit test to keep a record of some relevant use cases: what output value is expected when given some specific input values. The unit tests also make it easier for any person who wants to check - at a later time - if any new changes to the code has broken the original use cases, without having to read through the code of the operation to figure them out. This is called a regression test and aims at detecting any backward incompatibilities introduced by the new changes.
Assuming we want to write a unit test for the CalculateRectangleArea operation, we can use the Create Test wizard to generate the test source code, include the correct file headers and update the CMakeLists.txt to add our test. To start the wizard:
Let's start by filling in the fields on the form:
Our form will now look similar to this:
You will now see a screen showing a list of plugins that can be linked to your test.
If your unit test depends on code in any of the listed plugins, you will need to select it here. In this tutorial:
The next screen shows copyright notices that you can include in each of the source files in your plugin.
Workspace will now generate the code for our new test and place it in the directory that we selected earlier ({PluginDir}/Tests). If you navigate to this directory, you'll see that it has created or updated the following files:
In this section, we're going to customise our unit test. To do this, we need to modify the new file test_calculaterectanglearea.cpp
Let us take a look at the included header files section, we can see that calculaterectanglearea.h is already included by the Wizard. Since we also use the Rectangle data type in the test, we must include its header here manually:
The wizard automatically generates two test functions for us: initTestCase() and testRunnerscenario1(). We are going to leave initTestCase() as it is, and modify testRunnerscenario1() so that it does something useful.
In our test function testRunnerscenario1, we begin by creating an operation CalculateRectangleArea.
We can then populate the operation inputs with our test values by adding the following lines of code
and execute the operation with
Finally, we check that the operation produces the expected output by adding the following lines of code
Ensure that you have enabled the BUILD_TESTING checkbox in your makefile as this line will tell the build tool to also build all tests when it builds our plugin.
Now we can initiate the build command, for example in Linux: navigate to the plugin build directory and run make.
Once built, you should see subdirectory Tests in the plugin build directory. Inside Tests there is an executable file called test_calculaterectanglearea. This is our unit test ready to be executed. You cannot run this file directly, as the environment will not be set up properly, but through workspace-batch.exe. The most convenient way do this is to add both the Workspace bin and your test bin directories to your path first. See Running the workflow from the command line . Otherwise, navigate to your application bin directory. Depending on your installation directory, under Windows you might run:
"C:\Program Files\csiro.au\workspace\bin\workspace-batch.exe" –launch test_calculaterectanglearea.exe
The execution of the test should produce this output
********* Start testing of CSIRO::Testing::TestCalculateRectangleArea ********* Config: Using QtTest library 5.12.5, Qt 5.12.5 (x86_64-little_endian-lp64 shared (dynamic) release build; by MSVC 2017) Adding Built-in version 6.0.0 PASS : CSIRO::Testing::TestCalculateRectangleArea::initTestCase() PASS : CSIRO::Testing::TestCalculateRectangleArea::testRunnerScenario1() PASS : CSIRO::Testing::TestCalculateRectangleArea::cleanupTestCase() Totals: 3 passed, 0 failed, 0 skipped, 0 blacklisted ********* Finished testing of CSIRO::Testing::TestCalculateRectangleArea *********
which means all methods of the test class have run successfully, in particular testRunnerScenario1.
This tutorial has presented the Create Test wizard and the test code, how to write a test, compile and run it.
The following tutorials are suggested as next steps: