-
-
Notifications
You must be signed in to change notification settings - Fork 46
How can I reuse steps?
A useful way of reusing initial steps for many scenarios is to use Backgrounds.
However, when common steps need to take place after differing initial steps, or when only some scenarios in a class have common initial steps, background steps cannot be used and we need other strategies.
In all cases, it usually a good idea to encapsulate any heavy lifting in your steps in an internal DSL quite early so that most of your steps are left as one liners (see the code examples in http://adamralph.com/2012/07/28/new-background-attribute-in-xbehave-net-0-12-0/). This reduces the burden of repetition greatly and can often result in the best solution being to just repeat the steps. When this is not the case, another option is to define common steps in helpers methods which are called from scenario methods. E.g.:
public class ReallyCoolFeature
{
[Scenario]
public void BeingReallyCool(Foo fixture, Bar sut, Baz result)
{
"Given..."
.f(() => ...);
"When..."
.f(() => ...);
Then.CoolOutcomes(result);
}
}
public class UltraCoolFeature
{
[Scenario]
public void BeingUltraCool(Foo fixture, Bar sut, Baz result)
{
"Given..."
.f(() => ...);
"When..."
.f(() => ...);
Then.CoolOutcomes(result);
}
}
public static class Then
{
// NOTE: Do NOT mark this with a scenario attribute,
// otherwise it will be discovered by the test runner and executed as an independent scenario
public static void CoolOutcomes(Baz result)
{
"Then..."
.f(() => ...);
"And..."
.f(() => ...);
}
}
Of course, CoolOutcomes()
could also be defined in a base class which ReallyCoolFeature
and UltraCoolFeature
inherit from, or any other pattern for reuse could be applied. The world of C#/{insert favourite .NET language here} is your oyster!