Last week I wrote about using cucumber to test your web applications outside in. In the post I showed a simple example (actually comes with cucumber) to open a browser, go to a site and perform some actions.
I showed the same to my team at work and they mention that could be nice to been able to use c# and Visual Studio to do something similar.
After thinking about it I decided to give another try to a tool I check a few month ago. SpecFlow.
SpecFlow is a tool and a set of libraries to write specifications using Gherkin. You can check the screencast here or read this post from Ryan Lanciaux. SpecFlow can use either NUnit or MsTest as the engine.
Now I needed something to drive the browser. WatiN is the .Net port of WatiR and is very easy to use. The problem is that needs to run in a single thread process, so when called from inside an NUnit test we need to add the STAThreadAttribute to the Test method.
Of course, this is not a problem when you are writing the Test methods yourself but the way SpecFlow works is that the actual Test method is generated by a tool from your .feature file.
The code looks like this (actually I edited it a little bit to make it nicer).
So you need to add the STAThreadAttribute to this method.
This works great and I was able to drive the browser and write a few test in no time. The problem is that if you edit every time you edit the .feature file this class will be re-generated and you will have to re-add this attribute. (Note: I forked the repo on GitHub, made a change to the source code and sent a pull request to add this attribute by default, not sure if it will be accepted or not).
I think that the integration from SpecFlow with VS is great. If you or your team feel very strong on using VS this is a very good tool (there are versions for both VS 2008 and 2010). After writing the features and running the test you will have hints and code to copy and paste from the TestRunner into the specification class.
You will generate the specification from an item template.
Conclusion:
I think that this is a great tool, specially if you are using it to develop desktop apps or library code in c#. You can use tools like R# to generate your classes and methods from the step definition class directly.
For using with WatiN or similar tools to drive the browser I will probably keep using pure cucumber with Ruby at least until the STAThread issue gets solved.