单元测试学习笔记 之三

发表于:2011-8-24 13:29

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

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

gTest支持的命令行参数

 

--gtest-filter=[filter]

对执行的测试案例进行过滤,支持
?    单个字符
*    任意字符
-    排除,如,-a 表示除了a
:    取或,如,a:b 表示a或b
比如下面的例子:
./foo_test 没有指定过滤条件, 运行所有案例
./foo_test --gtest_filter=* 使用通配符*, 表示运行所有案例
./foo_test --gtest_filter=FooTest.* 运行所有[ClassUnderTest]Test为FooTest的案例
./foo_test --gtest_filter=FooTest.*-FooTest.Bar 运行所有[ClassUnderTest]Test为FooTest的案例, 但是除了FooTest.Bar这个案例

--gtest_repeat=[COUNT]

设置案例重复运行次数. 比如:
--gtest_repeat=1000      重复执行1000次, 即使中途出现错误.
--gtest_repeat=1000 --gtest_break_on_failure     重复执行1000次, 并且在第一个错误发生时立即停止. 这个功能对调试非常有用.

--gtest_print_time

打印每个测试方法运行的所用时间.

--gtest_catch_exceptions

捕捉异常. 这样当测试方法中抛出了异常时, 不会阻碍了后续测试方法的运行. 注意: 这个参数只在Windows下有效.

--gtest_output=xml[:DIRECTORY_PATH/|:FILE_PATH]

产生XML格式的报告。

gTest支持的断言

 

断言真伪

ASSERT_TRUE(condition) [<< message];
ASSERT_FALSE(condition) [<< message];

断言比较关系

ASSERT_EQ(expected, actual) [<< message];
ASSERT_NE(expected, actual) [<< message];
ASSERT_LT(expected, actual) [<< message];
ASSERT_LE(expected, actual) [<< message];
ASSERT_GT(expected, actual) [<< message];
ASSERT_GE(expected, actual) [<< message];
ASSERT_FLOAT_EQ(expected, actual) [<< message];
ASSERT_DOUBLE_EQ(expected, actual) [<< message];
ASSERT_NEAR(expected, actual, tolerance) [<< message];

断言C字符串关系

ASSERT_STREQ(expected_cstr, actual_cstr) [<< message];
ASSERT_STRNE(expected_cstr, actual_cstr) [<< message];
ASSERT_STRCASEEQ(expected_cstr, actual_cstr) [<< message];
ASSERT_STRCASENE(expected_cstr, actual_cstr) [<< message];

断言异常

ASSERT_THROW({ statements }, exception_ctor);
ASSERT_NO_THROW({ statements });
ASSERT_ANY_THROW({ statements });

HippoMocks用于动态stub生成

 

准备工作

// In [ClassUnderTest]Test.cpp file.
#include <gtest/gtest.h>
#include <hippomocks.h>
#include "[ClassUnderTest]Test.h"
// Test Method definition.
TEST_F([ClassUnderTest]Test, [Feature]_[Scenario]_[ExpectedBehavior])
{
    MockRepository mockEngine;
    CollaboratorService* stub = mockEngine.InterfaceMock<CollaboratorService>();
    ...
}

指定返回值

// We don't care about the passed-in arguments for CollaboratorService::methodName().
mockEngine.OnCall(stub, CollaboratorService::methodName).Return(stubResult);
// We care about the passed-in arguments for CollaboratorService::methodName().
mockEngine.OnCall(stub, CollaboratorService::methodName).With(arg1, arg2, ...).Return(stubResult);
// If CollaboratorService::methodName() is overloaded, use the following invocation forms.
mockEngine.OnCallOverload(stub, (return_type(CollaboratorService::*)(arg1_type, arg2_type, ...))&CollaboratorService::methodName).Return(stubResult);
mockEngine.OnCallOverload(stub, (return_type(CollaboratorService::*)(arg1_type, arg2_type, ...))&CollaboratorService::methodName).With(arg1, arg2, ...).Return(stubResult);

指定抛出异常

// We don't care about the passed-in arguments for CollaboratorService::methodName().
mockEngine.OnCall(stub, CollaboratorService::methodName).Throw(exception_ctor());
// We care about the passed-in arguments for CollaboratorService::methodName().
mockEngine.OnCall(stub, CollaboratorService::methodName).With(arg1, arg2, ...).Throw(exception_ctor());
// If CollaboratorService::methodName() is overloaded, use the following invocation forms.
mockEngine.OnCallOverload(stub, (return_type(CollaboratorService::*)(arg1_type, arg2_type, ...))&CollaboratorService::methodName).Throw(exception_ctor());
mockEngine.OnCallOverload(stub, (return_type(CollaboratorService::*)(arg1_type, arg2_type, ...))&CollaboratorService::methodName).With(arg1, arg2, ...).Throw(exception_ctor());

52/5<12345>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

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

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

沪ICP备05003035号

沪公网安备 31010102002173号