Monday, January 10, 2005

This is a rather funny application of unit testing. Let me state the problem:

We need to write a wrapper for every type that inherits from C. To make sure we don't miss any, we start by writing a combinatorial test that makes sure every type is mapped with a wrapper type*.

[TestFixture]
public class WrapperTest
{
    // get all the types assignable to C, from the assembly SomeAssembly
    [Factory(typeof(Type))]
    public IEnumerable<Type> Types()
    {
        foreach(Type type in SomeAssembly.GetExportedTypes())
        { 
            if (typeof(C).IsAssignableFrom(type))
               yield return type;
        }
    } 

    // tests that all types have a wrapper
    [CombinatorialTest]
    public void EnsureWrapper(
        [UsingFactories("Types")]Type type
    )
    {
        Assert.IsTrue( IsWrapped(type) );
    }
    
    // returns true of the wrapper exists
    private bool IsWrapped(Type type)
    {...}
}

Ok, we can run this test and see a lot of failures. So now we know wich wrapper has to be written.... but wait, why not making the test generate the wrapper as well.

    [CombinatorialTest]
    public void EnsureWrapper(
        [UsingFactories("Types")]Type type
    )
    {
        if (IsWrapped(type))
            return;

        // wrapper not found, 
        // generating the code and dumping it to Console.Out
        ...

        Assert.Fail("Wrapper not found");
    }

Now, we have test cases that give us the fix in case of a failure :)

*: this could be done by a FxCop rule as well since it is pure static analysis.

posted on Monday, January 10, 2005 8:13:00 AM UTC  #    Comments [5]
Tracked by:
"online poker tournaments" (online poker tournaments) [Trackback]
Monday, June 06, 2005 4:51:57 PM UTC
Or you could change IsWrapped() to GetWrapper() and generate the code at runtime. IIRC that's how serialization works :)
Graeme Foster
Monday, June 06, 2005 4:51:57 PM UTC
ISerializable
Monday, June 06, 2005 4:51:57 PM UTC
Hi Jonathan,
<br>I tried to contact you through your contact page on this side, but it failed. Failed also, when I tried to send you an e-Mail directly. You should take a look at that.
Hermann Klinke
Monday, June 06, 2005 4:51:57 PM UTC
Jonathan,
<br>
<br>Great work with <a title="MbUnit, Generating Unit Testing and Model Based Testing Framework for .NET Framework" href="http://mbunit.tigris.org" target="_blank">MbUnit</a>. I've put together a asp webform testing framework that runs in the context of the asp.net worker process. A webservice exposes the available tests, and they are added dynamically into <a title="MbUnit, Generating Unit Testing and Model Based Testing Framework for .NET Framework" href="http://mbunit.tigris.org" target="_blank">MbUnit</a>.
<br>
<br>I expect we'll be writing a significant number of tests for our company (in the hundreds per month range), so we are keenly interested in the ability to assign categories.
<br>
<br>Whether it be by test suite, testcase, or some other mechanism, I'm open - anything would be appreciated.
<br>
<br>Are the category definitions exposed anywhere? Is there like a TestSuites collection? Otherwise, the only thing I can think of is to dynamically create methods, so I can access the attributes. that or create the tests within a <a title="MbUnit, Generating Unit Testing and Model Based Testing Framework for .NET Framework" href="http://mbunit.tigris.org" target="_blank">MbUnit</a> framework, which then transmits the methods via the webservice to the webserver... ugly stuff.
<br>
<br>Any help is much appreciated!
<br>
<br>--ben joldersma
<br>scout.com
Benjamin Joldersma (benj at scout dot com)
Monday, June 06, 2005 4:51:58 PM UTC
Wow, superb!
<br>Thanks.
Diana
Comments are closed.