如何在 gtest 中参数化测试组?
How to parameterize test group in gtest?
我写了下面的示例测试:
// g++ t.cpp -I googletest/include -pthread libgtest{,_main}.a
#include <gtest/gtest.h>
class C : public ::testing::TestWithParam<int>
{
public:
void SetUp(){
int n=GetParam();
std::cout<<"SetUp "<<n<<std::endl;
}
static void SetUpTestCase(){
#if 0
// Condition parameter_ != nullptr failed. GetParam() can only be called inside a value-parameterized test -- did you intend to write TEST_P instead of TEST_F?
int n=GetParam();
std::cout<<"SetUpTestCase "<<n<<std::endl;
#endif
std::cout<<"SetUpTestCase"<<std::endl;
}
};
TEST_P(C,T1){
int n=GetParam();
std::cout<<"T1 "<<n<<std::endl;
}
TEST_P(C,T2){
int n=GetParam();
std::cout<<"T2 "<<n<<std::endl;
}
INSTANTIATE_TEST_SUITE_P(S, C, ::testing::Values(1,2));
此测试打印:
Running main() from gtest_main.cc
[==========] Running 4 tests from 1 test suite.
[----------] Global test environment set-up.
[----------] 4 tests from S/C
SetUpTestCase
[ RUN ] S/C.T1/0
SetUp 1
T1 1
[ OK ] S/C.T1/0 (0 ms)
[ RUN ] S/C.T1/1
SetUp 2
T1 2
[ OK ] S/C.T1/1 (0 ms)
[ RUN ] S/C.T2/0
SetUp 1
T2 1
[ OK ] S/C.T2/0 (0 ms)
[ RUN ] S/C.T2/1
SetUp 2
T2 2
[ OK ] S/C.T2/1 (0 ms)
[----------] 4 tests from S/C (0 ms total)
[----------] Global test environment tear-down
[==========] 4 tests from 1 test suite ran. (0 ms total)
[ PASSED ] 4 tests.
我只想看一次"设置1"。可能吗? 我试图在 SetUpTestCase(( 中调用 GetParam((,但它导致了错误Condition parameter_ != nullptr failed. GetParam() can only be called inside a value-parameterized test -- did you intend to write TEST_P instead of TEST_F?
。 实际上,对于每个测试参数,我的实际设置过程最多可能需要 60 秒(并且过程结果可以在测试之间共享(这里是 T1 和 T2((,所以我想将该过程放入 SetUpTestCase((。
解决方法是将资源记录到地图中。
// g++ t.cpp -I googletest/include -pthread libgtest{,_main}.a
#include <gtest/gtest.h>
class C : public ::testing::TestWithParam<int>
{
public:
void SetUp(){
int n=GetParam();
if(sharedResource.find(n)==sharedResource.end()){
std::cout<<"SetUp "<<n<<std::endl;
std::shared_ptr<char> p(new char[10000000]);
sharedResource[n] = p;
}
}
static void TearDownTestCase(){
std::map<int,std::shared_ptr<char>>::iterator it=sharedResource.begin();
for(;it!=sharedResource.end();){
std::cout<<"TearDown "<<it->first<<std::endl;
it = sharedResource.erase(it);
}
}
static std::map<int,std::shared_ptr<char>>sharedResource;
};
std::map<int,std::shared_ptr<char>> C::sharedResource;
TEST_P(C,T1){
int n=GetParam();
std::cout<<"T1 "<<n<<std::endl;
std::shared_ptr<char> p = sharedResource[n];
}
TEST_P(C,T2){
int n=GetParam();
std::cout<<"T2 "<<n<<std::endl;
std::shared_ptr<char> p = sharedResource[n];
}
INSTANTIATE_TEST_SUITE_P(S, C, ::testing::Values(1,2));
相关文章:
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 如果模板参数是另一个模板的实例化,则键入特征测试
- 在C++单元测试上下文中,抽象基类是否应将其他抽象基类作为函数参数
- isPalindrome不显示输出,isPalindrome函数未使用字符串输入作为字符串参数进行测试
- 如何将参数传递给Google基准测试程序
- GoogleTest 中的参数化测试无法按预期工作
- 如何在 gtest 中参数化测试组?
- C++ Catch 是否有类似 NUnit 的测试用例的东西,具有多个参数/输入选项
- 按类型参数进行GTEST滤波器测试
- 如何每次使用不同的参数多次测试方法
- 如何测试模板参数 T 的 std::函数<T>是否可构造
- GTEST-不同类型的参数化测试
- 为模板类创建GTEST值参数化测试
- 使用 googletest 在类型参数化测试中标识使用的基类
- GTEST - 参数化测试限制,编译错误
- 使用谷歌模拟测试C++时无法推断模板参数
- C 单元测试框架具有类型和值参数化
- 谷歌测试参数化的高级使用
- 输出结果时如何将参数化测试合并为一个
- 分配参数用于测试目的