gtest C++单元测试框架,由google公司发布。
断言
//bool值检查
//期待结果是true
ASSERT_TRUE(XX)
//期待结果是false
ASSERT_FALSE(XX)
//数值型数据检查
//期待结果X1=X2
ASSERT_EQ(X1,X2),
//期待结果X1!=X2
ASSERT_NE(X1,X2)
//less than <
ASSERT_LT(X1,X2)
//greater than >
ASSERT_GT(X1,X2)
//less equal <=
ASSERT_LE(X1,X2)
//greater equal >=
ASSERT_GE(X1,X2)
//NULL指针和空字符串被认为是不同的
//期待字符串相等
ASSERT_STREQ(str1, str2)
//忽略大小写
ASSERT_STRCASEEQ(expected_str,actual_str);
//期待字符串不等
ASSERT_STRNE(str1, str2)
//忽略大小写
ASSERT_STRCASENE(str1, str2);
//异常抛出
ASSERT_ANY_THROW(statement);
EXPECT_系列
//程序死了并且错误信息和X2匹配,如果X2为空字符串,则只需要看程序是否死了
ASSERT_DEATH(X1,X2)
TEST()
使用TEST()宏定义和命名测试函数,无返回值
TEST(TestCaseName, TestName) {
…
}
- TestCaseName 一系列test的组合名称。
TestCaseName ==> pcmtest - TestName 具体每一个测试项
CheckAudioDir,GetSoundDevs测试函数
TEST(pcmtest, CheckAudioDir) {
pcms = getPcmNodes();
ASSERT_GT(pcms, 0);
}
TEST(pcmtest1, GetSoundDevs) {
int err = getSndDev(pcms);
ASSERT_EQ(0, err);
}
#define PCM_PREFIX "pcm"
#define MIXER_PREFIX "control"
#define TIMER_PREFIX "timer"
const char kSoundDir[] = "/dev/snd";
int getPcmNodes(void)
{
DIR *d;
struct dirent *de;
unsigned int pcount = 0;
d = opendir(kSoundDir);
if (d == 0)
return 0;
while ((de = readdir(d)) != NULL) {
if (de->d_name[0] == '.')
continue;
if (strstr(de->d_name, PCM_PREFIX))
pcount++;
}
closedir(d);
return pcount;
}
运行
Running main() from gtest_main.cc
[==========] Running 2 tests from 2 test cases.
[----------] Global test environment set-up.
[----------] 1 test from pcmtest
[ RUN ] pcmtest.CheckAudioDir
[ OK ] pcmtest.CheckAudioDir (0 ms)
[----------] 1 test from pcmtest (1 ms total)
[----------] 1 test from pcmtest1
[ RUN ] pcmtest1.GetSoundDevs
[ OK ] pcmtest1.GetSoundDevs (7 ms)
[----------] 1 test from pcmtest1 (7 ms total)
[----------] Global test environment tear-down
[==========] 2 tests from 2 test cases ran. (12 ms total)
[ PASSED ] 2 tests.
TEST_F
作用:为测试用例准备/清理数据
- 1.写一个类,继承testing::Test,然后实现两个方法:SetUp,TearDown
SetUp方法在每个TestCase之前执行;TearDown方法在每个TestCase之后执行。 - 2.定义一系列TEST_F(TestSuiteName, TestName)
class FsRecoveryTest : public ::testing::Test {
protected:
FsRecoveryTest() : fs_type(FS_UNKNOWN), blk_fd_(-1) {}
virtual void SetUp() {
...
}
virtual void TearDown() {
...
}
Fs_Type fs_type;
private:
char blk_path_[FILENAME_MAX];
int blk_fd_;
}
TestCase, TestSuite,tests 区别
- 如果自定义SetUp,TearDown
SetUp,TearDown 会在FsRecoveryTest , FsRecoveryTest1 这些testcase之前和之后运行 - 如果自定义SetUpTestCase,TearDownTestCase
那么就是在第一个testcase(FsRecoveryTest)之前运行SetUpTestCase,最后一个testcase(FsRecoveryTest1)之后运行TearDownTestCase
TEST_F(FsRecoveryTest, EXT4_CorruptGdt) {
...
}
TEST_F(FsRecoveryTest, EXT4_SetErrorBit) {
...
}
TEST_F(FsRecoveryTest1, XXX1) {
...
}
TEST_F(FsRecoveryTest1, XXX2) {
...
}
运行测试
Running main() from gtest_main.cc
[==========] Running 2 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 2 tests from FsRecoveryTest
[ RUN ] FsRecoveryTest.EXT4_CorruptGdt
[ OK ] FsRecoveryTest.EXT4_CorruptGdt (721 ms)
[ RUN ] FsRecoveryTest.EXT4_SetErrorBit
[ OK ] FsRecoveryTest.EXT4_SetErrorBit (595 ms)
[----------] 2 tests from FsRecoveryTest (1317 ms total)
[----------] Global test environment tear-down
[==========] 2 tests from 1 test case ran. (1318 ms total)
[ PASSED ] 2 tests.
来源:CSDN
作者:gbmaotai
链接:https://blog.csdn.net/gbmaotai/article/details/103693794