c# - Testing for exceptions with [TestCase] attribute in NUnit 3? -
how test exceptions in testcase nunit3?
let's have method divide(a,b)
defined follows:
public double divide(double a, double b) { if(math.abs(b) < double.epsilon) throw new argumentexception("divider cannot 0"); return a/b; }
i want test method using nunit 3.0 test cases, maybe have:
[testcase(-10, 2, -5)] [testcase(-1, 2, -0.5)] public void testdivide(double a, double b, double result) { assert.that(_uut.divide(a, b), is.equalto(result)); }
is there way specify test case cause divide() throw argumentexception , somehow have expected result, e.g. along lines of:
[testcase(-10, 2, -5)] [testcase(-1, 2, -0.5)] [testcase(-1, 0, expectedresult = typeof(argumentexception)] public void testdivide(double a, double b, double result) { assert.that(_uut.divide(a, b), is.equalto(result)); }
(of course define separate test method , use assert.throws()
in this, purely out of curiosity)
expectedexception
have been correct method nunit 2.x, removed nunit 3.
there's various snippets of discussion in nunit google group , equivalent dev group - looks decision made it's better design pattern test expected outcomes, , exceptions in separate methods. (link)
the way in nunit 3, break down in 2 separate tests. (confirmed in similar question answered nunit core team, here.)
[testcase(-10, 2, -5)] [testcase(-1, 2, -0.5)] public void testdivide(double a, double b, double result) { assert.that(_uut.divide(a, b), is.equalto(result)); } [testcase(-1, 0)] public void testdividethrows(double a, double b) { assert.that(() => _uut.divide(a, b), throws.typeof<argumentexception>()); }
Comments
Post a Comment