Skip to content

4. Unit Tests

Jinfeng-Lyu edited this page Apr 13, 2023 · 39 revisions
  1. Unit Tests

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: #include #include "gtest/gtest.h"

int cube(int a) {return aaa;} int abs(int x) {return x>0 ? x : -x;}

Then we typed several googletest commands as follow: TEST(TestCube, JudgeTrueOrFalse) { EXPECT_EQ(cube(2),8); ASSERT_NE(100,cube(5)); ASSERT_LT(300,cube(8)); EXPECT_LE(125,cube(5)); ASSERT_GE(1000,cube(10)); }

TEST(TestAbs,JudgeTrueOrFalse) { ASSERT_TRUE(abs(2)==2); ASSERT_TRUE(abs(-5)==5); ASSERT_FALSE(abs(-3)==-3); EXPECT_EQ(abs(-3),abs(3)); EXPECT_NE(abs(0),1); ASSERT_GT(abs(-5),0); ASSERT_LT(abs(4),6); ASSERT_LE(-2,abs(-3)); ASSERT_GE(abs(-1),0); } int main() { ::testing::InitGoogleTest(); return RUN_ALL_TESTS(); }

In this case, there is no failure, the result is shown as follow: [==========] Running 2 tests from 2 test suites. [----------] Global test environment set-up. [----------] 1 test from TestCube [ RUN ] TestCube.JudgeTrueOrFalse [ OK ] TestCube.JudgeTrueOrFalse (0 ms) [----------] 1 test from TestCube (2 ms total)

[----------] 1 test from TestAbs [ RUN ] TestAbs.JudgeTrueOrFalse [ OK ] TestAbs.JudgeTrueOrFalse (0 ms) [----------] 1 test from TestAbs (2 ms total)

[----------] Global test environment tear-down [==========] 2 tests from 2 test suites ran. (12 ms total) [ PASSED ] 2 tests.

Process finished with exit code 0

To test the serious failure of the command ASSERT*, we changed all the commands in the test for the cube function into mistake: TEST(TestCube, JudgeTrueOrFalse) { ASSERT_EQ(cube(2),7); ASSERT_NE(125,cube(5)); ASSERT_LT(600,cube(8)); ASSERT_LE(126,cube(5)); ASSERT_GE(998,cube(10)); }

Clone this wiki locally