单元测试之NUnit和RhinoMocks
作者:网络转载 发布时间:[ 2015/8/4 15:02:39 ] 推荐标签:软件测试技术 NUnit
测试超时
[Test,Timeout(10*1000)]
public void Test_WriteAndGetOneData()
{
...
}
测试用例分类
[Test,Timeout(10*1000)]
[Category("FunctionTest")]
public void Test_WriteAndGetMultipleData()
{
....
}
单元测试中的RhinoMocks
创建桩对象
桩对象不参与验证过程,其作用是使代码可以顺畅地运行到完成或运行到你希望测试的点。
例子
[Test]
public void CreateStub_1()
{
MockRepository mocks = new MockRepository();
IAnimal animal = mocks.Stub<IAnimal>();
animal.Legs = 1;
Assert.AreEqual(1, animal.Legs);
}
创建模拟对象
模拟对象参与验证过程,其作用是为了检测对应的依赖对象是否被调用,以及调用的顺序是否正确等等
严格:
给模拟对象设定的期望必须被执行,没有设定的期望不能发生,否则调用Verify或VerifyAll时,会导致测试用例失败
例子
[Test]
public void CreateStrictMock_1()
{
MockRepository mocks = new MockRepository();
ICustomerService customerService = mocks.StrictMock<ICustomerService>();
customerService.Expect(p => p.BuyBook(null));
mocks.ReplayAll();
customerService.BuyBook(null);
mocks.VerifyAll();
}
[Test]
public void CreateStrictMock_2()
{
MockRepository mocks = new MockRepository();
ICustomerService customerService = mocks.DynamicMock<ICustomerService>();
using (mocks.Record())
{
customerService.Expect(p => p.BuyBook(null)).Repeat.Any();
}
customerService.BuyBook(null);
customerService.BuyBook(null);
customerService.BuyBook(null);
mocks.VerifyAll();
}
[Test]
public void CreateStrictMock_3()
{
ICustomerService customerService = MockRepository.GenerateStrictMock<ICustomerService>();
customerService.Expect(p => p.BuyBook(null));
customerService.Expect(p => p.BuyTicket(null));
customerService.BuyBook(null);
customerService.BuyTicket(null);
customerService.VerifyAllExpectations();
}
[Test]
public void CreateStrictMock_ShouldBeFail()
{
MockRepository mocks = new MockRepository();
ICustomerService customerService = mocks.StrictMock<ICustomerService>();
using (mocks.Record())
{
customerService.Expect(p => p.BuyBook(null)).Repeat.Any();
}
customerService.BuyBook(null);
customerService.BuyTicket(null);//BuyTicket is not recorded and it cannot be called.
mocks.VerifyAll();
}
非严格:
给模拟对象设定的期望必须被执行,没有设定的期望可以发生,否则调用Verify或VerifyAll时,会导致测试用例失败
例子
[Test]
public void CreateDynamicMock_1()
{
MockRepository mocks = new MockRepository();
ICustomerService customerService = mocks.DynamicMock<ICustomerService>();
customerService.Expect(p => p.BuyBook(null));
mocks.ReplayAll();
customerService.BuyBook(null);
mocks.VerifyAll();
}
[Test]
public void CreateDynamicMock_2()
{
MockRepository mocks = new MockRepository();
ICustomerService customerService = mocks.DynamicMock<ICustomerService>();
using (mocks.Record())
{
customerService.Expect(p => p.BuyBook(null));
}
customerService.BuyBook(null);
mocks.VerifyAll();
}
[Test]
public void CreateDynamicMock_3()
{
MockRepository mocks = new MockRepository();
ICustomerService customerService = mocks.DynamicMock<ICustomerService>();
using (mocks.Record())
{
customerService.Expect(p => p.BuyBook(null)).Repeat.Any();
}
customerService.BuyBook(null);
customerService.BuyTicket(null);//Even though BuyTicket is not recorded and it be called.
mocks.VerifyAll();
}
[Test]
public void CreateDynamicMock_4()
{
MockRepository mocks = new MockRepository();
IAnimal animal = mocks.DynamicMock<IAnimal>();
Expect.Call(animal.Legs).PropertyBehavior();
animal.Legs = 1;
Assert.AreEqual(1, animal.Legs);
}
创建部分模拟对象
部分模拟对象可以用于测试抽象类中的模板方法。
例子
[Test]
public void CreatePartialMock_1()
{
MockRepository mocks = new MockRepository();
AbstractProcessor processor = mocks.PartialMock<AbstractProcessor>();
using (mocks.Record())
{
Expect.Call(processor.GetStatus()).Return(0);
Expect.Call(processor.GetStatus()).Return(1);
}
Assert.AreEqual("OK", processor.Process());
Assert.AreEqual("NOTOK", processor.Process());
mocks.VerifyAll();
}
public abstract class AbstractProcessor
{
public string Process()
{
if (GetStatus() == 0)
{
return "OK";
}
else
{
return "NOTOK";
}
}
public abstract int GetStatus();
}
模拟对象属性调用
[Test]
public void Property_1()
{
MockRepository mocks = new MockRepository();
Ticket ticket = mocks.StrictMock<Ticket>();
using (mocks.Record())
{
Expect.Call(ticket.Movie).PropertyBehavior();
Expect.Call(ticket.Price).PropertyBehavior();
}
ticket.Movie = "ABC";
ticket.Price = 10.0f;
Assert.AreEqual("ABC", ticket.Movie);
Assert.AreEqual(10.0f, ticket.Price);
ticket.VerifyAllExpectations();
}
[Test]
public void Property_2()
{
MockRepository mocks = new MockRepository();
Ticket ticket = mocks.StrictMock<Ticket>();
using (mocks.Record())
{
Expect.Call(ticket.Movie).Return("ABC");
Expect.Call(ticket.Price).Return(10.0f);
}
Assert.AreEqual("ABC", ticket.Movie);
Assert.AreEqual(10.0f, ticket.Price);
ticket.VerifyAllExpectations();
}
[Test]
public void Property_3()
{
MockRepository mocks = new MockRepository();
Book book = mocks.Stub<Book>();
book.Name = "ABC";
book.Price = 10.0f;
Assert.AreEqual("ABC", book.Name);
Assert.AreEqual(10.0f, book.Price);
}
模拟delegate和设定模拟对象方法被调用的顺序
[Test]
public void Ordered_OK()
{
MockRepository mocks = new MockRepository();
ICustomerService customer = mocks.StrictMock<ICustomerService>();
using (mocks.Ordered())
{
customer.BuyTicket(null);
customer.BuyBook(null);
}
customer.Replay();
customer.BuyTicket(null);
customer.BuyBook(null);
customer.VerifyAllExpectations();
}
[Test]
public void Ordered_ShouldFail()
{
MockRepository mocks = new MockRepository();
ICustomerService customer = mocks.StrictMock<ICustomerService>();
using (mocks.Ordered())
{
customer.BuyTicket(null);
customer.BuyBook(null);
}
customer.Replay();
customer.BuyBook(null);
customer.BuyTicket(null);
customer.VerifyAllExpectations();
}
[Test]
public void Delegate_1()
{
MockRepository mocks = new MockRepository();
var oo = mocks.DynamicMock<DoThing>();
oo("BuyBook");
oo.Replay();
oo("BuyBook");
oo.VerifyAllExpectations();
}
[Test]
public void Delegate_Action()
{
MockRepository mocks = new MockRepository();
var oo = mocks.DynamicMock<Action<string>>();
oo("BuyBook");
oo.Replay();
oo("BuyBook");
oo.VerifyAllExpectations();
}
[Test]
public void Delegate_Func()
{
MockRepository mocks = new MockRepository();
var oo = mocks.DynamicMock<Func<string,string>>();
Expect.Call(oo("BuyBook")).Return("OK");
oo.Replay();
Assert.AreEqual("OK", oo("BuyBook"));
oo.VerifyAllExpectations();
}
public delegate void DoThing(string thing);
}
本文内容不用于商业目的,如涉及知识产权问题,请权利人联系SPASVO小编(021-61079698-8054),我们将立即处理,马上删除。
相关推荐
iOS单元测试mocha、chai、sinon和istanbul实现百分之百的单元测试覆盖率关于单元测试的总结及思考编写更好的Java单元测试的7个技巧Android单元测试框架Robolectric3.0介绍(一)使用Kiwi单元测试总结单元测试如此重要,为什么你不知道Python单元测试??使用装饰器实现测试跳过和预期故障对Controller的单元测试写好单元测试的10个技巧单元测试的重要性Angular单元测试系列??Component、Directive、Pipe 以及ServiceAndroid单元测试的整理提升单元测试体验的利器--Mockito使用总结iOS UnitTest单元测试Vue的单元测试探索(二)
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11热门文章
常见的移动App Bug??崩溃的测试用例设计如何用Jmeter做压力测试QC使用说明APP压力测试入门教程移动app测试中的主要问题jenkins+testng+ant+webdriver持续集成测试使用JMeter进行HTTP负载测试Selenium 2.0 WebDriver 使用指南