Pending scenarios in Behave#
This post was originally published here.
When first authoring stories and scenarios in Behave#, the implementation to support the scenario probably doesn’t exist yet. Part 4 of Nelson Montalvo’s series of posts on Behave# illustrates this quite nicely. The problem is that you don’t really want test failures because you haven’t implemented a scenario yet. Rbehave has the ability to create “pending” scenarios, which won’t fail if the implementation doesn’t exist.
Misleading failures
For example, here’s a scenario that doesn’t have an implementation yet:
[Test] public void Withdraw_from_savings_account_pending() {Account savings = <span class="kwrd">null</span>; Account cash = <span class="kwrd">null</span>; Story transferStory = <span class="kwrd">new</span> Story(<span class="str">"Transfer to cash account"</span>); transferStory .AsA(<span class="str">"savings account holder"</span>) .IWant(<span class="str">"to transfer money from my savings account"</span>) .SoThat(<span class="str">"I can get cash easily from an ATM"</span>); transferStory .WithScenario(<span class="str">"Savings account is in credit"</span>) .Given(<span class="str">"my savings account balance is"</span>, -20) .And(<span class="str">"my cash account balance is"</span>, 10) .When(<span class="str">"I transfer to cash account"</span>, 20) .Then(<span class="str">"my savings account balance should be"</span>, -20) .And(<span class="str">"my cash account balance should be"</span>, 10);}
When I execute this story, I get a failed test and a misleading exception:
Story: Transfer to cash account Narrative: As a savings account holder I want to transfer money from my savings account So that I can get cash easily from an ATM Scenario 1: Savings account is in credit TestCase 'BehaveSharp.Specs.AccountSpecs.Withdraw_from_savings_account_pending' failed: BehaveSharp.ActionMissingException : Action missing for action 'my savings account balance is'. C:devBehaveSharptrunksrcBehaveSharp.ExamplesAccountSpecs.cs(70,0): at BehaveSharp.Specs.AccountSpecs.Withdraw_from_savings_account_pending()
Not a very helpful message, especially if I’m executing a suite of stories and scenarios.
Pending scenarios
If a scenario has a pending implementation, I can now add a “pending” message to the scenario, so that I don’t get any error messages:
[Test] public void Withdraw_from_savings_account_pending() {Account savings = <span class="kwrd">null</span>; Account cash = <span class="kwrd">null</span>; Story transferStory = <span class="kwrd">new</span> Story(<span class="str">"Transfer to cash account"</span>); transferStory .AsA(<span class="str">"savings account holder"</span>) .IWant(<span class="str">"to transfer money from my savings account"</span>) .SoThat(<span class="str">"I can get cash easily from an ATM"</span>); transferStory .WithScenario(<span class="str">"Savings account is in credit"</span>) .Pending(<span class="str">"ability to withdraw from accounts"</span>) .Given(<span class="str">"my savings account balance is"</span>, -20) .And(<span class="str">"my cash account balance is"</span>, 10) .When(<span class="str">"I transfer to cash account"</span>, 20) .Then(<span class="str">"my savings account balance should be"</span>, -20) .And(<span class="str">"my cash account balance should be"</span>, 10);}
Note the “Pending” method call after “WithScenario”. Think of “Pending” similar to the “Ignore” attribute in NUnit. The story is there, the scenario is written, but the code to support the scenario doesn’t exist yet. Here’s the output of the execution with “Pending”:
Story: Transfer to cash account Narrative: As a savings account holder I want to transfer money from my savings account So that I can get cash easily from an ATM Scenario 1: Savings account is in credit Pending: ability to withdraw from accounts
The scenario stops execution of the scenario (just so you can see the pending scenarios more easily), and outputs the pending message. Without the “Pending” message, you will get an exception and your test will fail. With “Pending”, you can flag your pending scenarios without worrying about breaking the build if you check in.
