c# - Is there a way to run a list of different action methods on an object in Nunit using TestCase? -
say have following:
[test] // parameterize parameters in call , call public void test() { var res1 = _sut.method1(1); var res2 = _sut.method2("test"); var res3 = _sit.method3(3); assert.that(res1, is.null); assert.that(res2, is.null); assert.that(res3, is.null); } i'd parameterize tests using testcase/testcasesource attribute including call itself. due repetitive nature of tests, each method needs called different parameters, need able tag different call each of different parameters. possible in nunit? if so, how go it?
using testcasesource, should able loop on array of values , invoke desired methods, example this:
[testfixture] public class testclass { private sut _sut; public testclass() { _sut = new sut(...); } private ienumerable<object> testcases { { var values = new object[,] { { 1, "test", 3 }, { 2, "hello", 0 }, ... }; (var = 0; < values.getlength(0); ++i) { yield return _sut.method1((int)values[i,0]); yield return _sut.method2((string)values[i,1]); yield return _sut.method3((int)values[i,2]); } } } [testcasesource("testcases")] public void test(object val) { assert.that(val, is.null); } } note _sut instance needs instantiated in testclass constructor. not sufficient initialize within [setup] or [testfixturesetup] method.
in case need different _sut instantiations different method invocations, create collection of sut instances in constructor, , access relevant sut item within for loop of testcases getter. alternatively, loop on sut items in getter...
Comments
Post a Comment