Skip to content

4. Unit Tests

Jinfeng-Lyu edited this page Apr 16, 2023 · 39 revisions
#install gtest
git clone https://github.com/google/googletest
cd googletest
mkdir build
cd build
cmake ..
make
sudo make install

Basic concepts about googletest

In this section, we use googletest as the tool of unit test. The installation source is from this website: https://github.com/google/googletest. The function of googletest is to use assertions to check whether the output value of a programme is corresponded to the expected value. If does not correspond, the system will report a failure. There are two kinds of assertions, namely ASSERT and EXPECT. The former can produce a serious failure if detects an error in the programme, then stop the running of the programme. The latter can generate a slight failure and do not stop the program even if a mistake detected.

The commands in googletest are as follow: ASSERT_TRUE(condition_1), or EXPECT_TURE(condition_1): judge whether condition_1 is true. ASSERT_EQ(value_1,value_2), or EXPECT_EQ(value_1,value_2): judge whether value_1 equals to value_2. ASSERT_NE(value_1,value_2), or EXPECT_NE(value_1,value_2): judge whether value_1 inequals to value_2. ASSERT_LT(value_1,value_2), or EXPECT_LT(value_1,value_2): judge whether value_1 less than value_2. ASSERT_LE(value_1,value_2), or EXPECT_LE(value_1,value_2): judge whether value_1 less than or equal to value_2. ASSERT_GT(value_1,value_2), or EXPECT_GT(value_1,value_2): judge whether value_1 greater than value_2. ASSERT_GE(value_1,value_2), or EXPECT_GE(value_1,value_2): judge whether value_1 greater than or equal to value_2.

To try the viability of this test, we defined two functions, one calculates the absolute value of a number, another figures out the cube of a number.the codes are as follow:

image

Then we typed several googletest commands as follow:

image

In this case, there is no failure, the result is shown as follow:

image

To test the serious failure of the command ASSERT*, we changed all the commands in the test for the cube function into mistake, and all of them use ASSERT*, but remain all the commands in the test for absolute value correct:

image

After running, the results reveal as follow:

image

Obviously, there should be five failures in the test for cube, but the system only reports one failure, namely ASSERT_EQ(cube(2),7). This means it stops the test for this function after detecting one failure. However, the test for absolute value remains "OK", which means the failure in testing one function does not affect another function's test.

Then we changed all these commands into EXPECT*:

image

In this case, the program is not stopped by failure, and all of these failures are detected, as shown in the following:

image

Googletest items in this project

There are two tests in this projects, namely SetDeviceTest and WS2812BTest, the purpose of the former is to ensure the audio input has been initialized, and the latter to make sure that the LED strip has been initialized and that all the three modes of updating the LED strip can work normally as expected.

After running these two tests, the results is revealed as follow: 5aebe1f68bc3b5f5661f9e64d657caa b5596df083bfc68a50029be8c6bdee3

According to this result, SetDeviceTest succeeds, but WS2812BTest fails. The reason is that the permission has not been granted. After giving the system a permission (localuser:root), the tests succeed as shown in the following picture: 13746b19f953bed8d7649bd7bc945e6

Clone this wiki locally