thoughts on coding

March 26, 2010

StoryQ: scenario enhancements

Filed under: BDD, StoryQ, Uncategorized — Frantisek @ 11:04 am

Thankfully to Rob who inspired me from the comments in my previous post with the easy way how to create the scenario description from the test method name, I decided to share my StoryQ enhancements.

Rob wrote about using a StoryTestBase class with the property Scenario which creates the scenario with the text/name created based on the called method. I extended it little bit. My base class is called StorySpecBase and it contains a property

    1         protected virtual Scenario Scenario

    2         {

    3             get

    4             {

    5                 return WithScenario(StoryQHelper.ConvertTestMethodAsScenario());

    6             }

    7         }

and the helper class called StoryQHelper contains few methods which I use also in Run extension method, now.

    1         public static MethodBase GetTestMethod()

    2         {

    3             var trace = new StackTrace();

    4             for (int i = 0; i < trace.FrameCount; i++)

    5             {

    6                 var methodBase = trace.GetFrame(i).GetMethod();

    7                 bool isTestMethod = methodBase.GetCustomAttributes(true).Any(

    8                     attribute =>

    9                         {

   10                             var name = attribute.GetType().Name;

   11                             if (name == "TestAttribute" || name == "TestMethodAttribute") return true;

   12                             return false;

   13                         });

   14 

   15                 if (isTestMethod) return methodBase;

   16             }

   17             return null;

   18         }

   19 

   20         public static string GetTestMethodName()

   21         {

   22             var method = GetTestMethod();

   23             if (method == null) return null;

   24 

   25             return method.Name;

   26         }

   27 

   28         public static string Uncamel(string methodName)

   29         {

   30             return Regex.Replace(methodName, "[A-Z_]", x => " " + x.Value.ToLowerInvariant()).Trim();

   31         }

   32 

   33         public static string ConvertTestMethodAsScenario()

   34         {

   35             var methodName = GetTestMethodName();

   36             if (methodName == null) return "Nonamed scenario!";

   37 

   38             return Uncamel(GetTestMethodName());

   39         }

The method GetTestMethod uses StackFrames and it tries to find the caller with the attribute Test (Nunit) or TestMethod(MSTest) so I dont have specified fixed stack frame index.

All my tests are derived from StoryQSpecBase and I wrote the tests like:

    1         [Test]

    2         public void ShouldBuildNewStructure()

    3         {

    4             WithScenario("Building new structure")

    5                     .Given(TheModelContainsThaStructureDataWithId_, 0)

    6                     .When(BuildingANewStructure)

    7                     .Then(NewStructureIsFullyBuilt)

    8             .Run();

    9         }

The scenario is named the same way as the test method name :o), which is not very nice.

Now I write them as following:

    1         [Test]

    2         public void ShouldBuildNewStructure()

    3         {

    4             Scenario

    5                     .Given(TheModelContainsThaStructureDataWithId_, 0)

    6                     .When(BuildingANewStructure)

    7                     .Then(NewStructureIsFullyBuilt)

    8             .Run();

    9         }

Nicer, isn’t it? I avoided the string …

Advertisements

2 Comments »

  1. Fero, what BDD system, if any, do you actually use?

    Comment by foobar — December 11, 2010 @ 11:12 pm

    • I use StoryQ. Check their site for the feature set they provide.

      The best feature they have that the test description is translated to the test methods => no comments ! but the method names explains the test step. If I change the test step code, I change only the method name and that’s it.

      F

      Comment by Frantisek — December 12, 2010 @ 2:12 pm


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: