单元测试学习笔记 之三

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

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

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

NUnit支持的断言

 

断言真伪

Assert.That(condition, Is.True);
Assert.That(condition, Is.False);

断言比较关系

Assert.That(actual, Is[.Not].EqualTo(expected));
Assert.That(actual. Is.LessThan(expected));
Assert.That(actual. Is.GreaterThan(expected));
Assert.That(actual. Is.AtMost(expected));
Assert.That(actual. Is.AtLeast(expected));
Assert.That(actual. Is.LessThan(expected));
Assert.That(actual. Is.EqualTo(expected).Within(tolerance));

断言对象关系

Assert.That(obj, Is[.Not].Null);
Assert.That(obj, Is[.Not].InstanceOfType(type));
Assert.That(actualObj, Is[.Not].SameAs(expectedObj));

断言容器关系

Assert.That(container, Is[.Not].Empty);
Assert.That(container, Has.Length(length));

断言字符串关系

Assert.That(actual, Is[.Not].EqualTo(expected)[.IgnoreCase]);
Assert.That(phrase, Is[.Not].StringContaining(substring)[.IgnoreCase]);
Assert.That(phrase, StartsWith(substring)[.IgnoreCase]);
Assert.That(phrase, EndsWith(substring)[.IgnoreCase]);

断言异常

Assert.That(() => { statements }, Throws.Exception.TypeOf<exception_ctor>());
Assert.That(() => { statements }, Throws.Exception);
Assert.That(() => { statements }, Throws.Nothing;

RhinoMocks用于动态stub生成

 

准备工作

using NUnit.Framework;
using Rhino.Mocks;
[TestFixture]
public class [ClassUnderTest]Test
{
    [Test]
    public void [Feature]_[Scenario]_[ExpectedResult]()
    {
        MockRepository mockEngine = new MockRepository();
        CollaboratorService stub = mockEngine.Stub<CollaboratorService>();
        ...
        mockEngine.ReplayAll();
        ...
    }
}

指定返回值

// We don't care about the passed-in arguments for CollaboratorService::methodName().
Expect.Call(stub.methodName(dummy1, dummy2, dummy3, ...)).Return(stubResult);
// We care about the passed-in arguments for CollaboratorService::methodName().
Expect.Call(stub.methodName(dummy1, dummy2, dummy3, ...)).
    Constraints(Rhino.Mocks.Constraints.Is.Equal(arg1), Rhino.Mocks.Constraints.Is.Null(), Rhino.Mocks.Constraints.Is.Same(arg3), ...).
    Return(stubResult);

指定抛出异常

// We don't care about the passed-in arguments for CollaboratorService::methodName().
Expect.Call(stub.methodName(dummy1, dummy2, dummy3, ...)).
    Constraints(Rhino.Mocks.Constraints.Is.Anything(), Rhino.Mocks.Constraints.Is.Anything(), Rhino.Mocks.Constraints.Is.Anything(), ...).
    Throw(new exception_ctor());
// We care about the passed-in arguments for CollaboratorService::methodName().
Expect.Call(stub.methodName(arg1, arg2, arg3, ...)).Throw(new exception_ctor());

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

关注51Testing

联系我们

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

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

沪ICP备05003035号

沪公网安备 31010102002173号