是否存在允许多种继承行为的 BDD 风格框架?

发布于 2024-08-09 06:17:16 字数 1032 浏览 8 评论 0原文

我们的许多系统测试都是以 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅听莫相离 2024-08-16 06:17:16

Machine.Specifications 项目有这样的想法,您可以在其中指定一个具有Behaviors属性的类,然后在另一个类中,

Behaves_like<SomePredefinedBehaviour> some_predefined_behaviour;

在规范中多次指定,从而允许您从任意多个类继承行为。当来自传统的单元测试背景时,这种风格需要一段时间才能适应,但它确实支持这种行为。如果您下载该项目并查看示例,您将看到一个具有行为的示例。

The Machine.Specifications project has this idea, where you can specify a class with the Behaviours attribute, and then in another class, specify

Behaves_like<SomePredefinedBehaviour> some_predefined_behaviour;

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.

国粹 2024-08-16 06:17:16

使用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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文