集成测试时设置系统状态的最佳实践/想法?
我有许多使用 Cucumber 流行的 Give/When/Then 风格编写的 C# 集成测试。 我使用的框架基本上与 NBehave 相同。
我面临的一个反复出现的问题是设置和连接集成测试所需的所有应用程序状态的问题。 我的大多数测试看起来像这样:
Given an empty system And a new NetworkServer And a new ServerDatabase And a new Eventlogger And a new Networkconnection And a new LoggingClient When the client logs a new event Then it should appear in the server database
如您所见,操作和断言是单行,但我有 6 行“接线”。 我的几乎每个测试都会重复这 6 行。
这对我来说似乎是一种代码味道,但我不确定如何处理这个问题。 我可以将这 6 行重构为一行(给定“一个有效的系统...”
或类似的内容),但这似乎太过分了,我会隐藏太多信息。
我很感激在这方面有更多经验的其他人的任何想法。 多谢。
I have a number of C# integration tests written using the Given/When/Then style popularized by cucumber. I'm using a framework which basically works the same as NBehave.
A recurring issue I'm facing is the issue of setup and wiring up all the application state necessary for the integration test. Most of my tests look something like this:
Given an empty system And a new NetworkServer And a new ServerDatabase And a new Eventlogger And a new Networkconnection And a new LoggingClient When the client logs a new event Then it should appear in the server database
As you can see the action and assertion are single lines, but I have 6 lines of 'wiring'. Almost every test I have repeats these 6 lines.
This seems like a code smell to me, but I'm unsure as to how to handle this. I could refactor the 6 lines into a single one (Given "a valid system..."
or somesuch) but that seems like it goes too far and I'd be hiding too much information.
I'd appreciate any thoughts from others with more experience in this area. Thanks a lot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对我来说,这听起来就像您希望有一个基类来执行设置之类的事情,然后让您的测试类从该基类继承,并且仅添加新的测试功能。
To me this smells like you'd like to have a base class doing the setup kind of things and then have your test classes inherit from this base and only add the new testing functionality.
我们有这样的东西
然后,对于每组类似的测试,我们创建一个像这样的 BaseContext:
We have something like this
Then for each group of similar tests we create a BaseContext like this: