-
Notifications
You must be signed in to change notification settings - Fork 3
4. Unit Tests
#install gtest
git clone https://github.com/google/googletest
cd googletest
mkdir build
cd build
cmake ..
make
sudo make install
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:

Then we typed several googletest commands as follow:

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

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:

After running, the results reveal as follow:

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*:

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

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:

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:
