Click here to Skip to main content
15,893,814 members
Articles / Web Development / ASP.NET

MbUnit : Generative Unit Test Framework

Rate me:
Please Sign up or sign in to vote.
4.70/5 (38 votes)
15 Apr 20046 min read 222K   496   120  
A new highly flexible unit test framework with new fixtures
<?xml version="1.0"?>
<doc>
    <assembly>
        <name>GUnit.Framework</name>
    </assembly>
    <members>
        <member name="T:GUnit.Framework.Testers.CollectionIndexingTester">
            <summary>
            Collection indexing test class
            </summary>
            <!-- No matching elements were found for the following include tag --><include file="GUnit.Framework.Testers.Doc.xml" path="dot/remarkss/remarks[@name='CollectionIndexingTester']"/>
        </member>
        <member name="T:GUnit.Framework.Testers.CollectionOrderTester">
            <summary>
            Collection order tester class.
            </summary>
        </member>
        <member name="T:GUnit.Framework.Testers.EnumerationTester">
            <summary>
            Tests for the <seealso cref="T:System.Collections.IEnumerable"/> and <seealso cref="T:System.Collections.IEnumerator"/>.
            </summary>
        </member>
        <member name="T:GUnit.Framework.CollectionIndexingFixtureAttribute">
            <summary>
            Collection indexing pattern.
            </summary>
            <remarks name="CollectionIndexingFixtureAttribute">
<para>
The <see cref="T:GUnit.Framework.CollectionIndexingFixtureAttribute"/> implements the <b>Collection Indexing Pattern</b>.
</para>
<para>
The user provides filled collection, index type and index range through
the <see cref="T:GUnit.Core.Framework.IndexerProviderAttribute"/> attribute.
</para>
</remarks>
            <example name="Indexing">
<para>
This example checks the Collection Indexing Pattern for the <see cref="T:System.Collections.ArrayList"/>
and <see cref="T:System.Array"/> collections:
</para>
<code><![CDATA[
[CollectionIndexingFixture]
public class CollectionIndexingFixtureAttributeTest
{		
    [IntIndexerProvider(typeof(ArrayList),100)]
    public ArrayList ProvideArrayList100()
    {
        ArrayList list = new ArrayList();
        for(int i =0;i<100;++i)
            list.Add(i);	 	
		 	
        return list;
    }
		 
    [IntIndexerProvider(typeof(Array),10)]
    public ArrayList ProvideArrayList()
    {
        ArrayList list = new ArrayList();
        for(int i=0;i<10;++i)
            list.Add(i);
		 	
        return list;
    }
}
]]>
</code>
</example>
        </member>
        <member name="M:GUnit.Framework.CollectionIndexingFixtureAttribute.#ctor">
            <summary>
            Default constructor
            </summary>
        </member>
        <member name="M:GUnit.Framework.CollectionIndexingFixtureAttribute.#ctor(System.String)">
            <summary>
            Constructor with fixture description
            </summary>		
        </member>
        <member name="M:GUnit.Framework.CollectionIndexingFixtureAttribute.GetRun">
            <summary>
            Creates the execution logic
            </summary>
            <remarks>
            See summary.
            </remarks>
            <returns>A <see cref="T:GUnit.Core.Runs.IRun"/> instance that represent the type
            test logic.
            </returns>
            <example name="Indexing">
<para>
This example checks the Collection Indexing Pattern for the <see cref="T:System.Collections.ArrayList"/>
and <see cref="T:System.Array"/> collections:
</para>
<code><![CDATA[
[CollectionIndexingFixture]
public class CollectionIndexingFixtureAttributeTest
{		
    [IntIndexerProvider(typeof(ArrayList),100)]
    public ArrayList ProvideArrayList100()
    {
        ArrayList list = new ArrayList();
        for(int i =0;i<100;++i)
            list.Add(i);	 	
		 	
        return list;
    }
		 
    [IntIndexerProvider(typeof(Array),10)]
    public ArrayList ProvideArrayList()
    {
        ArrayList list = new ArrayList();
        for(int i=0;i<10;++i)
            list.Add(i);
		 	
        return list;
    }
}
]]>
</code>
</example>
        </member>
        <member name="T:GUnit.Framework.CollectionOrderTest">
            <summary>
            Different collection order
            </summary>
        </member>
        <member name="F:GUnit.Framework.CollectionOrderTest.OrderedAscending">
            <summary>Tests ascending order collection</summary>
        </member>
        <member name="F:GUnit.Framework.CollectionOrderTest.OrderedDescending">
            <summary>Tests ascending order collection</summary>
        </member>
        <member name="T:GUnit.Framework.CollectionOrderFixtureAttribute">
            <summary>
            Collection Order Pattern implementations.
            </summary>
            <remarks name="CollectionSortFixtureAttribute">
<para><em>Implements:</em> Collection Order Pattern</para>
<para><em>Logic:</em>
<code>
{Provider}
[SetUp]
{Fill}
(Order)  // internal
[TearDown]
</code>
</para>
<para>
This fixture tests sorted collections. The user must provider a
comparer and the type of desired test based on the <see cref="T:GUnit.Framework.CollectionOrderTest"/>
enumeration: ascending, descending.
</para>
<para>
Tested collections are provided by methods tagged with the <see cref="T:GUnit.Core.Framework.ProviderAttribute"/>
attribute. The collection are then filled using methods tagged by the 
<see cref="T:GUnit.Core.Framework.FillAttribute"/> attribute. The rest of the tests is handled by the framework.
</para>
<para>
SetUp and TearDown methods can be added to set up the fixtue object.
</para>
</remarks>
        </member>
        <member name="T:GUnit.Framework.EnumerationFixtureAttribute">
            <summary>
            Enumeration Pattern implementations.
            </summary>
            <remarks name="EnumerationFixtureAttribute">
<para><em>Implements:</em>Enumeration Test Pattern</para>
<para><em>Login:</em>
<code>
{DataProvider}
{CopyToProvider}
[SetUp] 
(EnumerationTester)
    - GetEnumerator
    - Enumerate
    - ElementWiseEquality
    - Current
    - CurrentWithoutMoveNet
    - CurrentPastEnd
    - Reset
    - CollectionChanged 
[TearDown]
</code>
</para>
<para>
This example tests the <seealso cref="T:System.Collections.IEnumerable"/> and <seealso cref="T:System.Collections.IEnumerator"/>.
</para>
</remarks>
        </member>
        <member name="M:GUnit.Framework.EnumerationFixtureAttribute.GetRun">
            <summary>
            </summary>		
        </member>
        <member name="T:GUnit.Framework.NamespaceDoc">
            <summary name="GUnit.Framework">
<para>
The <b>GUnit.Framework</b> contains fixture that implement <b>Unit Test Patterns</b> 
as defined in the Marc Clifton article.
</para>
</summary>
        </member>
        <member name="T:GUnit.Framework.TestFixtureAttribute">
            <summary>
            Simple Test Pattern fixture.
            </summary>
            <remarks name="TestFixtureAttribute">
<para><em>Implements:</em> Simple Test Pattern</para>
<para><em>Login:</em>
<code>
[SetUp] 
{Test} 
[TearDown]
</code>
</para>
<para>
This is the classic unit test fixture attribute. It defines a class that contains unit
tests.
</para>
<para>
The test execution logic is described by the following sequence of custom attributes:
where <c>[]</c> denotes an optional attribute, <c>{}</c> denotes a custom attribute
that can tag multiple number of methods.
</para>
<para>
Unit test methods must be tagged with the <see cref="T:GUnit.Framework.TestFixtureAttribute"/>, return
<c>void</c> and take no arguments:
<code>
[Test]
public void UnitTest()
{
   ...
}
</code>
The same fixture can hold an arbitrary number of unit test methods.
</para>
<para>
If the fixture needs initilization, you can add a set up method tagged with the
<see cref="T:GUnit.Core.Framework.SetUpAttribute"/> attribute. Note that there can be only one 
method tagged with <see cref="T:GUnit.Core.Framework.SetUpAttribute"/>.
</para>
<para>
Symmetricaly, you can specify a method that will clean up resources allocated by
the fixture. This method must be tagged with the <see cref="T:GUnit.Core.Framework.TearDownAttribute"/>
and there can be only one method with this attribute.
</para>
</remarks>
            <example name="GraphicsBitmap">
<para>
This example shows a test fixture class implementing the <b>Simple Test pattern</b>.
It tests image based method of the Graphics class in GDI+. 
</para>
<para>
A set up method
(tagged by <see cref="T:GUnit.Core.Framework.SetUpAttribute"/> is used to create a new bitmap, while
a tear down (tagged by <see cref="T:GUnit.Core.Framework.TearDownAttribute"/>) is used to released the bitmap.
</para>
<code>
[TestFixture("Bitmap")]
public GraphicsAndBitmapTest
{
    private Bitmap bmp;
    
    [SetUp]
    public void SetUp()
    {
       this.bmp = new Bitmap(300,300);
    }
    
    [Test]
    public void CreateGraphics()
    {
    	Graphics g = Graphcis.FromImage(this.bmp);
    	Assert.IsNotNull(g);
    	Assert.AreEqual(g.Width,this.bmp.Width);
    	...
    }
    
    ...
    
    [TearDown]
    public void TearDownCanHaveOtherNames()
    {
    	if(this.bmp!=null)
	    	this.bmp.Dispose();
    }
}
</code>
</example>
        </member>
        <member name="M:GUnit.Framework.TestFixtureAttribute.#ctor">
            <summary>
            Default constructor
            </summary>
            <remarks>
            </remarks>
        </member>
        <member name="M:GUnit.Framework.TestFixtureAttribute.#ctor(System.String)">
            <summary>
            Constructor with a fixture description
            </summary>
            <param name="description">fixture description</param>
            <remarks>
            </remarks>
        </member>
        <member name="M:GUnit.Framework.TestFixtureAttribute.GetRun">
            <summary>
            Creates the execution logic
            </summary>
            <remarks>
            See summary.
            </remarks>
            <returns>A <see cref="T:GUnit.Core.Runs.IRun"/> instance that represent the type
            test logic.
            </returns>
            <example name="GraphicsBitmap">
<para>
This example shows a test fixture class implementing the <b>Simple Test pattern</b>.
It tests image based method of the Graphics class in GDI+. 
</para>
<para>
A set up method
(tagged by <see cref="T:GUnit.Core.Framework.SetUpAttribute"/> is used to create a new bitmap, while
a tear down (tagged by <see cref="T:GUnit.Core.Framework.TearDownAttribute"/>) is used to released the bitmap.
</para>
<code>
[TestFixture("Bitmap")]
public GraphicsAndBitmapTest
{
    private Bitmap bmp;
    
    [SetUp]
    public void SetUp()
    {
       this.bmp = new Bitmap(300,300);
    }
    
    [Test]
    public void CreateGraphics()
    {
    	Graphics g = Graphcis.FromImage(this.bmp);
    	Assert.IsNotNull(g);
    	Assert.AreEqual(g.Width,this.bmp.Width);
    	...
    }
    
    ...
    
    [TearDown]
    public void TearDownCanHaveOtherNames()
    {
    	if(this.bmp!=null)
	    	this.bmp.Dispose();
    }
}
</code>
</example>
        </member>
        <member name="T:GUnit.Framework.TypeFixtureAttribute">
            <summary>
            Type fixture pattern implementation.
            </summary>
            <remarks name="TypeFixtureAttribute">
<para><em>Implements:</em> Type Test Pattern</para>
<para><em>Logic:</em>
<code>
{Provider}
[SetUp]
{Test}
[TearDown]
</code>
</para>
<para>
This fixture is quite similar to the <b>Simple Test</b> pattern, but it applies to
any instance of a particular type provided by the user.
</para>
<para>
The test fixture first looks for methods tagged with the <see cref="T:GUnit.Core.Framework.ProviderAttribute"/>
method. These method must return an object assignable with the tested type. This instance will
be feeded to the other methods of the fixture.
</para>
</remarks>
            <example name="IDictionary">
<para>
This example shows the squeleton of a fixture tests the <b>IDictionary</b> interface,
the fixture implements the <b>Type Test</b> pattern.
</para>
<para>
The tested instances are feeded by the methods tagged with the <see cref="T:GUnit.Core.Framework.ProviderAttribute"/>.
These methods must return an instance that is assignable with <see cref="T:System.Collections.IDictionary"/>.
Subsequent will receive the created instance as parameter.
</para>
<code>
[TypeFixture(typeof(IDictionary),"IDictionary interface fixture")]
public void DictionaryTest
{
    [Provider(typeof(Hashtable))]
    public Hashtable ProvideHashtable()
    {
    	return  new Hashtable();
    }

    [Provider(typeof(SortedList))]
    public SortedList ProvideSortedList()
    {
    	return  new SortedList();
    }
    
    // tests
    [Test]
    [ExpectedException(typeof(ArgumentException))]
    public void AddDuplicate(IDictionary dic) // dic comes from a provider class
    {
		dic.Add("key",null);
		dic.Add("key",null); // boom
    }

}
</code>
</example>
        </member>
        <member name="M:GUnit.Framework.TypeFixtureAttribute.#ctor(System.Type)">
            <summary>
            Creates a fixture for the <paramref name="testedType"/> type.
            </summary>
            <remarks>
            Initializes the attribute with <paramref name="testedType"/>.
            </remarks>
            <param name="testedType">type to apply the fixture to</param>
            <exception cref="T:System.ArgumentNullException">testedType is a null reference</exception>
        </member>
        <member name="M:GUnit.Framework.TypeFixtureAttribute.#ctor(System.Type,System.String)">
            <summary>
            Creates a fixture for the <paramref name="testedType"/> type 
            and a description
            </summary>
            <remarks>
            Initializes the attribute with <paramref name="testedType"/>.
            </remarks>
            <param name="testedType">type to apply the fixture to</param>
            <param name="description">description of the fixture</param>
            <exception cref="T:System.ArgumentNullException">testedType is a null reference</exception>
        </member>
        <member name="M:GUnit.Framework.TypeFixtureAttribute.GetRun">
            <summary>
            Creates the execution logic
            </summary>
            <remarks>
            See summary.
            </remarks>
            <returns>A <see cref="T:GUnit.Core.Runs.IRun"/> instance that represent the type
            test logic.
            </returns>
            <example name="IDictionary">
<para>
This example shows the squeleton of a fixture tests the <b>IDictionary</b> interface,
the fixture implements the <b>Type Test</b> pattern.
</para>
<para>
The tested instances are feeded by the methods tagged with the <see cref="T:GUnit.Core.Framework.ProviderAttribute"/>.
These methods must return an instance that is assignable with <see cref="T:System.Collections.IDictionary"/>.
Subsequent will receive the created instance as parameter.
</para>
<code>
[TypeFixture(typeof(IDictionary),"IDictionary interface fixture")]
public void DictionaryTest
{
    [Provider(typeof(Hashtable))]
    public Hashtable ProvideHashtable()
    {
    	return  new Hashtable();
    }

    [Provider(typeof(SortedList))]
    public SortedList ProvideSortedList()
    {
    	return  new SortedList();
    }
    
    // tests
    [Test]
    [ExpectedException(typeof(ArgumentException))]
    public void AddDuplicate(IDictionary dic) // dic comes from a provider class
    {
		dic.Add("key",null);
		dic.Add("key",null); // boom
    }

}
</code>
</example>
        </member>
    </members>
</doc>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Engineer
United States United States
Jonathan de Halleux is Civil Engineer in Applied Mathematics. He finished his PhD in 2004 in the rainy country of Belgium. After 2 years in the Common Language Runtime (i.e. .net), he is now working at Microsoft Research on Pex (http://research.microsoft.com/pex).

Comments and Discussions