MbUnit v3 alpha 3
Jeff announced the release of Gallio alpha 3 yesterday, my time constriants at the moment meant I missed blogging Alpha 2 back in March but that release was a massive release in terms of features and the work that Jeff, Julian, Graham, Mark, Ben and the rest of the team have put in.
I have talked about Gallio before but as a re-cap, Gallio is described best on the website "The Gallio Automation Platform is an open, extensible, and neutral system for .NET that provides a common object model, runtime services and tools (such as test runners) that may be leveraged by any number of test frameworks", What does that mean, well unit test frameworks such as xUnit.net, NUnit, Pex and MSTest can plugin into the framework (and Gallio supports them all) or frameworks like MbUnit v3 use it directly. It's runners then use that framework and Gallio has the largest set of runners of any unit test framework to date.
-
GUI (Icarus)
-
Console (Echo)
-
TD.NET
-
R#
-
VSTS
-
CCNet
-
Powershell
-
MSBuild
-
NAnt
The runners them selfs are very rich, those who remember the ZaneBug framework will remember how rich it's GUI was and that is something we have aimed for in Icarus with great use of layouts and display information. So now you can take your framework of choice and reuse them across the Gallio platform.
Related directly to MbUnit is the change to the parameterized testing, this has had some heavy work done in make things a little simpler and far more flexible. With this kind of testing now added to Xunit.net, NUnit and csUnit there are lots of different ways you can go about it, the following is a reprint from Jeff's announcement.
A test can now be parameterized in any of the following ways:
- Adding a method parameter to a test method.
- Adding a generic method parameter to a test method.
- Adding a constructor parameter to a test fixture.
- Adding a field to a test fixture.
- Adding a writable property to a test fixture.
- Adding a generic type parameter to a test fixture.
Correspondingly data-source attributes can be specified on any of the following code elements:
- Test methods.
- Test method parameters.
- Test method generic parameters.
- Test fixture types.
- Test fixture constructors.
- Test fixture constructor parameters.
- Test fixture fields.
- Test fixture properties.
- Test fixture generic parameters.
So there are many choices, but each choice is quite simple.
Here are a few interesting combinations.
A basic "Row"-test.
CHANGE !! - : The [RowTest] attribute is not needed anymore and has been eliminated.
[TestFixture]
public class Fixture
{
[Test]
[Row(3, 4, 5, true)]
[Row(1, 2, 3, false)]
public void Test(int a, int b, int c, bool expectedResult)
{
Assert.AreEqual(expectedResult, Math.IsPythagoreanTriple(a, b, c));
}
}
CHANGE !! - A generic "Type"-test.
Note: The [TypeFixture] attribute is not needed anymore and has been eliminated.
[TestFixture]
[Row(typeof(List<int>))]
[Row(typeof(LinkedList<int>))]
public class Fixture<T> where T : IList<int>, new()
{
[Test]
public void Test()
{
T list = new T();
list.Add(123);
Assert.AreEqual(1, list.Count);
}
}
CHANGE !! - A combinatorial test.
Note: The [CombinatorialTest] attribute is not needed anymore and has been eliminated.
[Test]
public void Test([Column(0, 1)] int a, [Column("a", "b")] string b, [Column(false, true)] bool c)
{
// Test something with the specified combination.
// Will be executed 8 times total.
}
CHANGE!! - A pairwise test.
Only tests all combinations of pairs of values. Can yield a significant reduction in test cases compared to ordinary combinatorial tests while still obtaining great coverage. See also: www.pairwise.org.
[Test, PairwiseJoin]
public void Test([Column(0, 1)] int a, [Column("a", "b")] string b, [Column(false, true)] bool c)
{
// Test something with the specified combination.
// Will be executed only 4 times instead of 8.
}
NEW ! - A CSV data source.
Just one of many external data sources to be implemented.
[Test]
[CsvData(ResourcePath="CsvDataTest.csv", HasHeader=true)]
public void ImplicitlyScopedResourceWithHeader(decimal price, string item)
{
Log.WriteLine("{0}: {1}", item, price);
}
The data itself should be stored in an embedded resource called "CsvDataTest.csv". We just create a file with that name in the same folder as the test class and then set its Build Action property to Embedded Resource in Visual Studio.
Item, Price
Apples, 1.00
Bananas, 1.50
Cookies, 2.00
Note the changes to TypeFixture and CombinatorialTest, also note Factory is not included, if you use this with combinatorial tests I'd like to hear from you. The Pairwise testing is something new to v3 and I'll cover in more detail in a future post. If you have further questions, things you want to see them also let me know.
Please also note that Jeff sat down with Scott Hansleman, Brad Wilson (XUnit.net), Charlie Poole (NUnit) and Roy Osherove to talk about the past and future of unit test frameworks in .NET at the recent ALT.NET event, Jeff does talk about Gallio and it is well worth a listen. I do hope that in the future the discussion thrown open wider to other folks that are central to the topic, such as Jim Newkirk, Peli de Halleux, Manfred Lange and others.