简体   繁体   中英

Run unit test cases with different app keys

I have a unit test case project which has 100 UTCs. The unit test project has a app.config file. There is a flag XYZFeatureOn in app.config file and out of 100, 50 test cases run green when flag is true an remaining 50 run green when flag is false. In every test case the feature is turned on/off by replacing key in the config file as:

var xmlDoc = new XmlDocument();
xmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

xmlDoc.SelectSingleNode("//XYZFeatureON").Attributes["Enabled"].Value = true/false;
xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

ConfigurationManager.RefreshSection("XYZFeatureON");

Now when I run a test suite in TFS build half of them fail because flag is initialized to either true or false, but when tests are run individually the test case runs green.

By running above code it doesn't actually change the setting at the run time.

What can I do to in this case? Couldn't find if I can have two different app.config file for each case of test case.

Any help appreciated.

What you have now is a dependency on an external thing which goes against unit testing methodology.

You have two options :

  1. abstract it away and mock it in your tests
  2. pass it as a parameter to whatever method needs it. Your test then only has to call whatever it needs to call, by passing the different options in the methods signature.

This way, your tests will be reliable and will run quicker since you don't need to update the configuration file.

Finally, have a couple integration tests to make sure the config values are passed correctly and call it a day.

You mentioned that the configuration value is read somewhere deep in the code.

In that case, one option could be to read it and immediately store it in the application cache.

From that point onward you can read it from the cache when you need to.

You could even create an abstraction, an IApplicationSettings interface, which deals with reading and setting something in the application cache, pass that to whichever part of the code needs it, so you can call the Get method and use the value, If you do it like this then in your tests you can mock this value and return whatever you want for your test.

The point is that you will need to make some changes if you want to have simple tests which makes sense and are maintainable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM