-
Notifications
You must be signed in to change notification settings - Fork 0
Backgrounds
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".
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()
{
...
}
}
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.
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.
- Home
- Quick start
-
Documentation
- Writing scenarios
- Running scenarios
- Package dependencies
- Debugging scenarios
- Assertions
- Step names
- Debugging Scenarios with examples
- Background methods
- TearDown methods
- Async steps
- Object disposal
- Rollback
- Skipping steps and scenarios
- Step metadata
- Continuing on failure
- Step filters
- Changes in xBehave.net version 2.0
- Changes since deriving from xBehave.net
- Extending xWellBehaved.net
- FAQ
- Known Issues
- Contributions