c# - Using Nunit with TestCaseSource - running multiple times changes results -
i writing unit tests using nunit c# project.
i trying run single test multiple times different data using testcasesource
attribute.
i doing elsewhere without problems, finding first time run tests, code passes. next time, doesn't. using console.writeline
statements, can see test data different each time.
the method used generate data internal test class, not static , generates required dependencies scratch each test.
--
i have fake class holds queue of values return when given function called. new class created each test.
however, if first time test run, exhausts queue, next time run, no data found. surely should regenerated every time?
--
it if nunit not calling method specified testcasesource
attribute every time test run - when project first loaded.
is expected? there workaround?
edit:
ok, here basic example, below:
[testfixture] public class tests { public interface ientry { object read(); } [testcasesource("testdata")] public void test(mock<ientry> entry) { object o = entry.object.read(); object o2 = entry.object.read(); } public system.collections.ienumerable testdata() { var entry = new mock<ientry>(); int call = 0; entry.setup(x => x.read()).returns(() => { console.writeline(call); return null; }).callback(() => { call++; }); yield return new testcasedata(entry); } }
if watch test output in nunit, should display 0, followed 1. in case, each time test run incremented. i.e. second run: 2 , 3, third run: 4 , 5, etc.
if move testdata
code test
, correct values returned every time.
i assume you're using nunit gui runner?
what you're seeing (i believe, can't find confirmation in nunit source easily) optimization of test runner. rather re-create values provided testcasesource
attribute on every test run, when assembly under test changes.
if change code remove moq dependency, it's little clearer:
[testfixture] public class sampletests { [testcasesource("testdata")] public void test(calltracker calltracker) { calltracker.call++; calltracker.call++; } public ienumerable testdata() { yield return new testcasedata(new calltracker()); } public class calltracker { int call; public int call { { return call; } set { call = value; console.writeline(call); } } } }
this yields same behavior code. calltracker
created anew whenever testcasesource
evaluated, since call count keeps going up, test runner must resuing same instance (for i'm assuming performance reasons).
resharper's test runner in visual studio does not exhibit behavior; shows 1
, 2
repeated runs without recompiling. why it's slower start running tests nunit gui runner is. similarly, nunit console not exhibit behavior since starts cold.
Comments
Post a Comment