Project DescriptionStoryQ is a portable (single dll), embedded BDD framework for .NET 3.5. It runs within your existing test runner and helps produce human-friendly test output (html or text). StoryQ's fluent interface adds strong typing, intellisense and documentation to your BDD grammar.
Here are four examples using NUnit runner:
*
Simple example for analysis *
Using a "Narrative" to execute the test *
Using an Action to execute the test *
Complex example with output to html
Example One: Simple usage for analysis
Cannot resolve file macro, invalid file name or id.
using NUnit.Framework;
using StoryQ.Framework;
namespace StoryQ.Tests.Documentation
{
[TestFixture]
public class SimpleDocumentationSpec
{
[Test]
public void WritingSpecificationsToBegin()
{
Story story = new Story("writing executable specifications");
story.AsA("business person")
.IWant("to write executable specifications")
.SoThat("a developer can implement them")
.WithScenario("Writing specifications")
.Given("That i have written everything in text")
.When("the test is run")
.Then("the test result should be pending")
.WithScenario("Writing specifications again")
.Given("That i have written everything in text")
.And("still have no asserts")
.And("and have another condition")
.When("the test is run")
.And("I am happy")
.Then("the test result should be pending")
.And("here's just some more and's");
story.Assert();
}
}
}
Results output from NUnit
SimpleDocumentationSpec.WritingSpecificationsToBegin : IgnoredPending
Story: writing executable specifications
As a business person
I want to write executable specifications
So that a developer can implement them
Scenario 1: Writing specifications
Given That i have written everything in text *Pending
When the test is run *Pending
Then the test result should be pending *Pending
Scenario 2: Writing specifications again
Given That i have written everything in text *Pending
And still have no asserts *Pending
And and have another condition *Pending
When the test is run *Pending
And I am happy *Pending
Then the test result should be pending *Pending
And here's just some more and's *Pending
Example Two: Using a Narrative to execute a test
Cannot resolve file macro, invalid file name or id.
using NUnit.Framework;
using StoryQ.Framework;
namespace StoryQ.Tests.Documentation
{
[TestFixture]
public class UsingNarrativesSpec
{
[Test]
public void WritingExecutablePassingSpecificationsWithNarratives()
{
Story story = new Story("writing executable specifications");
story.AsA("business person")
.IWant("to write executable specifications")
.SoThat("a developer can implement them")
.WithScenario("Writing specifications again")
.Given(Narrative.Text("I have a narrative"))
.When(() => TheTestIs_("run"))
.Then(Narrative.Exec("this will now pass", passMe));
story.Assert();
}
static void passMe()
{
Assert.IsTrue(true);
}
static void TheTestIs_(string s)
{
Assert.AreEqual(s, "run");
}
}
}
Results output from NUnit
UsingActionsSpec.WritingExecutableSpecificationsAsActions : PassedStory: writing executable specifications
As a business person
I want to write executable specifications
So that a developer can implement them
Scenario 1: Writing specifications
Given That i have written everything in text *Pending
When the test is run *Pending
Then the test result should be pending *Pending
Scenario 2: Writing specifications again
Given That everything isnt Passed
When The test is run Passed
Then the test result should be pending *Pending
Example Three: Using an Action to execute the test
Cannot resolve file macro, invalid file name or id.
using System;
using NUnit.Framework;
using StoryQ.Framework;
namespace StoryQ.Tests.Documentation
{
[TestFixture]
public class UsingActionsSpec
{
[Test]
public void WritingExecutableSpecificationsAsActions()
{
Story story = new Story("writing executable specifications");
story.AsA("business person")
.IWant("to write executable specifications")
.SoThat("a developer can implement them")
.WithScenario("Writing specifications")
.Given("That i have written everything in text")
.When("the test is run")
.Then("the test result should be pending")
.WithScenario("Writing specifications again")
.Given(() => ThatEverythingIsnt())
.When(() => TheTestIs_("run"))
.Then("the test result should be pending");
//this test should pass only if an IgnoreException is throw by story.Assert()
expectException<IgnoreException>(story.Assert);
}
static void TheTestIs_(string s)
{
Assert.AreEqual(s, "run");
}
static void ThatEverythingIsnt()
{
Assert.IsTrue(true);
}
static T expectException<T>(Action action) where T : Exception
{
try
{
action();
Assert.Fail("Expected exception of type " + typeof(T).FullName);
}
catch (T e)
{
return e;
}
return null;
}
}
}
Results output from NUnit
UsingActionsSpec.WritingExecutableSpecificationsAsActions : PassedStory: writing executable specifications
As a business person
I want to write executable specifications
So that a developer can implement them
Scenario 1: Writing specifications
Given That i have written everything in text *Pending
When the test is run *Pending
Then the test result should be pending *Pending
Scenario 2: Writing specifications again
Given That everything isnt Passed
When The test is run Passed
Then the test result should be pending *Pending
Example Four: Complex scenario with narratives, actions and output to an html file
Cannot resolve file macro, invalid file name or id.
using System.Linq;
using System.Reflection;
using NUnit.Framework;
using StoryQ.Framework;
namespace StoryQ.Tests.Documentation
{
[TestFixture]
public class ComplexScenarioWithOutputToFileSpec
{
Narrative narrative;
bool executed;
const string aString = "LIKE THIS";
[Test]
public void WithOutputToFile()
{
Story story = new Story("implementing executable specifications");
story.AsA("developer")
.IWant("to implement executable specifications")
.SoThat("they can show a pass or a fail")
.WithScenario("converting narratives into test methods")
.Given(Narrative.Text("I have a written a method with a name like the narrative"))
.And(() => TheMethodHasntBeenRunYet())
.When(() => IReplaceTheStringNarrativeWithACallToTheMethod())
.And(() => TheTestIsRun())
.Then(() => TheTestOutputShouldPrintADescriptionOfThatMethod())
.And(() => TheMethodShouldBeExecutedAsPartOfTheTestRun())
.WithScenario("implementing specificiations with existing method calls")
.Given(Narrative.Text("I don't want to use a really big name" + aString))
.And(() => TheMethodHasntBeenRunYet())
.When(Narrative.Exec("I add a method call, but leave the text there", TheTestIsRun))
.Then(()=>TheMethodShouldBeExecutedAsPartOfTheTestRun())
.And(Narrative.Exec("the narrative will go into the test output", () =>
Assert.AreEqual("I add a method call, but leave the text there"
, story.Children.Last().Children.ElementAt(2).Narrative.Description
)
));
story.AssertAndReportToFile(MethodBase.GetCurrentMethod());
}
private void TheTestIsRun()
{
narrative.Action();
}
void TheTestOutputShouldPrintADescriptionOfThatMethod()
{
Assert.AreEqual("Do something", narrative.Description);
}
void TheMethodShouldBeExecutedAsPartOfTheTestRun()
{
Assert.IsTrue(executed);
}
void TheMethodHasntBeenRunYet()
{
executed = false;
}
void IReplaceTheStringNarrativeWithACallToTheMethod()
{
narrative = new Narrative(() => DoSomething());
}
void DoSomething()
{
executed = true;
}
}
}
Results output from NUnit Cannot resolve file macro, invalid file name or id.
ComplexScenarioWithOutputToFileSpec.ImplementingExecutableSpecificationsUsingActionsAndNarrativesOutputToFile : PassedStory: implementing executable specifications
As a developer
I want to implement executable specifications
So that they can show a pass or a fail
Scenario 1: converting narratives into test methods
Given I have a written a method with a name like the narrative
And The method hasnt been run yet Passed
When I replace the string narrative with a call to the method Passed
And The test is run Passed
Then The test output should print a description of that method Passed
And The method should be executed as part of the test run Passed
Scenario 2: implementing specificiations with existing method calls
Given I don't want to use a really big nameLIKE THIS
And The method hasnt been run yet Passed
When I add a method call, but leave the text there Passed
Then The method should be executed as part of the test run Passed
And the narrative will go into the test output Passed