Workspace 6.21.5
Writing a Unit Test - looking at the code

In this section, we're going to understand the code that was automatically generated by Workspace so that we know how to customise our unit test.


CMakeLists.txt

This file is created or updated automatically by the wizard when you add a new test to the directory. It purpose is to tell the compiler to compile the added tests and to link with the dependent libraries.

find_package(WorkspaceTest)
add_workspace_test(test_calculaterectanglearea QTEST LIBRARIES simpleplugin workspace)
Workspace * workspace
Definition: mongodbremotescheduler.cpp:171

test_calculaterectanglearea.cpp

The test_calculaterectanglearea.cpp file contains the test implementation. Except for initTestCase and a few other methods whose names are reserved, all private slots is a test function and will be run in the order of their definition. In our file, we only have one such function: testRunnerScenario1.

Let us take a look at the included header files section, we can see that calculaterectanglearea.h is already included by the Wizard.

Let us now examine the body of the test class. As its name suggests, initTestCase will be called before the first test function is executed to initialize the test class. We use it to initialize the class members.

private slots:
void initTestCase()
{
// Do the plugin manager setup first to make test output cleaner
Settings::getInstance().setPluginAutoLoadPolicy(Settings::AutoLoadNoPlugins);
PluginManager::getInstance();
}

The wizard also creates an initial test function testRunnerscenario1, that we can modfiy to do something useful. We can add any number of similar test functions and they will be run one after the other.