This week I'm going to learn GoogleTest for c++. Goals for day 3: test an existing embedded library: my GPS parser lib ( C++ parser library for NMEA GPS data - pt. 1: ideas, concepts, early design ).
I'm using the lessons learned from day 1 and 2, and apply them to test an object in my library. The class under test, nmea::gll, parses a GPS NMEA payload and writes the values to the object members.
The test class will validate that this works as expected. Useful as a plain unit test. And it will be invaluable when I want to touch this code. Rerunning the tests will show if I broke something.
I created a VSCode project, where I added the library sources to the ./source folder (actually using git to get that code from its github repo). The test cases are in the ./test folder.
Here is the test fixture, that 'll be used to test all cases:
#include <gtest/gtest.h> import nmea; class gllTest : public testing::Test { protected: gllTest() : parse_ok(false) {} void SetUp() override { parse_ok = nmea::gll::from_data("$GPGLL,5051.83778,N,00422.55809,S,185427.150,A,N*4F", o); } nmea::gll o; bool parse_ok; };
These are all the test cases:
TEST_F(gllTest, parse) { EXPECT_TRUE(parse_ok) << "parse failed"; } TEST_F(gllTest, valid) { EXPECT_TRUE(o.valid); } TEST_F(gllTest, source) { EXPECT_TRUE(o.source == nmea::talker_id::gps); } TEST_F(gllTest, lat) { EXPECT_FLOAT_EQ(o.lat, 50.8639641); } TEST_F(gllTest, lon) { EXPECT_FLOAT_EQ(o.lon, 4.37596798); } TEST_F(gllTest, time) { EXPECT_EQ((int)(o.t.hours().count()), 18) << "hours wrong"; EXPECT_EQ((int)(o.t.minutes().count()), 54) << "minutes wrong"; EXPECT_EQ((int)(o.t.seconds().count()), 27) << "seconds wrong"; EXPECT_EQ((int)(o.t.subseconds().count()), 150) << "subseconds wrong"; }
It's surprising how easy it is to define these tests. The test fixture, in combination with its SetUp(), takes care that each test can focus on validating one aspect of the class under test.
The library source didn't need any code change to make this work.
Output:
You can find the code on github. Check the CMake file in particular. It deals with some of the intrinsics of including GoogleTest, and dealing with potential link conflicts.
link to all posts.