c# - NUnit 3.x - TestCaseSource for descendant test classes -
i have set of unit tests consistent number of rest api endpoints. class defined so.
public abstract class getallroutetests<tmodel, tmodule> { [test] public void hasmodels_returnspagedmodel() { // implemented test } }
with implemented test fixture looking like:
[testfixture(category = "/api/route-to-test")] public getallthethings : getallroutetests<thething, modulethethings> { }
this enables me run number of common tests across all/list routes. means have classes linked directly module being tested, , links between tests , code in resharper / visual studio / ci "just work".
the challenge routes require query parameters testing other pathways through route code;
e.g. /api/route-to-test?category=big.
as [testcasesource] requires static field, property, or method there appears no nice way override list of query strings pass. closest thing have come seems hack. namely:
public abstract class getallroutetests<tmodel, tmodule> { [testcasesource("statictodefinelater")] public void hasmodels_returnspagedmodel(dynamic args) { // implemented test } } [testfixture(category = "/api/route-to-test")] public getallthethings : getallroutetests<thething, modulethethings> { static ienumerable<dynamic> statictodefinelater() { // yield return query things } }
this works because static method defined implemented test class, , found nunit. huge hack. problematic else consuming abstract class need "know" implement "statictodefinelater" static something.
i looking better way of achieving this. seems non-static testcasesource sources removed in nunit 3.x, that's out.
thanks in advance.
notes:
- getallroutetests<> implements number of tests, not 1 shown.
- iterating through routes in 1 test "hide" covered, avoid that.
the way solved similar problem having base source class implements ienumerable (another acceptable source nunit), consider if design suits usecase:
// in parent fixture... public abstract class testcases : ienumerable { protected abstract list<list<object>> cases; public ienumerator getenumerator() { return cases.getenumerator(); } } // in tests private class testcasesfortestfoobar : testcases { protected override list<list<object>> cases => /* sets of args */ } [testcasesource(typeof(testcasesfortestfoobar))] public void testfoobar(list<object> args) { // implemented test }
Comments
Post a Comment