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?