Skip to content
mshafer edited this page Aug 27, 2014 · 9 revisions

Testing Frameworks

gtest - for testing plain old C++ code
rostest - for testing behaviour that requires ROS (e.g. publishing/subscribing)

gtest

Any behaviour that does not require ROS should be tested using the gtest framework. If ROS is required, see rostest below.

Create a new test file

A new test file should be created for each major component of the system.

Assuming we have the files:

elderly_care_simulation/include/Assistant.h
elderly_care_simulation/src/Assistant.cpp

We can test the doubleEquals function in Assistant.cpp by making the following test file. Note that methods on a class instance will be accessed slightly differently to a function in a plain executable.

test/TestAssistant.cpp
#include "Assistant.h"
#include <gtest/gtest.h>

TEST(DoubleEqualsTest, returnTrue)
{
    EXPECT_TRUE(doubleEquals(0.0, 0.0, 0.05));
}

TEST(DoubleEqualsTest, returnFalse)
{
    EXPECT_FALSE(doubleEquals(-1.2, 1.2, 1));
}

// Run all the tests that were declared with TEST()
int main(int argc, char **argv){
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

Then add the following lines to CMakeLists.txt:

# Register the new test file
rosbuild_add_gtest(test/TestAssistant test/TestAssistant.cpp)

# Ensure that doubleEquals in Assistant can be used from TestAssistant
rosbuild_add_library(AssistantLib src/Assistant.cpp)
target_link_libraries(test/TestAssistant AssistantLib)

Run the tests

./scripts/test.sh

Note:

  • As of 19/08/14 this file is not yet on develop. You can find it on feature/navigation-tests
  • Ignore any errors during the build stage.

rostest

Any behaviour that requires ROS functionality should be tested using rostest.

Concept

With rostest, your gtest test files are launched as ROS nodes alongside any other nodes you specify. With this you can do things like:

  • Send messages to a node and test how it responds
  • Subscribe to messages from a node
  • Use a node's services

Adding a new test

Examples:

  • This commit shows all necessary elements to add a rostest to the project.
  • TestResident.cpp is another example that calls Resident's services.
src/test/TestSomething.cpp

This is just like a regular gtest file (see above) except it registers as a ROS node and uses ROS-specific functionality. See TestScheduler.cpp for an example of this.

test/TestSomething.test

This is how you specify which nodes need to be launched during the tests.

<launch>
  <node pkg="elderly_care_simulation" type="Scheduler" name="Scheduler" />
  <test test-name="TestScheduler" pkg="elderly_care_simulation" type="TestScheduler" />
</launch>
CMakeLists.txt

Finally you need to tell ROS about these files.

# add the test executable, keep it from being built by "make all"
rosbuild_add_executable(TestScheduler EXCLUDE_FROM_ALL src/test/TestScheduler.cpp)

# Link TestScheduler against gtest
rosbuild_add_gtest_build_flags(TestScheduler)

# Make sure rostest launches test/TestScheduler.test during "make test"
rosbuild_add_rostest(test/TestScheduler.test)

Run the tests

make test
rostest test/Test<X>.test

In the make test step, ignore where it says tests are failing. The tests run correctly when running rostest.

These two commands will soon be bundled up into scripts/test.sh

Clone this wiki locally