Skip to content

Backgrounds

Michael W Powell edited this page Dec 8, 2020 · 1 revision

xWellBehaved.net allows the addition of background steps, equivalent to Cucumber Background.

public class CalculatorFeature
{
    private Calculator calculator;

    [Background]
    public void Background() // this method can have any name
    {
        "Given a calculator".x(() => this.calculator = new Calculator());
    }

    [Scenario]
    public void Addition(int x, int y, int answer)
    {
        "Given the number 1".x(() => x = 1);

        "And the number 2".x(() => y = 2);

        "When I add the numbers together".x(() => answer = calculator.Add(x, y));

        "Then the answer is 3".x(() => Assert.Equal(3, answer));
    }

    [Scenario]
    public void Multiplication(int x, int y, int answer)
    {
        "Given the number 2".x(() => x = 2);

        "And the number 3".x(() => y = 3);

        "When I multiply the numbers together".x(() => answer = calculator.Multiply(x, y));

        "Then the answer is 6".x(() => Assert.Equal(6, answer));
    }
}

The background methods are executed exactly once before each scenario.

Any disposable objects or rollback delegates registered in a background method are executed after the execution of each scenario.

For another example see the blog post "New Background Attribute in xBehave.net 0.12.0".

Taming background complexity

When the background steps need to span multiple classes, they can be moved to a common base class.

public abstract class CoolFeature
{
    [Background]
    public void Background()
    {
        "Given..."
            .x(() => ...);
        "And..."
            .x(() => ...);
        ...
    }
}
public class ReallyCoolFeature : CoolFeature
{
    [Scenario]
    public void BeingReallyCool()
    {
        ...
    }
}
public class UltraCoolFeature : CoolFeature
{
    [Scenario]
    public void BeingUltraCool()
    {
        ...
    }
}

Managing background complexity

Of course, which base class really depends upon the complexity of your test framework. How you manage that complexity is entirely up to you to decide, xWellBehaved.net is not there to get in the way of those decisions.

public abstract class BaseClassOne
{
    [Background]
    public BackgroundOne() { ... }
}

public abstract class BaseClassTwo : BaseClassOne
{
    [Background]
    public BackgroundTwo() { ... }
}

public class ClassTests : BaseClassTwo
{
    [Background]
    public ClassBackground() { ... }
}

xWellBehaved.net discovers each of these background methods and invokes them front to back, that is, from the base class forward, in the expected order.

An interesting side effect

public class ClassTests : BaseClassTwo
{
    [Background]
    public ClassBackground() { ... }

    [Background]
    public AnotherClassBackground() { ... }
}

Given this behavior, xWellBehaved.net will also discover multiple background methods, although we cannot guarantee the order in which same scoped methods will be invoked. As long as the backgrounds are distinct from one another, you are probably fine, but we recommend considering either establish a base class, or consider merging the method bodies into a single background method, whatever is most appropriate to do so in your scenario.

Clone this wiki locally