Thoughts on automatically generating Test Methods in a Team Developer Edition Test Project

With my current implementation of the GUITesting framework integrated with Test Projects within VS.Net, I end up with all the GUITests in a single [TestMethod]. This is a far from perfect solution, if nothing else it is hard to debug, you only see the first failing test.

So I thought what I need to do is automatically generate [TestMethod] flagged methods at runtime in something like the form below in the runtime UnitTest class within the Test Project.

[TestMethod]
public void TestMethod2()

   // in the constructor put all the test in an array 
   // then call a helper method to do the test 
   DoTest(tests[2]);
}

So how to do this? Well I don’t have a working answer at present, but my initial attempts have raised a few questions:

  • If I used Reflector to get the IL code for the above method, I could use the DynamicMethod system to generate a set of [TestMethod] methods. The best example of using this I have found is on Joel Pobar’s blog. However, I have not worked out how to put the actual test attribute into the IL code, and I am unclear if I can add the method to the current running containing class or have to create a new dynamic assembly. If I cannot do the former then this method is not going to work.
  • Probably more importantly if I don't put the [TestMethod] methods in test project at design time the testing harness in VS.Net fails to run as it says there is no tests defined. I am not sure of a way around this if I generate them at runtime. It seems to me that the [TestMethod] framework only allows a single test result per method (not unsurprising really).
  • It would be nice of the UnitTest framework had a way to return a number of tests from a single defined [TestMethod], it would remove complexity for me.

More I am sure to follow.