These assertions do basic true/false condition testing.
Fatal assertion | Nonfatal assertion | Verifies |
---|---|---|
ASSERT_TRUE(condition); | EXPECT_TRUE(condition); | condition is true |
ASSERT_FALSE(condition); | EXPECT_FALSE(condition); | condition is false |
This section describes assertions that compare two values.
Fatal assertion | Nonfatal assertion | Verifies |
---|---|---|
ASSERT_EQ(val1, val2); | EXPECT_EQ(val1, val2); | val1 == val2 |
ASSERT_NE(val1, val2); | EXPECT_NE(val1, val2); | val1 != val2 |
ASSERT_LT(val1, val2); | EXPECT_LT(val1, val2); | val1 < val2 |
ASSERT_LE(val1, val2); | EXPECT_LE(val1, val2); | val1 <= val2 |
ASSERT_GT(val1, val2); | EXPECT_GT(val1, val2); | val1 > val2 |
ASSERT_GE(val1, val2); | EXPECT_GE(val1, val2); | val1 >= val2 |
现在直接要么用Test(),要么用Test_F(),这个F的函数可以在SetUp调用里共享数据,但只是最开始执行同样的共享操作,对每个suit里的test是独立的
Test返回都是void,把要带的值放到形参里,用指针指一下
看到定制<<操作符了,直接往流里输入一个实例就完事了
看到Regular Expression Syntax了,正则的语法
Expression | Meaning |
---|---|
c | matches any literal character c |
\\d | matches any decimal digit |
\\D | matches any character that’s not a decimal digit |
\\f | matches \f |
\\n | matches \n |
\\r | matches \r |
\\s | matches any ASCII whitespace, including \n |
\\S | matches any character that’s not a whitespace |
\\t | matches \t |
\\v | matches \v |
\\w | matches any letter, _ , or decimal digit |
\\W | matches any character that \\w doesn’t match |
\\c | matches any literal character c , which must be a punctuation |
. | matches any single character except \n |
A? | matches 0 or 1 occurrences of A |
A* | matches 0 or many occurrences of A |
A+ | matches 1 or many occurrences of A |
^ | matches the beginning of a string (not that of each line) |
$ | matches the end of a string (not that of each line) |
xy | matches x followed by y |
可以在代码里指定是在windows下跑还是Linux下跑,windows下的是simple的re
GTEST_USES_SIMPLE_RE=1
or GTEST_USES_POSIX_RE=1
.
给输出信息加个作用范围,可以让输出信息不再杂乱无章
SCOPED_TRACE(message);//SCOPED_TRACE("This is first Test");
他妈的在测试函数内调用了一个子函数,子函数报致命错误返回了,还他妈会接着执行下去,醉了,还得传播致命错误
- 可以加一个监听器OnTestPartResult中判断是不是致命错误,是就抛出错误
- 在类中加个静态方法
- static bool HasFatalFailure();
- 函数返回的时候if (testing::Test::HasFatalFailure()) return;
- HasNonfatalFailure
- HasFailure
- static bool HasFatalFailure();
这个logging additional information加在xml里,可我能拿来做什么呢?为什么用呢?无解
RecordProperty("MaximumWidgets", ComputeMaxUsage());
在一组测试中共享数据,跟相同组测试共享数据类似SetUp(),TearTown()
先把变量声明成static的,在类外赋值成0的或nullptr
然后定义static void SetUpTestSuite(),在里面放变量的赋值
在static void TearDownTestSuite()里放变量的析构
怎么那么多重载SetUp和TearDown的,太free白给了
啊,我看到Value-Parameterized Tests,好东西啊,相同测试,只是参数变了,不用复制粘贴了
-
testing::TestWithParam
-
// Inside a test, access the test parameter with the GetParam() method 用GetParam()方法拿测试输入的参数
-
INSTANTIATE_TEST_SUITE_P 用来传输你要设的一系列参数 这个作用于指定的整个test suit,这么说吧,这玩意放在函数外,命名空间内,作用于命名空间内所有的 同一个suit内的test,不管其放于 INSTANTIATE_TEST_SUITE_P 前或者后
-
GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(FooTest); //用来抑制检查,没太懂.先放着,概念太多了
-
-
或者用
- testing::Test
- testing::WithParamInterface
Test Suit 和Test的名字不要包含下划线(会被moc替代然后出错的)
测试的东西相同,只是继承了接口,其类型不同而已,这里可以不用全部实现其所有了,又可以偷懒了
template <typename T>
class FooTest : public testing::Test {
public:
...
using List = std::list<T>;
static T shared_;
T value_;
};
using MyTypes = ::testing::Types<char, int, unsigned int>;
TYPED_TEST_SUITE(FooTest, MyTypes);
TYPED_TEST(FooTest, DoesBlah) {
// Inside a test, refer to the special name TypeParam to get the type
// parameter. Since we are inside a derived class template, C++ requires
// us to visit the members of FooTest via 'this'.
TypeParam n = this->value_;
// To visit static members of the fixture, add the 'TestFixture::'
// prefix.
n += TestFixture::shared_;
// To refer to typedefs in the fixture, add the 'typename TestFixture::'
// prefix. The 'typename' is required to satisfy the compiler.
typename TestFixture::List values;
values.push_back(n);
...
}
TYPED_TEST(FooTest, HasPropertyA) { ... }
我靠,看炸了,实操
例子7没看懂,那个void SetUp() override { table_ = (*GetParam())(); }是做甚的
例子8是用来组合标志位来测试的,一个测试使用一个组合标志来测试
//解包参数,6666
bool force_on_the_fly;
int max_precalculated;
std::tie(force_on_the_fly, max_precalculated) = GetParam();
第9个例子是用来用反射输出结果到黑框框中的
- 为什么用反射?因为可以枚举所有的测试case并进行测试
- 可以定制输出,比如带彩色的提示信息
第10个例子检查的是指针没释放的,内存泄露??
搞什么监听啊,搞得我一脸蒙逼