c# - In NUnit, how can I indicate that a 'DataPoint' is applicable to just one Theory? -
in nunit
, there way indicate datapoint(s)attribute
should applied 1 theory only, if there more 1 theory in same testfixture
class?
reason ask have followed unit test convention methods of test class (cut) tested multiple [test]
methods rolled single test fixture class, , trying move away parameterized tests toward [theory]
.
or should continue use values / range / random attributes of parameterized tests such tests?
e.g. below, want ensure different datapoints theories add , divide:
// c.u.t. public class badmaths { public int badadd(int x, int y) { return x + y - 1; } public int divide(int x, int y) { return x / y; } } [testfixture] public class badmathstest { // ideally want 2 x different datapoints - 1 add, , different 1 divide [datapoints] private tuple<int, int>[] _points = new tuple<int, int>[] { new tuple<int, int>(20, 10), new tuple<int, int>(-10, 0), }; [theory] public void addtheory(tuple<int, int> point) { assume.that((long)point.item1 + (long)point.item2 < (long)int.maxvalue); assert.that(point.item1 + point.item2, is.equalto(new badmaths().badadd(point.item1, point.item2))); } [theory] public void dividetheory(tuple<int, int> point) { assume.that(point.item2 != 0); // seems best can - test inconclusive assert.that(point.item1 / point.item2, is.equalto(new badmaths().divide(point.item1, point.item2))); } }
edit
the example given above isn't example of theory
usage - better fit testcasesource
, , new roslyn nameof
operator neither [datapoints]
nor [usedimplicitly]
attributes required on source data.
[testcasesource(nameof(_points)] public void ensureaddpoints(tuple<int, int> point) { ....
i don't believe there's direct way of asking nunit use different datapoints of same type different theories. there 2 possible ways can work around:
the first use different textfixture classes tests require different datapoint values:
[testfixture] public class badmathsadditiontest { // ideally want 2 x different datapoints - 1 add, , different 1 divide [datapoints] private tuple<int, int>[] _points = new tuple<int, int>[] { new tuple<int, int>(20, 10), new tuple<int, int>(-10, 0), }; // add tests use these datapoints [theory] public void addtheory(tuple<int, int> point) { assume.that((long)point.item1 + (long)point.item2 < (long)int.maxvalue); assert.that(point.item1 + point.item2, is.equalto(new badmaths().badadd(point.item1, point.item2))); } } [testfixture] public class badmathsdivisiontest { // ideally want 2 x different datapoints - 1 add, , different 1 divide [datapoints] private tuple<int, int>[] _points = new tuple<int, int>[] { new tuple<int, int>(20, 10), }; // add test use these datapoints }
the second method requires bit more work arguably gives more readable code wrap each datapoint set in different struct, this:
// c.u.t. public class badmaths { public int badadd(int x, int y) { return x + y - 1; } public int divide(int x, int y) { return x / y; } } [testfixture] public class badmathstest { public struct additiondata { public int first { get; set; } public int second { get; set; } } [datapoints] private additiondata[] _points = new additiondata[] { new additiondata{first=20, second=10}, new additiondata{first=-10, second=0} }; public struct divisiondata { public int first { get; set; } public int second { get; set; } } [datapoints] private divisiondata[] _points2 = new divisiondata[] { new divisiondata{first=20, second=10}, }; [theory] public void addtheory(additiondata point) { assume.that((long)point.first + (long)point.second < (long)int.maxvalue); assert.that(point.first + point.second, is.equalto(new badmaths().badadd(point.first, point.second))); } [theory] public void dividetheory(divisiondata point) { assume.that(point.second != 0); // want keep condition anyway. second==0 separate test assert.that(point.first / point.second, is.equalto(new badmaths().divide(point.first, point.second))); } }
Comments
Post a Comment