A little bit of Gherkin

I have been using Spec Flow for the last few months as my feature specification framework, but sometimes the output I want to assert can be very repetitive, a simple example of this the string calculator kata.

Each part of the add feature specification is actually simply checking the return value given a specified input string.

using a traditional spec flow scenario specification it would look a bit like this

  1. Feature:Calculator Kata
  2. As a developer   
  3. I want practice my craft
  4. So that I become a better developer
  5.  
  6. Scenario: string calculator add method with input containing "1"
  7.   Given the input stirng contains 1
  8.   When add is called
  9.   Then the result is 1
  10.  
  11. Scenario: string calculator add method with input containing "1,2"
  12.   Given the input stirng contains 1,2
  13.   When add is called
  14.   Then the result is 3
  15.  
  16. Scenario: string calculator add method with input containing "1,2,3"
  17.   Given the input stirng contains 1,2,3
  18.   When add is called
  19.   Then the result is 6
  20.  
  21.   etc……

 

lots of repeated code here, even using a regex to simplify the step definitions our feature file is going to get very long and difficult to maintain.

To help with just this sort of problem, Spec Flow supports the gherkin table construct this allows us to define the scenario steps separately from the data required by the steps, in this case our data is the input string “<input> and expected result <output>.

The steps are defined by a scenario outline with place holders for each of the required data values, and the data itself is defined in an examples table structure with a column for each place holder separated by the “|” pipe character.

  1. Scenario Outline: String calculator add method with valid input
  2.      Given the input stirng contains ‘<input>’
  3.      When add is called
  4.      Then the result is ‘<output>’
  5.      
  6.      Examples:
  7.      | input    | output |
  8.      |          | 0      |
  9.      | 1        | 1      |
  10.      | 2        | 2      |
  11.      | 1,2      | 3      |
  12.      | 1,2,3    | 6      |
  13.      | 1!2,3    | 6      |
  14.      | //;!1;2  | 3      |

 

When the scenario is run it loops around replacing the place holders with each row of data, until either there is an error (fail) or it runs out of data (success).

The step definitions for the scenario outline are exactly the same as for a normal step definition, using a regex to extract the values passed by the framework, and handing them into the method definition as parameters.

  1. [Binding]
  2. public class CalculatorAddSteps
  3. {
  4.     [Given(@"the input stirng contains ‘(.*)’")]
  5.     public void GivenTheInputStirngContains(string inputString)
  6.     {            
  7.     }
  8.  
  9.     [When(@"add is called")]
  10.     public void WhenAddIsCalled()
  11.     {
  12.     }
  13.  
  14.     [Then(@"the result is ‘(.*)’")]
  15.     public void ThenTheResultIs(int expected)
  16.     {            
  17.     }
  18. }

 

Using the table structure is a very powerful way of displaying the data required by a specification, and controlling the number of scenarios that need to be written.

Posted in Uncategorized | Leave a comment

The Problem with Tests

Ok so that got your attention, from someone who has spent the last few years jumping up and down about unit testing, now he has problems with it!

Actually no, its not the tests, it’s the what.   Going back over some old code the other day, and by old I mean about three months old, I had a problem, after some fairly heavy refactoring which involved combining one repository class with another, so that the repository link-to-SQL matched the database, where as the repository API matched what the business layer wanted, I noticed some push back from the tests.

The refactoring should have broken a single group of tests, those that relied on the original repository methods, but instead may tests across the application were failing, what made it a bit worse was the fact that the mass of tests made it difficult to actually predict which tests should fail because of the changes made, an important distinction, when refactoring you need to know what you expect to happen, against those cases where you have actually broken something without realising.

So what was the problem.  I have lots of tests, (a good thing), virtually all the code is covered, if I were to run a code coverage tool across the code, then the result would be in the high 80%-90% range, (you can very rarely reach 100%), but, some of these tests are fragile, which means that the test is tightly bound to the structure of the code, also some of my tests actually cover the same code many time, and these tests maybe in different test fixtures, which also added to the confusion.

So what has happened? Well all the code written in this application was test driven using the Behaviour Driven Development style of testing, and I made the error of treating my tests as inferior to the production code, I refactored the production code,  but didn’t treat the tests in the same way.  To be totally honest I am not sure that I refactored the production code as well as it could have been, which could also lead to the problems I was having.

So what have I learnt, when doing test driven development, one of the most important steps, which is actually very easy to miss is refactoring, you write a failing test, then make it pass, the temptation is to then write the next test, I find, by this point, the next test is in my head, I know where to go next! Its so important to stop at this point, write that test down on paper if you have to just so you don’t forget it, don’t write it in the code, because you will be tempted to then make it pass, as you should  never refactor on a red line!, which will start the whole process off again, just stop. Look at your production code, and refactor it, run the tests again, then go and look at your test code, all your test code, and refactor that, make sure that each test is still valid, that you are not covering the same area twice, don’t be afraid to delete, combine, or change tests, as your code develops, and changes, so should the tests, they are not immutable, they are the wrapping around your production code, and like a wrapping paper around a parcel, the shape should reflect the shape of the code they are covering.

This is another thing those silly katas can help with, just the simple muscle memory of refactoring after each test, both in the production code and in the test code can be practiced, and the action of red, green refactor will become second nature, which will help the tests to stay fresh and meaningful.

Posted in Uncategorized | Leave a comment

Code Retreat Bletchley Park March 2010

you could not find a better setting for a code retreat than Bletchley Park, the home of one of the worlds first computers and the code breakers of world war 2, or a better guide for the day than Corey Haines.

The idea behind the code retreat is to give space for developers to hone their programming skills, in an environment away from the pressures of actually having to produce.  Using a simple problem, pair programming and test driven development.  The day is split into sessions of about 45 minutes, after each session the code is deleted, and people swap pairs.  The idea is not to solve the problem, but during each session to explore different ways of coding, or aspects of the problem domain.

We worked on  Conway’s Game of Life the first time out I tried to build the domain, starting with an array representing the universe, Corey pointed out that we didn’t strictly need an array, with the guidance in subsequent sessions I concentrated on the rules and cells themselves, until in the final session of the day we looked at the game board, and how the infinite area defined by the rules could be modelled.

During the second session I was introduced to the technique of "TDD as if you meant It", which was a revelation to me, , having never really heard of it before.  Simply put a single test is written as normal but the code to make the test pass is also written within the test, as each test is added to the test fixture code, merciless refactoring pulls out the final production code, which can then eventually be pushed into its own classes.

The upshot of this technique is that only the code actually required is written, and because the final production code methods are created by extract method refactoring, the developer never actually writes a method!

The joy of the code retreat is that not only do you get to pair with many different people, but you also get the chance to use different languages and development environments, seeing ruby used in anger for the first time, displayed why the language has become so popular, the readability and simplify of the code mad my own c# code seam quite clunky in compassion.  Another feature of the code retreat was the way Corey manages to compress time, so that each 45 minute session, seamed to only last a most 10 minutes.  The amount I learnt on this single day, will take many days to digest.

The lunchtime guided tour of The National Museum of Computing was a geeks paradise, with the history of our trade laid out, and the working example of the “Colossus” being the highlight, although it does show how young our profession really is, as it was somewhat strange to see the machine I learnt programming on displayed in a museum! Or maybe its just my age.

The day ended much to quickly, but it was one of the best days I have experienced, if you get the chance then I can I can’t recommend enough attending one of these events, regardless of your experience level, you are guaranteed to lean something new, and have a great programming experience.

Thanks to everyone who arranged and attended for making such a great day, and to the staff of the National Museum of Computing and Bletchley Park for there welcome and help, and the wealth of information we received throughout the day.

I look forward to the next time.

Posted in Code Retreat | Leave a comment

Quick Quote

“Quality is never an accident;  It is always the result of high intelligent direction and skilful execution;  It represents the wise choice of many alternatives”

 

found by corey haines in a muffler shop!

Posted in Uncategorized | Leave a comment

Don’t Make It, Fake it

The gentle art of deception

Writing code kata’s is fine, you end with a small class, that has no dependencies connects to nothing, doesn’t need a network, service or database! So what do you do when you are trying to do good tdd in the real world?

Unit tests should be run frequently, which means that there should be no time penalty for running the tests, so the tests need to be quick, and because they are going to be run so often they also need to be repeatable.

Connecting to the database or running over a network takes time, and the more production code each test calls the closer that test is probably tied to the current implementation, this creates fragile tests, which break easily with even small changes to either the data or the production code. 

Creating fragile tests is bad, there is nothing worse than doing a perfectly sensible refactoring, and discovering that 50 tests have broken, or simply loading an old project from source control and discovering that because all the data has changed, none of the tests will run correctly.  Developers lose confidence in the whole concept of the tests, and the test suite, when things like this happen, which results in the tests not getting run, and all the advantages of having a test framework are lost.  Once you comment out, ignore, or delete a test, the production code that is covered by that test becomes legacy code and is a debt to the software instead of an asset.

So each unit test needs to cover the smallest amount of production code possible, but still have meaning, without relying on external sources of data or functions.  So how do you test a method that gets data from the database?

 

So if we write a test class like this

    

We have a brittle slow running test! It relies on the database and the record being in the database to pass, so its going to be time and data dependent all of which could cause the developer not to run the test as often as they should, or in the worst case actually delete it.

So what can be done?

We need to fake the database, so that the only code that is actually being tested is the code within the method we are calling.  this is done very easily simply by writing a class that implements the correct interface and stub out the method that is called, in this case returning some canned data.

Now the test is totally independent of the database, the test data coming back is canned so it is guaranteed to always be the same, and because its just all code,  the test runs very quickly.  But I am not testing the gateway class! Which is true, this class would have its own batch of tests, which when run test its functionality, again without reference to the database, each class in the whole application should be tested this way, independent of as may other external sources as possible, using fake objects to represent each of the objects dependencies.

This is all a mock or stub object is, a dummy object that behaves in a predetermined way, and may if required monitor its inputs and other parameters.  Here I have rolled my own fake object, which sometimes is the best solution but there are many frameworks out there like RhinoMocks and Moq, which create fake objects automatically from a given interface, and provide services that allow you to raise events, return multiple values and many other functions for testing using fake objects.

Posted in Programming | Leave a comment

TDD Like you mean it! Part 3

The second test and first refactoring.

Having completed the first test, it is time to start the second test. 

The Second Test

The add function takes a single number and returns that number.

This test can be written completely as at no point does the code fail to compile

 

This gives us our next failing test.  All tests must fail before they pass, this is to ensure that the test actually does something, and is able to fail.  Again to make this pass is trivial, first we need to check that the incoming string is not empty, if it is then return zero, this will continue to make the first test pass, then we simply need to return 1, to make the second test pass, the resulting production code looks like this.

 

All the tests now pass, and we can start to refractor both the test and production code, looking first at the test code we can see some duplication and the call to the add method could be moved to inside the assert call, also the first test assert parameters are the wrong way around!

 

To remove the duplicated code we use the promote to field refactoring with the field instantiation carried out in a new test setup method which has the test initialize attribute so that it runs before each test.

We can also use the inline variable refactoring to remove the result attribute and call the add method within the assert method call, refactoring unit tests must be carried out carefully as we want to promote readability, so following this we end up with test code that looks like this

 

All tests still pass, so nothing is broken.

Looking at the production code it can be seen that the add method is now doing at least two things.

 

the check to see if the input is empty is refactored out into its own method “IsInputEmpty”, and the return zero is refactored into its own method “HandleEmptyInput”

 

Why am I doing this? Don’t I end up with lots of small methods?

Solid programming principles state that a method should do one thing, and have one reason to change, we can now change the definition of empty input or how empty input is handled, both in totally different methods, so I am satisfying that rule, also by having well named methods the add function is starting to be easy to read, and contains nothing that we would not expect.

This example is again simple, but even so the rules of TDD and Solid can be applied, by practicing these principles in the kata, makes it easier to apply them in real production and test code.

 

Posted in Programming | Leave a comment

TDD Like you mean it Part 2

Some Actual Real Tests!

For these examples I will be using the Calculator Kata designed by Roy Osherove, starting with a solution containing two empty projects, the production project “CalculatorDomain” and the test project “CalculatorDomain.Tests”.

 

The first test

The calculator add method should take an empty string and return zero.

First off create the calculator test class and add the test code until the solution won’t compile

 

At this point its time to write production code, and creating the calculator class is enough to make the tests compile, so its back to the test code to continue writing the test

 

and now we require an add method in the calculator class, creating a stub of this method is enough to make the solution compile.

 

so back to the test code and finally adding the assert clause allows us to run the test, and get our first fail.

 

One of the tenants of TDD is that the code written to make a test pass should be the simplest possible, in this case we simply have to return 0!  Which makes the test pass.

As this small demonstration shows calling this development technique “Test First Development”, is not strictly accurate, as the test and production code is actually written concurrently, which makes the idea of writing the tests a lot easier, because the test is written in concert with the production code, the result is all the code written, is by definition testable, which as many can testify cannot be said of code which is written before tests.

Once a test is passing, the next stage of the development process is refactoring, which is where duplication in the code is removed and the structure of the class is created, methods are simplified, and the code is structured to follow the “SOLID” principles of good code design. The tests are then rerun to ensure that the code still behaves as expected.

In this case we only currently have one test so there is no refactoring to do yet, and the production code is as simple as it can be so again no refactoring.

Next time test two!

Posted in Programming | Leave a comment

Test Driven Development

Testing like it or not is now mainstream, just about every version of visual studio 2010 has the ability to run native Microsoft unit tests, and there is a plethora of unit testing frameworks out there to choice from for those who don’t like the Microsoft “way”.   So there is really no escape, and as we move towards automated rollouts, and a defined build cycle, the requirement to “prove” that the code works has moved from being nice to have being a strict required!

TDD is a technique, which along with traditional design  methods and programming patterns, help the developer produce well structured, encapsulated code, which is easy to maintain and extend.  TDD on its own will not create great code, but it is the corner stone on which great code is written,   One of upshots of working in the TDD way is that the time spend debugging is reduced, after writing code there is no debugging time, which can over a project lifespan save a considerable amount of time, and money.

TDD is based on three simple rules, which lead to the programmer too writing code in a tight loop between the test class and the production code.

Rule 1: You are not allowed to write any production code, unless it is to make a failing test pass.

Rule 2: You are not allowed to write any more of unit test code than is sufficient to fail, and not compiling is failing.

Rule 3: You are not allowed to write any more production code than is sufficient to pass the one failing test

This is not easy (nothing worthwhile ever is!), So like any skill to be effective it must be practiced, and that is what the code kata’s are about, these are simple small programming problems, where the solution is easy to arrive at, but the process of repeating the kata trains the muscles to that you instinctively start coding by writing the test first, and building from there.

Next time some code examples!

Posted in Uncategorized | Leave a comment

Installing VS2010 Beta 2

I have just installed visual studio 2010 beta 2, on my windows 7 laptop, it went fairly well, this was a dvd install and not the web version that is available, which might be different.

My only problem has been that I had already installed Expression Studio 3, so had Silverlight already installed, choosing the default full install of visual studio threw an error when it tried to install Silverlight! So if you have Silverlight installed already it might be an idea to remove it before running the VS2010 beta 2 install. also I noted at work that behind a proxy can cause problems with the documentation, which would not install, which is not a huge problem as I tend to use online documentation as a default anyway.

Love the new interface, very crisp and very fast even on a laptop, although I am not sure of the dark blue colour, and I do wish I could close the menu bar etc so I could get the maximum available real-estate for my code windows, but minimising the menu bar closes all the windows as well even though they are detached.

I didn’t try the beta one very much, other than to fire up the VPC, but this version seams very stable, I am just waiting for Resharper, before giving this a serious workout.

Posted in Computers and Internet | Leave a comment

My First Ever Screencast

Well I have at last put my code where my mouth is 😉 and uploaded a screencast, this is of me carrying out a simple code kata, something I have been doing in private for a few months now, I started with Uncle Bobs bowling kata, and then got introduced to Roy Osherove’s String Calculator kata.

The idea of the kata is that you memorise it, and then do it every day, I spend 25 minutes doing the kata first thing in the morning, the idea is that it builds muscle memory, and the habit of test driving your designs, which I have found to be very useful.

One final part of the kata is to perform it in public, so you can get feedback and gain insight into your code style, with the aim of improvement, so with that in mind I present for your perusal and comment

The String Calculator Kata.

Posted in Uncategorized | Leave a comment