Unit Tests - November 10, 2019
lib7842 is a library that will be released to many teams around the world. In order to make it as bug-free and reliable as possible, all classes have tests written for them. These tests check to make sure all the code is working as expected, and that there are no crashes or bugs.
Tests are run by Azure Pipelines CI. Every commit I make on GitHub gets processed and tested in the cloud, with the results reported back to me. The automated process also runs static analysis via CppCheck, which can find bugs in my code. Finally I run valgrind and callgrind, a memory leak and profiler tool.
Tests
I use a testing framework called doctest. It allows me to write expressive tests through the TDD (Test Driven Development) workflow.
Here is an example test:
GIVEN("a data point with some data") {
DataPoint point1 {5_in, 3_in};
point1.setData("curvature", 5.0);
point1.setData("distance", 5_m);
point1.setData("velocity", 5_mps);
point1.setData("segmentIndex", 5);
THEN("constructors should work") {
DataPath();
DataPath({point1});
DataPath({point1, point1});
DataPath(std::vector<DataPoint>({point1, point1}));
}
GIVEN("a path containing three points") {
DataPath path({point1, point1, point1});
THEN("the size of the path should be three") {
REQUIRE(path().size() == 3);
}
THEN("each point should contain data") {
for (auto&& point : path()) {
CHECK(point->getData<double>("curvature") == 5.0);
CHECK(point->getData<QLength>("distance") == 5_m);
CHECK(point->getData<QSpeed>("velocity") == 5_mps);
CHECK(point->getData<int>("segmentIndex") == 5);
}
}
GIVEN("a simple path generated from the data path") {
SimplePath ipath = path.generate();
THEN("the size of the path should be three") {
REQUIRE(ipath().size() == 3);
}
THEN("the positions should be the same") {
for (auto&& point : ipath()) {
CHECK(*point == point1);
}
}
}
}
}
If any of the tests fail, I will be notified and I will be able to solve the problem. Tests have already found many bugs I would have missed, and is a great way to test code without needing a robot.