开发者

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

开发者 https://www.devze.com 2023-03-07 00:20 出处:网络
I have a component that reads some configuration from the standard .NET configuration (app.config) file.

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 开发者_如何学Cthis 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.


  • 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.


By default the .NET runtime looks in the working directory of the AppDomain, which is being managed by NUnit in the temp location.

This link offers two solutions about how to get config files picked up:

http://blogs.msdn.com/b/josealmeida/archive/2004/05/31/loading-config-files-in-nunit.aspx

Basically, they need to live in the testing directory.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号