我们应该如何设置复杂的测试场景?
我目前正在从事所谓的集成测试。我想验证如果调用 WCF 服务,它会执行我期望的操作。
让我们来看一个非常简单的场景。假设我们有一个可以搁置或取消搁置的合同对象。现在编写搁置测试非常简单。您创建一个合约实例并执行将其放在代码上的代码。
当我们想要测试起飞等待服务呼叫时,我遇到了问题。问题在于,搁置合同实际上可能相当复杂,导致各种对象都被修改。所以通常我会使用 Builder 模式并执行类似的操作..
var onHoldContract = new ContractBuilder().PutOnHold().Build();
我遇到的问题是现在我必须复制大部分搁置服务。现在,当我更改搁置某些内容的含义时,我有两个地方必须修改。
我立即想到的另一个选择是仅使用搁置服务作为我的测试设置的一部分,但现在我将我的测试与另一段代码的成功结合起来,这是我不喜欢做的事情因为它可能导致一个地方失败,破坏其他地方不相关的测试(例如,如果搁置失败)。
我在这里错过了其他选择吗?或关于哪种方法更可取以及为什么的意见?
I'm currently working on what I would call integration tests. I want to verify that if a WCF service is called it will do what I expect.
Let's take a very simple scenario. Assume we have a contract object that we can put on hold or take off hold. Now writing the put on hold test is quite simple. You create a contract instance and execute the code that puts it on code.
The question I have comes when we want to test the taking off hold service call. The problem is that putting a contract on hold can be actually quite complicated leading to various objects all be modified. So usually I would use the Builder pattern and do something like this..
var onHoldContract = new ContractBuilder().PutOnHold().Build();
The problem I have with this is now I have to pretty much replicate a large part of my put on hold service. Now when I change what putting something on hold means I have two places I have to modify.
The other option that immediately jumps out at me is to just use the put on hold service as part of my test setup but now I'm coupling my test to the success of another piece of code which is something I don't like to do since it can lead to failures in one spot breaking unrelated tests elsewhere (if put on hold failed for example).
Any other options I'm missing out here? or opinions on which method is preferable and why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Mock 框架是单元测试的一个不错的选择。但据我了解,您在这个阶段实际上正在进行整合,对吧?因此,您将 WCF 服务称为黑匣子(从客户端的角度来看)。我假设当您执行“暂停”操作时,您必须对存储库(数据库、XML 文件等)进行一些持久性操作。
在这种情况下,仅在测试端的模拟框架不会对您有太大帮助,因为为了测试“Off Hold”操作,您需要一个处于正确状态的“On Hold”对象,包括存储库条目等。在我看来,无需重新发明轮子即可做到这一点的唯一方法是使用该服务首先将其置于保留状态。但是,如果您坚持解耦,那么您将需要设置环境,这意味着代码重复(实际上我必须在某些集成测试场景中执行此操作 - 我要做的是在测试开始时设置它跑步)。
请注意,您的单元测试将位于服务本身的实现中的较低级别,并且您应该尽可能地将其解耦 - 这就是我将应用模拟框架的地方。
我希望这有帮助。
The Mock framework is a good option for unit testing. But for what I understood you are actually doing integration at this stage right? So you're calling your WCF service as a black box (from a client point of view). I'm assuming that when you're executing your On Hold operation, you have to do some persistance on a repository (Database, XML files, etc).
In this case then, the mock framework only at the testing side will not help you much, because in order to test the Off Hold operation, you need a On Hold object in the proper state, including repository entries etc. Seems to me that the only way to do this without having to reinvent the wheel is to use the service to put it On Hold first. But if you're admant to decouple then, then you will need to setup the environment and this means code duplication (I actually had to do this on some integration test scenarios - what I would do was to setup it on the start of the test run).
Notice that your unit tests would be on a lower level, in the implementation of the service itself, and there you should be decoupling it as much as possible - this is where I would be applying the Mock Framework.
I hope this helps.
您可以使用模拟框架。
模拟框架允许您创建“模拟”对象,这些对象可以代替真实对象,并在测试中使用时以特定方式响应。这将允许您直接创建人工“暂停”合同对象,然后将其传递给您的“起飞保留”服务。您可以在模拟上定义可以验证的特定行为,以便它在取消保留时能够正确运行。
您没有提到您的语言,但所有主要语言都有可用的模拟框架。对于 .net,一些示例是 NMock 的 Rhino 模拟。对于 Java,流行的一个是 Mockito。无论您使用哪种语言,如果您通过谷歌搜索,您都会发现更多内容。
You could take a look at using a mocking framework.
Mocking frameworks allow you to create "mock" objects that can take the place of your real objects and respond in specific ways when used in tests. This would allow you just to create your artificial "on hold" contract object directly and then pass it to your "take off hold" service. You would define specific behaviour on the mock that could be validated so it acted correctly while being taken off hold.
You didn't mention your language, but all of the major languages have mocking frameworks available. For .net, some examples would be NMock of Rhino mocks. For Java, a popular one is Mockito. You'll find loads more if you google for one in whatever language you are using.