I have just finished a CodeSmith template that "automatically" generates empty tests case for a given type.
This template does a little bit more than just enumerating the available methods: it explores MSIL using RAIL and creates test case following the given rules:
- if the IL instruction is a conditional branch, create a test case for both possibilities (true/false case)
- if the IL instruction throws, create a test case expecting the exception,
- if the IL instruction is returning, create a test case that checks the returned value
Those rules are quite simplistic but can already generate a "huge" number of test case automatically. Each test case comes with a piece of documentation and the method IL code where the target instruction has been set in bold. Currently, the documentation contains IL code, but in the future it would be nicer to output real code, using Reflector for example.
In order to make the template work, you need to put RAIL assemblies and the AssemblyHelper assembly in the CodeSmith directory.
Here's a quick example. The method:
public void ABitMoreComplext(bool goForIt)
{
if (goForIt)
throw new Exception();
else
Console.Write("did not throw");
// other instruction
Console.Write("hello");
}
and the resulting output in the generated class:
/// <summary>
/// Tests ABitMoreComplext method when condition is executed as true
/// (see remarks).
/// </summary>
/// <remark>
/// <para>
/// Not implemented
/// </para>
/// <code>
/// ldarg.1
/// <b>brfalse.s</b>
/// newobj
/// throw
/// ldstr
/// call
/// ldstr
/// call
/// ret
/// </code>
/// </remarks>
[Test]
[Ignore]
public void ABitMoreComplextIfTrue1()
{
throw new NotImplementedException();
}
/// <summary>
/// Tests ABitMoreComplext method when condition is executed as true
/// (see remarks).
/// </summary>
/// <remark>
/// <para>
/// Not implemented
/// </para>
/// <code>
/// ldarg.1
/// <b>brfalse.s</b>
/// newobj
/// throw
/// ldstr
/// call
/// ldstr
/// call
/// ret
/// </code>
/// </remarks>
[Test]
[Ignore]
public void ABitMoreComplextIfFalse1()
{
throw new NotImplementedException();
}
/// <summary>
/// Tests that the ABitMoreComplext method throws (see remarks)
/// </summary>
/// <remark>
/// <para>
/// Not implemented
/// </para>
/// <code>
/// ldarg.1
/// brfalse.s
/// newobj
/// <b>throw</b>
/// ldstr
/// call
/// ldstr
/// call
/// ret
/// </code>
/// </remarks>
[Test]
[Ignore]
[ExpectedException(typeof(Exception))]
public void ABitMoreComplextThrow3()
{
// don't forget to update the exception type.
throw new NotImplementedException();
}
ps: The template is called TestFixture and is in the Templates directory in the MbUnit CVS.