简体   繁体   English

运行NUnit测试时的错误配置文件(TD.NET)

[英]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. 我有一个组件从标准.NET配置(app.config)文件中读取一些配置。

when I run unit tests (NUnit) for this component (using TD.NET), i noticed that the configuration file is not read. 当我为这个组件运行单元测试(NUnit)时(使用TD.NET),我注意到没有读取配置文件。

Upon inspection of AppDomain.CurrentDomain.SetupInformation.ConfigurationFile 检查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). 我注意到它的'值设置为C:\\ Users \\ ltal \\ AppData \\ Local \\ Temp \\ tmp6D2F.tmp(一些临时随机位置)。

Is there a reason for why this is happening? 为什么会发生这种情况有原因吗? (Is it NUnit or TD.NET's fault?) (是NUnit还是TD.NET的错?)

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. 我想我可以自己设置这个SetupInformation对象,为了测试,还没有尝试过,但仍然想知道为什么它是这样创建而不是默认。

  • To workaround this, you can create an app.config in your unit test project. 要解决此问题,您可以在单元测试项目中创建app.config。 This will then be called in place of the main app.config by your unit tests. 然后通过单元测试调用它来代替主app.config。 You can then change values in that app.config in your unit tests making it easier to test different values and configurations ie you can setup your test app.config with certain values before running your test. 然后,您可以在单元测试中更改该app.config中的值,以便更轻松地测试不同的值和配置,即您可以在运行测试之前使用特定值设置测试app.config。

    ConfigurationManager.AppSettings[""] = "";

  • Another option might be to place settings in the Settings.setting file of your main project. 另一个选项可能是将设置放在主项目的Settings.setting文件中。 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 有关设置和app.config之间差异的一些链接 - MSDN论坛StackOverflow用户设置 - 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. 当然,第三种选择是通过引入一个接口并将依赖项注入组件,从而轻松地模拟它并进行单元测试,从组件中删除对app.config的依赖。

By default the .NET runtime looks in the working directory of the AppDomain , which is being managed by NUnit in the temp location. 默认情况下,.NET运行时在AppDomain的工作目录中查找,该目录由NUnit在临时位置进行管理。

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 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. 基本上,他们需要住在测试目录中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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