是否存在允许多种继承行为的 BDD 风格框架?
我们的许多系统测试都是以 BDD 风格编写的,并且我们充分利用继承的行为来最大程度地减少重复,例如,这可能是购买测试的基本层次结构。
class BehavesLikeSuccessfulPurchase
class BehavesLikePurchaseWithValidCreditCard : BehavesLikeSuccessfulPurchase
在本例中,BehavesLikeSuccessfulPurchase
定义常见行为,例如帐户对账单应包含借方条目,而 BehavesLikePurchaseWithValidCreditCard
类定义使用有效信用购买任何类型产品的测试工作流程卡,因此测试是小的派生类,仅提供具体的产品实例,例如,
[Concern(typeof(Video))]
class WhenPurchasedWithValidCreditCard : BehavesLikePurchaseWithValidCreditCard
但是,根据具体的产品类型,我们还需要进行一些额外的检查,例如,每当成功购买视频时,我们要检查它是否是添加到用户的视频库。理想情况下,这可以由另一个类定义并使用假设的语法进行混合:
class BehavesLikeSuccessfulVideoPurchase
[Concern(typeof(Video))]
class WhenPurchasedWithValidCreditCard : BehavesLikePurchaseWithValidCreditCard
mixin BehavesLikeSuccessfulVideoPurchase
{
}
但是当然,C# 不支持多重继承或混合,因此我们最终编写了一堆样板方法,这些方法将调用转发到其他行为,这每次行为改变时都需要改变。
我们真正需要的是一个框架,它有自己的机制,只需提供应该观察的附加行为的类型即可支持测试中的多种行为。我一直在研究 xUnit 和规范示例,看起来可以对其进行一些扩展来实现这一目的,但是是否已经存在任何东西?
Many of our system tests are written in a BDD style, and we make decent use of inherited behaviours to minimise duplication, for example this might be a basic hierarchy for purchase tests.
class BehavesLikeSuccessfulPurchase
class BehavesLikePurchaseWithValidCreditCard : BehavesLikeSuccessfulPurchase
In this case the BehavesLikeSuccessfulPurchase
defines common behaviours like the account statement should have a debit entry, and the BehavesLikePurchaseWithValidCreditCard
class defines the test workflow for purchasing any type of product with a valid credit card, so the tests are small derived classes that simply supply the concrete product instance, e.g.
[Concern(typeof(Video))]
class WhenPurchasedWithValidCreditCard : BehavesLikePurchaseWithValidCreditCard
However, depending on the concrete product type we also need to have some additional checks, for example whenever a video is successfully purchased we want to check that it is added to the user's video library. Ideally this could be defined by another class and mixed in, using the hypothetical syntax:
class BehavesLikeSuccessfulVideoPurchase
[Concern(typeof(Video))]
class WhenPurchasedWithValidCreditCard : BehavesLikePurchaseWithValidCreditCard
mixin BehavesLikeSuccessfulVideoPurchase
{
}
But of course C# doesn't support multiple inheritance or mixins so we end up writing a load of boiler-plate methods that forward calls to the additional behaviours, which needs to change every time that behaviour changes.
What we really need is a framework that has its own mechanism for supporting multiple behaviours from tests simply by supplying the type(s) of additional behaviours that should be observed. I've been looking at xUnit and the specification examples, and it looks like it would be possible to come up with some extensions to it that could do the trick, but is there anything already in existence?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Machine.Specifications 项目有这样的想法,您可以在其中指定一个具有Behaviors属性的类,然后在另一个类中,
在规范中多次指定,从而允许您从任意多个类继承行为。当来自传统的单元测试背景时,这种风格需要一段时间才能适应,但它确实支持这种行为。如果您下载该项目并查看示例,您将看到一个具有行为的示例。
The Machine.Specifications project has this idea, where you can specify a class with the Behaviours attribute, and then in another class, specify
more than once in the specification, allowing you to inherit behaviour from as many classes as you like. The style takes a while to get used to when coming from a traditional unit testing background, but it does support the behaviour. If you download the project and look at the examples, you'll see one with behaviours.
使用Linfu你可以做Mixins: http://www.codeproject.com/KB/cs /LinFuPart2.aspx
不过,我不确定 BDD 框架是否能与 LinFu 动态对象很好地配合。
我自己还没有机会使用 LinFu 的 Mixins,所以有兴趣听听它们在中等复杂的场景中使用时有多容易/复杂,以及是否有任何重大缺点。
Using Linfu you can do Mixins: http://www.codeproject.com/KB/cs/LinFuPart2.aspx
What I'm not sure about though, is if the BDD framework will play nicely with LinFu dynamic objects.
I haven't had a chance to use LinFu's Mixins myself, so would be interested in hearing how easy/complex they are when used in a moderately complex scenario and if there's any major drawbacks.