The TypeFixture lets you write a fixture for specific type and apply to a number of instance of this type.This fixture is particularly usefull for writing fixtures of interfaces and apply it to all the types that implement the interface.
In this tutorial, we are going to write a fixture for the IEnumerable interface (note that this interface is best tested with EnumerationFixture).
IEnumerable
EnumerationFixture
The TypeFixture has the following execution logic:
TypeFixture
SetUpAttribute
ProviderAttribute
ProviderFactoryAttribute
TestAttribute
TearDownAttribute
Create a new class EnumerableTest and tag it with TypeFixture. The TypeFixture attribute takes the tested type as parameter:
EnumerableTest
using System; using System.Collections; using MbUnit.Core.Framework; using MbUnit.Framework; [TypeFixture(typeof(IEnumerable))] public EnumerableFixture { }
MbUnit needs instance of the tested type to feed them into the different tests. Creating those instances is the job of tester. To do so, you can either
We start by writing two methods that return respectively and enumerator on a empty list and non-empty list:
using System; using System.Collections; using MbUnit.Core.Framework; using MbUnit.Framework; [TypeFixture(typeof(IEnumerable))] public EnumerableFixture { [Provider(typeof(IEnumerable))] public ArrayList ProviderEmptyArrayList() { return new ArrayList(); } [Provider(typeof(IEnumerable))] public ArrayList ProviderArrayList() { ArrayList list = new ArrayList(); list.Add(0); list.Add(1); return list; } }
The disadvantage of "hardcoding" methods in the fixture is that we cannot reuse the code for other fixture, for example, for the IList fixture. To avoid this problem you can wrap you "generation" methods in a class factory and use the ProviderFactory to tell MbUnit to use this factory:
IList
ProviderFactory
using System; using System.Collections; using MbUnit.Core.Framework; using MbUnit.Framework; public class ArrayListFactory { public ArrayList Empty { get { return new ArrayList(); } } public ArrayList TwoElems { get { ArrayList list = new ArrayList(); list.Add(0); list.Add(1); return list; } } } [TypeFixture(typeof(IEnumerator))] [ProviderFactory(typeof(ArrayListFactory), typeof(IEnumerable))] public EnumeratorFixture { } That's much better because we can reuse the factory for other fixtures. Of course, you can attach an arbitrary number of factories and provider methods. Step 3: Add some tests Add the unit tests as usuals on the IEnumerable instance. These methods must take the tested type the tested type as argument. Here we check that the enumerator throws if Current is called while the cursor if before the first element or past the last element:... [TypeFixture(typeof(IEnumerable))] [ProviderFactory(typeof(ArrayListFactory), typeof(IEnumerable))] public EnumerableFixture { ... [Test] [ExpectedException( typeof(InvalidOperationException), "Current called while cursor is before the first element" )] public void CurrentCalledBeforeMoveNext(IEnumerable en) { IEnumerable er = en.GetEnumerator(); en.Current; } [Test] [ExpectedException( typeof(InvalidOperationException), "Current called while cursor is past the last element" )] public void CurrentCalledBeforeMoveNext(IEnumerable en) { IEnumerable er = en.GetEnumerator(); while(en.MoveNext()); en.Current; } } Step 4: Compile and run Compile and load in the GUI. MbUnit will scan the providers and create a fixture for each one of them.
using System; using System.Collections; using MbUnit.Core.Framework; using MbUnit.Framework; public class ArrayListFactory { public ArrayList Empty { get { return new ArrayList(); } } public ArrayList TwoElems { get { ArrayList list = new ArrayList(); list.Add(0); list.Add(1); return list; } } } [TypeFixture(typeof(IEnumerator))] [ProviderFactory(typeof(ArrayListFactory), typeof(IEnumerable))] public EnumeratorFixture { }
That's much better because we can reuse the factory for other fixtures. Of course, you can attach an arbitrary number of factories and provider methods.
Add the unit tests as usuals on the IEnumerable instance. These methods must take the tested type the tested type as argument.
Here we check that the enumerator throws if Current is called while the cursor if before the first element or past the last element:
... [TypeFixture(typeof(IEnumerable))] [ProviderFactory(typeof(ArrayListFactory), typeof(IEnumerable))] public EnumerableFixture { ... [Test] [ExpectedException( typeof(InvalidOperationException), "Current called while cursor is before the first element" )] public void CurrentCalledBeforeMoveNext(IEnumerable en) { IEnumerable er = en.GetEnumerator(); en.Current; } [Test] [ExpectedException( typeof(InvalidOperationException), "Current called while cursor is past the last element" )] public void CurrentCalledBeforeMoveNext(IEnumerable en) { IEnumerable er = en.GetEnumerator(); while(en.MoveNext()); en.Current; } }
Compile and load in the GUI. MbUnit will scan the providers and create a fixture for each one of them.
Page rendered at Thursday, December 04, 2008 7:24:50 AM UTC
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.