xml - Nunit loads all test Test data for all the test before the test gets executed -


i using nunit. loads test test data test before test gets executed.i want why happening . want if there other effective way load data.i have posted code use below , data source xml

    private ienumerable example {     { return getexample; } } private ienumerable getexample() {     var doc = xdocument.load("example.xml");     return         examples in doc.descendants("example")         let example1 = examples.attribute("example1").value         let example2 = examples.attribute("example2").value         let example3 = examples.attribute("example3").value          select new object[] { example1, example2, example3}; } [testcasesource("example")] public void shouldlogin(string username, string password, bool expected) {     // test uses data above  } <?xml version="1.0" encoding="utf-8" ?> <examples>   <example example1="fsse" example2="dj7sihfs" example3="true" />   <example example1="hgtd" example2="sd122?=s" example3="true" />   <example example1="asde" example2="!!sf3mff" example3="true" />   <example example1="bsfd" example2="--sdfj+?" example3="true" />   <example example1="aefb" example2="!#¤%/(sd" example3="true" />  </examples> 

it loads same data twice when when 2 different test calls same test data thank u in advance

i think that's design. nunit designed clean environment before test, generate same conditions test independent execution order.

if test change xdocument, test b run changes. might led indeterminate test results.

if test b expects changes made test a, tests not isolated, that's bad practice.

if wan't change behaviour, implement lazy field , load test data on first access. can increase performance if know not changing data in of tests, pay attention.

private static lazy<ienumerable> testdata = new lazy<ienumerable>(getexample);   private static ienumerable getexample() {     var doc = xdocument.load("example.xml");     return         examples in doc.descendants("example")         let example1 = examples.attribute("example1").value         let example2 = examples.attribute("example2").value         let example3 = examples.attribute("example3").value          select new object[] { username, password, expected }; } 

you can use testfixturesetupattribute

private ienumerable testdata;   [testfixturesetup] public void loadtestdata() {     testdata = getexample(); } 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -