Created
January 7, 2015 15:13
-
-
Save nonnb/d50e5efabbd4ceefad70 to your computer and use it in GitHub Desktop.
NUnit Data Points and Theories - http://stackoverflow.com/questions/27818357/best-practice-for-checking-double-float-parameter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using NUnit.Framework; | |
namespace ClassLibrary1 | |
{ | |
public static class SillyClass | |
{ | |
public static bool IsNaNOrInfinity(this double foo) | |
{ | |
return (double.IsNaN(foo)) || (double.IsInfinity(foo)); | |
} | |
} | |
class CUT | |
{ | |
public void Foo(double bar, double bar2, double bar3, double bar4) | |
{ | |
if (bar.IsNaNOrInfinity()) throw new ArgumentOutOfRangeException(); | |
if (bar2.IsNaNOrInfinity()) throw new ArgumentOutOfRangeException(); | |
if (bar3.IsNaNOrInfinity()) throw new ArgumentOutOfRangeException(); | |
if (bar4.IsNaNOrInfinity()) throw new ArgumentOutOfRangeException(); | |
} | |
} | |
[TestFixture] | |
public class FooTests | |
{ | |
[Datapoints] | |
private double[] _permutations = new[] | |
{ | |
double.PositiveInfinity, | |
double.NegativeInfinity, | |
double.NaN | |
}; | |
[Theory] | |
public void EnsureInvalidDoublesAreThrown(double bar, double bar2, double bar3, double bar4) | |
{ | |
Assert.Throws<ArgumentOutOfRangeException>(() => new CUT().Foo(bar, bar2, bar3, bar4)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here we use NUnit's Theories along with an array of DataPoints, which when run with a decent TestRunner (I used Resharper) will generate all permutations of the parameters and pass them into test Theories which are matched by parameter type (double). The above generates 3 ^ 4 = 81 invocations of the Unit test
EnsureInvalidDoublesAreThrown