单元测试学习笔记 之三

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

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

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

分享:

HippoMocks用于动态mock生成

 

准备工作

// 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* mock = mockEngine.InterfaceMock<CollaboratorService>();
    ...
}

设置期望:
一定不被调用

mockEngine.NeverCall(mock, CollaboratorService::methodName);
mockEngine.NeverCallOverload(mock, (return_type(CollaboratorService::*)(arg1_type, arg2_type, ...))&CollaboratorService::methodName);

设置期望:
不一定被调用

mockEngine.OnCall(mock, CollaboratorService::methodName);
mockEngine.OnCallOverload(mock, (return_type(CollaboratorService::*)(arg1_type, arg2_type, ...))&CollaboratorService::methodName);

设置期望:
不一定被调用, 但如果一旦被调, 那么关心输入参数

mockEngine.OnCall(mock, CollaboratorService::methodName).With(arg1, arg2, ...);
mockEngine.OnCallOverload(mock, (return_type(CollaboratorService::*)(arg1_type, arg2_type, ...))&CollaboratorService::methodName).With(arg1, arg2, ...);

设置期望:
一定被调用, 但不关心输入参数

mockEngine.ExpectCall(mock, CollaboratorService::methodName);
mockEngine.ExpectCallOverload(mock, (return_type(CollaboratorService::*)(arg1_type, arg2_type, ...))&CollaboratorService::methodName);

设置期望:
一定被调用, 并关心输入参数

mockEngine.ExpectCall(mock, CollaboratorService::methodName).With(arg1, arg2, ...);
mockEngine.ExpectCallOverload(mock, (return_type(CollaboratorService::*)(arg1_type, arg2_type, ...))&CollaboratorService::methodName).With(arg1, arg2, ...);

设置期望:
一定被调用, 但不关心被调用的次序

mockEngine.autoExpect = false;
mockEngine.ExpectCall(mock, CollaboratorService::methodName1);
mockEngine.ExpectCall(mock, CollaboratorService::methodName2);

设置期望:
一定被调用, 并关心被调用的次序

mockEngine.ExpectCall(mock, CollaboratorService::methodName1);
mockEngine.ExpectCall(mock, CollaboratorService::methodName2);

显式验证

mockEngine.VerifyAll();

  3.3 用于C#的单元测试框架

  对于C#,我们推荐使用NUnit(目前为2.5版)加RhinoMocks(目前为3.6版)的组合。如下的表格演示了如何使用这两个框架。

NUnit的基本用法

 

Test Fixture和Test Method

using NUnit.Framework;
using Rhino.Mocks;
[TestFixture]
public class [ClassUnderTest]Test
{
    [SetUp]
    public void SetUp()
    {
        ...
    }
    [TearDown]
    public void TearDown()
    {
        ...
    }
    [Test]
    public void [Feature]_[Scenario]_[ExpectedResult]()
    {
        ...
    }

// Temporarily ignore a test case.

[Test]

[Ignore]
    public void [Feature]_[Scenario]_[ExpectedResult]()
    {
        ...
    }

}

Test Runner

NUnit提供了GUI界面的Test Runner,只需把编译生成的程序集(assembly)加载到该Test Runner中即可。

53/5<12345>
春暖花开更文季,点击参与还有惊喜礼品~

关注51Testing

联系我们

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

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

沪ICP备05003035号

沪公网安备 31010102002173号