单元测试学习笔记 之四

发表于:2011-8-25 11:11

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:未知    来源:51Testing软件测试网采编

分享:

gTest对参数化测试的支持

 

声明Test Fixture

#ifndef CLASS_UNDER_TEST_TEST_H
#define CLASS_UNDER_TEST_TEST_H
// In [ClassUnderTest]Test.h file.
#include <gtest/gtest.h>
// Test Fixture declaration.
class [ClassUnderTest]Test : public testing::TestWithParam<param_type>
{
protected:
    // Optional SetUp() and TearDown().
    virtual void SetUp();
    virtual void TearDown();
};
#endif

定义Test Method

// In [ClassUnderTest]Test.cpp file.
#include <gtest/gtest.h>
#include <hippomocks.h>
#include "[ClassUnderTest]Test.h"
// Optional SetUp() and TearDown().
void [ClassUnderTest]Test::SetUp()
{
    ...
}
void [ClassUnderTest]Test::TearDown()
{
    ...
}
// Test Method definition.
INSTANTIATE_TEST_CASE_P(prefix, [ClassUnderTest]Test, param_value_generator);
TEST_P([ClassUnderTest]Test, [Feature]_[Scenario]_[ExpectedBehavior])
{
    ClassUnderTest obj;
    param_type param = GetParam();
    result_type result = obj.SingleParam_MethodUnderTest(param);
    ASSERT_XX(result);
}
// Other Test Method definition.
TEST_F([ClassUnderTest]Test, [Feature]_[Scenario]_[ExpectedBehavior])
{
    ...
}

gTest所支持的参数值生成器

 

testing::Range(begin, end[, step])

范围在[begin, end)之间, 步长为step. step缺省为1.

testing::Values(val1, val2, ...)

范围为(val1, val2, ...).

testing::ValuesIn(array)
testing::ValuesIn(container)

范围由数组或STL容器给定.

testing::Bool()

范围为[false, true].

52/5<12345>
重磅发布,2022软件测试行业现状调查报告~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2023
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号