作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想应用参数化测试并具有以下夹具:
class MyTest: public ::testing::TestWithParam<float> {...};
我想设置两个参数化测试用例,其中一个小参数失败,但大参数成功。
TEST_P(MyTest, smallParamsFail)
{
auto param = GetParam();
EXPECT_EQ(true, param<1);
}
TEST_P(MyTest, largeParamsSucceed)
{
auto param = GetParam();
EXPECT_EQ(true, param>1);
}
我现在想开始测试用例 smallParamsFail,其中一个值较小,另一个值较大。不幸的是,我只能像这样参数化整个测试:
INSTANTIATE_TEST_CASE_P(
testLargeParams,
MyTest,
::testing::Values(2.0f, 3.14f));
这将执行两个测试用例,并且显然会在 smallParamsFail 情况下崩溃。我需要的是仅在测试用例 largeParamsSucceed 上使用参数 2.0f、3.14f 实例化 MyTest。然后使用适当的参数以相同的方式启动另一个案例。
最佳答案
这在 GTest 中是不可能的,正如 yksisarvinen 的评论中已经提到的那样.
我将使用以下解决方法
class MyTestBase: public ::testing::TestWithParam<float> {
protected:
MyTestBase() {
// whatever
}
// whatever
int someParam_;
};
class MyTestForSmall: public MyTestBase {
};
class MyTestForBig: public MyTestBase {
};
TEST_P(MyTestForSmall, smallParamsFail) {
auto param = GetParam();
EXPECT_EQ(true, param<1);
}
TEST_P(MyTestForBig, largeParamsSucceed) {
auto param = GetParam();
EXPECT_EQ(true, param>1);
}
TEST_P(MyTestBase, someGeneralTest) {
auto param = GetParam();
EXPECT_TRUE(true);
}
INSTANTIATE_TEST_CASE_P(
testSmallParams,
MyTestForSmall,
::testing::Values(0.1, 0.2));
INSTANTIATE_TEST_CASE_P(
testLargeParams,
MyTestForBig,
::testing::Values(2.0f, 3.14f));
INSTANTIATE_TEST_CASE_P(
allKindsOfParams,
MyTestBase,
::testing::Values(0.1, 0.2, 2.0f, 3.14f));
在基类中,您可以设置所有必要的环境,并仅使用子类进行测试参数分离。
可能的输出:
[----------] 2 tests from testSmallParams/MyTestForSmall
[ RUN ] testSmallParams/MyTestForSmall.smallParamsFail/0
[ OK ] testSmallParams/MyTestForSmall.smallParamsFail/0 (0 ms)
[ RUN ] testSmallParams/MyTestForSmall.smallParamsFail/1
[ OK ] testSmallParams/MyTestForSmall.smallParamsFail/1 (0 ms)
[----------] 2 tests from testSmallParams/MyTestForSmall (0 ms total)
[----------] 2 tests from testLargeParams/MyTestForBig
[ RUN ] testLargeParams/MyTestForBig.largeParamsSucceed/0
[ OK ] testLargeParams/MyTestForBig.largeParamsSucceed/0 (0 ms)
[ RUN ] testLargeParams/MyTestForBig.largeParamsSucceed/1
[ OK ] testLargeParams/MyTestForBig.largeParamsSucceed/1 (0 ms)
[----------] 2 tests from testLargeParams/MyTestForBig (0 ms total)
[----------] 4 tests from allKindsOfParams/MyTestBase
[ RUN ] allKindsOfParams/MyTestBase.someGeneralTest/0
[ OK ] allKindsOfParams/MyTestBase.someGeneralTest/0 (0 ms)
[ RUN ] allKindsOfParams/MyTestBase.someGeneralTest/1
[ OK ] allKindsOfParams/MyTestBase.someGeneralTest/1 (0 ms)
[ RUN ] allKindsOfParams/MyTestBase.someGeneralTest/2
[ OK ] allKindsOfParams/MyTestBase.someGeneralTest/2 (0 ms)
[ RUN ] allKindsOfParams/MyTestBase.someGeneralTest/3
[ OK ] allKindsOfParams/MyTestBase.someGeneralTest/3 (0 ms)
[----------] 4 tests from allKindsOfParams/MyTestBase (0 ms total)
关于c++ - GTest,仅参数化测试用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52666475/
我最近购买了《C 编程语言》并尝试了 Ex 1-8这是代码 #include #include #include /* * */ int main() { int nl,nt,nb;
早上好!我有一个变量“var”,可能为 0。我检查该变量是否为空,如果不是,我将该变量保存在 php session 中,然后调用另一个页面。在这个新页面中,我检查我创建的 session 是否为空,
我正在努力完成 Learn Python the Hard Way ex.25,但我无法理解某些事情。这是脚本: def break_words(stuff): """this functio
我是一名优秀的程序员,十分优秀!