GoogleTest 中是否有类似的内容:
ASSERT_EQ_ONE_OF_TWO(TestValue, Value1, Value2)
Run Code Online (Sandbox Code Playgroud)
哪个测试如果TestValue == Value1 || TestValue == Value2?
这个变体:
ASSERT_TRUE(TestValue == Value1 || TestValue == Value2)
Run Code Online (Sandbox Code Playgroud)
可以,但是TestValue如果失败,它不会在日志中显示哪个值。
GoogleTest 中有类似的东西吗
我想不是。
可以,但是如果失败,它不会在日志中显示 TestValue 的值。
您可以添加附加日志信息,如下所示:
TEST (ExampleTest, DummyTest)
{
// Arrange.
const int allowedOne = 7;
const int allowedTwo = 42;
int real = 0;
// Act.
real = 5;
// Assert.
EXPECT_TRUE (real == allowedOne || real == allowedTwo)
<< "Where real value: " << real
<< " not equal neither: " << allowedOne
<< " nor: " << allowedTwo << ".";
}
Run Code Online (Sandbox Code Playgroud)
这段代码在失败时会产生以下日志:
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from ExampleTest
[ RUN ] ExampleTest.DummyTest
/home/gluttton/ExampleTest.cpp:13: Failure
Value of: real == allowedOne || real == allowedTwo
Actual: false
Expected: true
Where real value: 5 not equal neither: 7 nor: 42.
[ FAILED ] ExampleTest.DummyTest (0 ms)
[----------] 1 test from ExampleTest (0 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (0 ms total)
[ PASSED ] 0 tests.
[ FAILED ] 1 test, listed below:
[ FAILED ] ExampleTest.DummyTest
Run Code Online (Sandbox Code Playgroud)
我刚刚找到了一个更好的解决方案,我想分享,但Andre Holzner已经在这个答案中提出了它。
该解决方案具有我的所有优点,但没有缺点。而且它也更灵活,因为它支持匹配器,所以你可以做这样的事情:
EXPECT_THAT(someString, AnyOf(StartsWith("foo"), StartsWith("bar")));
Run Code Online (Sandbox Code Playgroud)
这将给出与此类似的输出(使用GTest 1.10创建)
error: Value of: someString
Expected: (starts with "foo") or (starts with "bar")
Actual: "baz"
Run Code Online (Sandbox Code Playgroud)
您可以EXPECT_THAT()结合使用容器和Contains()匹配器来实现此目的:
EXPECT_THAT((std::array{ Value1, Value2 }), Contains(TestValue));
Run Code Online (Sandbox Code Playgroud)
array请注意,列表初始化需要后面的大括号,并且array需要周围的括号,因为宏(例如EXPECT_THAT())不理解大括号,否则会将两个参数解释为三个参数。
这将给出与此类似的输出(使用GTest 1.10创建)
error: Value of: (std::array{ Value1, Value2 })
Expected: contains at least one element that is equal to 42
Actual: { 12, 21 }
Run Code Online (Sandbox Code Playgroud)
专业人士:
缺点:
TestValue并不容易std::array而需要 C++11 (仍然不是在所有地方都可用)std::initializer_list<T>{ ... }在 C++11/14 上使用。或者,可以使用自由函数来推断数组的大小和类型。| 归档时间: |
|
| 查看次数: |
11038 次 |
| 最近记录: |