Which is as ‘ brief ’ as I have known a play.
But by ten words, my lord, it is too long"
-- Shakespeare, Midsummer Night's Dream
Martin Heidegger in his paper, The Origin of the Work of Art, gives us three different ways of defining thingness.
- bearer of traits and properties
- sense perceptions
- formed stuff
Traits is an interesting term in computer science, it is a collection of behaviors and their implementation which can be used to extend an object. Most languages do not directly support the creation of stand-alone traits but traits are the essence of what makes up a programs value.
I am going to make a very bold statement here, I am going to say that BDD (Behavior Driven Development) is really about proving the traits of software. Using the definition of traits given above we see that a trait is a collection of behaviors and how they have been implemented. This sounds a lot like what we are proving out when we do BDD.
In BDD what are we doing? We are proving that behavior X as it is implemented by code S does the following when given state G with stimulus W yielding state T. In other words we are showing Given the following conditions, When we do the following against X in this Source code, Then the following result will occur. This sounds a lot like proving the traits of the software to me.
"Words, words, words."
-- Shakespeare, Hamlet
Let us look at a example.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace FizzBuzz | |
{ | |
public class FizzBuzzer | |
{ | |
public string Translate(int value) | |
{ | |
var result = string.Empty; | |
if (value % 3 == 0) result += "Fizz"; | |
if (value % 5 == 0) result += "Buzz"; | |
return string.IsNullOrEmpty(result) ? value.ToString() : result; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Feature: FizzBuzzerSpec | |
I want to be able to translate numbers to their FizzBuzz value | |
In order to be able to communicate with the FizzBuzzons | |
As a Starfleet Captain | |
Scenario: 2 must translate to the string 2 | |
Given a FizzBuzzer | |
And the value of '2' | |
When translate is invoked | |
Then the result should be '2' | |
Scenario: 3 must translate to the string Fizz | |
Given a FizzBuzzer | |
And the value of '3' | |
When translate is invoked | |
Then the result should be 'Fizz' | |
Scenario: 5 must translate to the string Buzz | |
Given a FizzBuzzer | |
And the value of '5' | |
When translate is invoked | |
Then the result should be 'Buzz' | |
Scenario: 15 must translate to the string FizzBuzz | |
Given a FizzBuzzer | |
And the value of '15' | |
When translate is invoked | |
Then the result should be 'FizzBuzz' | |
Scenario Outline: FizzBuzz translation | |
Given a FizzBuzzer | |
And the value of '<value>' | |
When translate is invoked | |
Then the result should be '<expected value>' | |
Examples: | |
| value | expected value | | |
| 2 | 2 | | |
| 4 | 4 | | |
| 3 | Fizz | | |
| 9 | Fizz | | |
| 5 | Buzz | | |
| 25 | Buzz | | |
| 15 | FizzBuzz | | |
| 30 | FizzBuzz | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using FizzBuzz; | |
using TechTalk.SpecFlow; | |
using NUnit.Framework; | |
namespace FizzBuzzSpec | |
{ | |
[Binding] | |
public class FizzBuzzerSpecSteps | |
{ | |
[Given(@"a FizzBuzzer")] | |
public void GivenAFizzBuzzer() | |
{ | |
ScenarioContext.Current.Add("FizzBuzzer", new FizzBuzzer()); | |
} | |
[Given(@"the value of '(.*)'")] | |
public void GivenTheValueOf(int value) | |
{ | |
ScenarioContext.Current.Add("value", value); | |
} | |
[When(@"translate is invoked")] | |
public void WhenTranslateIsInvoked() | |
{ | |
var fizzbuzzer = ScenarioContext.Current.Get<FizzBuzzer>("FizzBuzzer"); | |
var value = ScenarioContext.Current.Get<int>("value"); | |
ScenarioContext.Current.Add("actual", fizzbuzzer.Translate(value)); | |
} | |
[Then(@"the result should be '(.*)'")] | |
public void ThenTheResultShouldBe(string expected) | |
{ | |
var actual = ScenarioContext.Current.Get<string>("actual"); | |
Assert.That(expected, Is.EqualTo(actual)); | |
} | |
} | |
} |
In the example above we see that a Starfleet Captain needs to be able to communicate with the FizzBuzzons and thus needs to have a translator.
She is given the following C# source code to preform the needed behavior of translating.
public string Translate(int value){var result = string.Empty;if (value % 3 == 0) result += "Fizz";if (value % 5 == 0) result += "Buzz";return string.IsNullOrEmpty(result) ? value.ToString() : result;}
To prove to her that it works the engineers and communication specialist came up with the following specifications listing what happens in a Given state When the an input value is translated Then the value will have a certain output.
By testing around these specifications we can prove to our captain that the translator does hold the desired traits.
Does this slight different definition around BDD change anything? No, but I hope it does widen the thought around it.
Scenario: 2 must translate to the string 2Given a FizzBuzzerAnd the value of '2'When translate is invokedThen the result should be '2'Scenario: 3 must translate to the string FizzGiven a FizzBuzzerAnd the value of '3'When translate is invokedThen the result should be 'Fizz'Scenario: 5 must translate to the string BuzzGiven a FizzBuzzerAnd the value of '5'When translate is invokedThen the result should be 'Buzz'Scenario: 15 must translate to the string FizzBuzzGiven a FizzBuzzerAnd the value of '15'When translate is invokedThen the result should be 'FizzBuzz'
By testing around these specifications we can prove to our captain that the translator does hold the desired traits.
Does this slight different definition around BDD change anything? No, but I hope it does widen the thought around it.
Think but this, and all is mended:
That you have but slumbered here
While these visions did appear."
-- Shakespeare, Midsummer Night's Dream