views:

305

answers:

2

I've got a LOT of tests written for a piece of software (which is a GREAT thing) but it was built essentially as a standalone test in C#. While this works well enough, it suffers from a few shortcomings, not the least of which is that it isn't using a standard testing framework and ends up requiring the person running the test to comment out calls to tests that shouldn't be run (when it isn't desired to run the entire test 'suite'). I'd like to incorporate it into my automated testing process.

I saw that the Test Edition of VS 2008 has the notion of a 'Generic Test' that might do what I want, but we're not in a position to spend the money on that version currently. I recently started using the VS 2008 Pro version.

These test methods follow a familiar pattern:

  • Do some setup for the test.
  • Execute the test.
  • Reset for the next test.

Each of them returns a bool (pass/fail) and a string ref to a fail reason, filled in if it fails.

On the bright side, at least the test methods are consistent.

I am sitting here tonight contemplating the approach I might take tomorrow morning to migrate all this test code to a testing framework and, frankly, I'm not all that excited about the idea of poring over 8-9K lines of test code by hand to do the conversion.

Have you had any experience undertaking such a conversion? Do you have any tips? I think I might be stuck slogging through it all doing global search/replaces and hand-changing the tests.

Any thoughts?

+1  A: 

You're about to live through the idiom of "An Ounce of Prevention is worth a pound of cure". Its especially true in programming.

You make no mention of NUnit(which I think was bought by Microsoft for 2008, but don't hold me to that). Is there a paticular reason you didn't just use NUnit in the first place?

Jonathan Beerhalter
The tests were written by someone unfamiliar with NUnit at the time. I'm thrilled that they wrote any tests at all, but its my job to get it all converted over.
itsmatt
+4  A: 

If you use NUnit (which you should), you'll need to create a new test method for each of your current test methods. NUnit uses reflection to query the test class for methods marked with the [Test] attribute, which is how it builds its list of the tests that show up in the UI, and the test classes use the NUnit Assert method to indicate whether they've passed or failed.

It seems to me that if your test methods are as consistent as you say, all of those NUnit methods would look something like this:

[Test]
public void MyTest()
{
   string msg;
   bool result = OldTestClass.MyTest(out msg);
   if (!result)
   {
      Console.WriteLine(msg);
   }
   Assert.AreEqual(result, true);

}

Once you get that to work, your next step is to write a program that uses reflection to get all of the test method names on your old test class and produces a .cs file that has an NUnit method for each of your original test methods.

Annoying, maybe, but not extremely painful. And you'll only need to do it once.

Robert Rossney
Thanks, Robert - yeah, I've been using NUnit since I started writing C# code and it is a life saver. Your solution works great (I've already written the app to do the conversion) and I thank you much! Take care!
itsmatt