Mathias Brandewinder on .NET, F#, VSTO and Excel development, and quantitative analysis / machine learning.
24. July 2009 15:54

I really like the addition of [TestCase] in NUnit 2.5. A significant part of the code I write is math or finance oriented, and I find Data-Driven tests more convenient that “classic” unit tests to validate numeric procedures.

However, I got a bit frustrated today, because of the lack of tolerance mechanism in data-driven tests. Tolerance allows you to specify a margin of error (delta) on your test, and is supported in classic asserts:

[Test]
public void ClassicToleranceAssert()
{
double numerator = 10d;
double denominator = 3d;
Assert.AreEqual(3.33d, numerator / denominator, 0.01);
Assert.That(3.33d, Is.EqualTo(numerator / denominator).Within(0.01));
}

You can specify how close the result should be from the expected test result, here +/- 0.01.

I came into some rounding problems with data driven tests today, and hoped I would be able to resolve them with the same mechanism. Here is roughly the situation:

[TestCase(10d, 2d, Result = 5d)]
[TestCase(10d, 3d, Result = 3.33d)]
public double Divide(double numerator, double denominator)
{
return numerator / denominator;
}

Not surprisingly, the second test case fails – and when I looked for a similar tolerance mechanism, I found zilch.

The best solution I got was to do something like this:

[TestCase(10d, 2d, Result = 5d)]
[TestCase(10d, 3d, Result = 3.33d)]
public double Divide(double numerator, double denominator)
{
return Math.Round(numerator / denominator, 2);
}

Of course, this works – but this is clumsy. I was really hoping that TestCase would support the same functionality as Assert, with a built-in delta tolerance. It seems particularly relevant: rounding error issues are typical in numerical procedures, a field where data-driven tests are especially adapted.

Maybe the feature exists, but is undocumented. If you know how to do this, sharing your wisdom will earn you a large serving of gratitude, and if it the feature doesn’t exist yet… maybe in NUnit 2.5.1?

Comments

9/28/2011 8:30:15 AM #

I am sure you worked this out by now but you can do:

[TestCase(10d, 2d, 5d)]
[TestCase(10d, 3d, 3.33d)]
public double Divide(double numerator, double denominator, double result)
{
Assert.That(numerator / denominator, Is.EqualTo(result).Within(0.01));
}

9/28/2011 8:32:00 AM #

oops, copy pasta mistake,  return type should be void.

11/3/2013 8:33:37 PM #

Pingback from extractgreenbean.com

Green Coffee Bean Burn Fat

Add comment

• Comment
• Preview

#### Need help with F#?

The premier team for
F# training & consulting.

Comment RSS