Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrong configuration file when running NUnit tests (TD.NET)

I have a component that reads some configuration from the standard .NET configuration (app.config) file.

when I run unit tests (NUnit) for this component (using TD.NET), i noticed that the configuration file is not read.

Upon inspection of AppDomain.CurrentDomain.SetupInformation.ConfigurationFile

I have noticed that its' value is set to C:\Users\ltal\AppData\Local\Temp\tmp6D2F.tmp (some temp random locaiton).

Is there a reason for why this is happening? (Is it NUnit or TD.NET's fault?)

I suppose i could set this SetupInformation object myself for the sake of the test, haven't tried yet, but still wondering why is it being created like that and not as default.

like image 875
lysergic-acid Avatar asked Oct 12 '22 00:10

lysergic-acid


1 Answers

  • To workaround this, you can create an app.config in your unit test project. This will then be called in place of the main app.config by your unit tests. You can then change values in that app.config in your unit tests making it easier to test different values and configurations i.e. you can setup your test app.config with certain values before running your test.

    ConfigurationManager.AppSettings[""] = "";

  • Another option might be to place settings in the Settings.setting file of your main project. You do not have to change anything in your unit test project then. Some links about the difference between settings and app.config - MSDN forums, StackOverflow, User Settings - MSDN

  • And of course a third option would be to remove the dependency on the app.config from your component by introducing an interface and inject the dependency into the component making it easy to mock it out and unit test.

like image 113
Peter Kelly Avatar answered Nov 01 '22 14:11

Peter Kelly