Search This Blog

Wednesday, 28 April 2010

SqLite Manager plugin for firefox

There is a good chance some of you use this or know about this already. However this is kind of new stuff for me.. In my current project we use SqLite  to run our integration tests for the repository tests and the some of the rhino ETL process classes. We use SqLite as a file based database rather than an in memory instance and while i was debugging through the code I hit a point where i wanted to see how the schema was created and the data stored, so i can be sure the foreign keys where stored with the right values

SqLite Manager is a useful plugin for Firefox 3.6. This is browser based and allows you to work on the file based database just like Sql Server Enterprise Manager allows you to work with SQL 2000. This is a really useful tool when you are writing integration tests with SqLite.

You can get the plugin at , you may need firefox 3.6 or above

https://addons.mozilla.org/en-US/firefox/addon/5817

Sunday, 25 April 2010

NUnit - Teamcity - TestCaseSource tip

If you are using features from NUnit 2.5 or above TestCaseSource attribute may sound familiar. This is a very useful feature in NUnit now which helps us reduce the clutter of repetitive code in our test class. I am sure everyone can see how this can be done on the NUnit website as well , but the point of this post is in relation to a problem I ran into when I used the TestCaseSource attribute. To elaborate on this let me use a simple example of a class which calculates the square of an integer.

public class SquareOfNumbers
{
   public static int Square(int x)
{
   return x * x;
}
}

When i first started writing tests i used the TestCaseSource attribute , so my test class looked like the one below

[TestFixture]

public class SquareOfNumberFixture
{
private static readonly Dictionary<int, int> _testData = new Dictionary<int, int>();

public static Dictionary<int, int> TestData
{
   get
   {
       _testData.Add(2, 4);
       _testData.Add(0, 0);
       return _testData;
   }
}

[Test, TestCaseSource("TestData")]
public void Test(KeyValuePair<int, int> keyValuePair)
{
   Assert.That(SquareOfNumbers.Square(keyValuePair.Key ), Is.EqualTo(keyValuePair.Value));
}
}

These tests run successfully, but when run as part of my teamcity build there is no detail about what the tests where as in there was no reference to the name of my test method. I did spend sometime trying to fix this and found a couple of issues.

1. Team city was not using the same nunit version that I was using to write tests, so the plugin had to be updated on team city, this way i made sure the build uses the same nunit framework as my source code does

2. I changed my tests to use TestCaseData class in conjunction with the TestCaseSource attribute, this allowed me to specify a name for each test scenario , so after refactoring the test class , I can now see the test names on the nunit test report generated by Team city, the code for the new test class is shown below

In addition to fixing the problem , TestCaseData class made the test code more readable.

[TestFixture]
public class SquareOfNumberFixture
{
  public static IEnumerable TestData
  {
      get
      {
          TestCaseData squareOfZeroTest = new TestCaseData(0).Returns(0);
          squareOfZeroTest.SetName("SquareOfZeroReturnsZero");
          yield return squareOfZeroTest;

          TestCaseData squareOfIntegerTest = new TestCaseData(2).Returns(4);
          squareOfIntegerTest.SetName("SquareOfTwoReturnsFour");
          yield return squareOfIntegerTest;
      }
  }

  [Test, TestCaseSource("TestData")]
  public int TestSquareOfNumbers(int x)
  {
      return SquareOfNumbers.Square(x);
  }
}

On second thoughts further refactoring the class makes it more readable if i had done the following…

[TestFixture]

public class SquareOfNumberFixtureNew
{
  [TestCase(0, Description = "SquareOfZero", Result = 0)]
  [TestCase(2, Description = "SquareOfTwo", Result = 4)]
  [Test]
  public int TestSquareOfNumbers(int x)
  {
           return SquareOfNumbers.Square(x);
  }
}

Thursday, 22 April 2010

Acceptance Testing – Cucumber? Specflow? Cuke4Nuke

I have been pretty much dormant on this blog due to my part time MBA coupled with a some tight deadlines at work. And to get me back into the blog bandwagon I just thought I will do this small pet project of mine outside work for which I have been exploring the acceptance testing tool to use infact functional test tool which will allow me to translate my acceptance criteria on stories to tests from plain english.

I found something about Specflow on a few blogs,it is 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. You could use specflow and drive WATIN.

Being a .Net developer I am quite inclined to use Specflow and Watin after having had a look at it mainly because i dont need to learn a new language and i can use my c# knowledge to write tests, However previous experience with WATIN a couple of years was not great and it always seemed be behind some of the things you could do with WATIR, hopefully this has changed now, In my current job we use Cucumber + Watir to write acceptance tests and we use Ruby, Although I found it a bit steep on the learning curve initially and mainly because i havent worked in anything but .Net. Now after a couple of weeks into it I am beginning to think if this is may be the right choice, for e.g I am not even a novice in Ruby but RubyMine from JetBrains makes it easy when you are a new bee.

I still didnt want to give up exploring a .Net + cucumber combination and my search led me to this article by Richard Lawrence in his blog which compares the pros and cons of Watir and Watin.. go to . Interestingly Richard and a group of other people started driving this project called Cuke4Nuke sometime ago now which should allow us to write tests in C# for cucumber, That sounds great to me, because I am not looking at any advanced usage of cucumber, i guess my needs are pretty basic for now, I have not used it yet but all i can say at the moment is having looked at the screen cast and the background the creators of the project have given it quite a bit of thought on performance (if you read the blog you will find that they did explore using Iron Python over WATIN which was horribly slow).

A couple of months ago they released 0.3 which supports almost everything you can do with Cucumber in Ruby or Java, making C# a first class language for Cucumber. (The only missing features are small things like tags on Before and After hooks and a richer Table object.)

To install and have a play with this go to the Cuke4Nuke Wiki at http://wiki.github.com/richardlawrence/Cuke4Nuke/

Screen Cast on Cuke4Nuke and WATIN

http://www.richardlawrence.info/2009/12/03/screencast-testing-web-applications-in-net-with-cuke4nuke-and-watin/

I am going to give this a go for a spike on my new project so lets see how this goes :).