[英]ConfigurationManager.GetSection(sectionName) returns null while performing unit tests
I have a unit tests project with it's own app.config
file, which is a mock of a real configuration file defined by target project being tested. 我有一个单元测试项目,它有自己的app.config
文件,它是由正在测试的目标项目定义的真实配置文件的模拟。 This mock file is loaded and processed by unit test code (not by target project), and it works properly if I run only tests within only this one test project. 这个模拟文件由单元测试代码(而不是目标项目)加载和处理,如果我只在这个测试项目中运行测试,它就能正常工作。
ConfigurationManager.GetSection(sectionName)
However, if I run tests from several test projects, and other test projects are performed prior to relevant project, the above statement returns null
. 但是,如果我从多个测试项目运行测试,并且在相关项目之前执行其他测试项目,则上述语句将返回null
。 If discussed test project is performed as first, there is no problem with loading configuration file. 如果首先执行讨论的测试项目,则加载配置文件没有问题。
How can I fix loading of configuration file in unit test to work correctly? 如何在单元测试中修复配置文件的加载才能正常工作?
Your problem is not ConfigurationManager.GetSection(sectionName) returns null, it is how can I test some code containing ConfigurationManager.GetSection(sectionName)? 你的问题不是ConfigurationManager.GetSection(sectionName)返回null,它是如何测试包含ConfigurationManager.GetSection(sectionName)的一些代码的?
And the answer is: wrap it, inject it, then for your test mock it. 答案是:包装它,注入它,然后为你的测试模拟它。
You have several examples of pepole facing the same issue: 您有几个面临相同问题的人员示例:
(The second one is much more detailed, still the idea is the same). (第二个更详细,但想法仍然相同)。
Anyway, this is quite logical that you cannot use information from app.config in a unit test, as an app.config is contextual for the whole application, when it is required to write test absolutely independant. 无论如何,这是合乎逻辑的,你不能在单元测试中使用来自app.config的信息,因为app.config是整个应用程序的上下文,当需要编写绝对独立的测试时。 If you use directly an app.config value, then you have non logical coupling. 如果直接使用app.config值,则表示您具有非逻辑耦合。
Facing the same issue this solved it: app.config
should be picked up inside a unit test if it's Copy to Output Directory
property set to Copy if newer
or if you add the DeploymentItem attribute [DeploymentItem("your.config")]
. 面对同样的问题,这解决了它:如果将Copy to Output Directory
属性设置为Copy if newer
[DeploymentItem("your.config")]
Copy if newer
或者添加DeploymentItem属性[DeploymentItem("your.config")]
则应该在单元测试中拾取app.config
。
More detailed description: http://social.msdn.microsoft.com/Forums/en-US/3e520735-8ced-4092-b681-38b69e0db534/unit-test-configuration#32998bf4-5a76-4083-99da-42f0c3a91559 更详细的描述: http : //social.msdn.microsoft.com/Forums/en-US/3e520735-8ced-4092-b681-38b69e0db534/unit-test-configuration#32998bf4-5a76-4083-99da-42f0c3a91559
similar question: MSTest and app.config issue 类似的问题: MSTest和app.config问题
I think problem is either it could not find the file in test working directory or file itself failed to load. 我认为问题是它无法在测试工作目录中找到该文件或文件本身无法加载。
I have solved this problem by explicitly loading configuration file with name. 我通过显式加载配置文件名称解决了这个问题。 In your case you can try same. 在你的情况下,你可以尝试相同的。
ExeConfigurationFileMap configMap = new ExeConfigurationFileMap();
configMap.ExeConfigFilename = @"d:\test\test.config";
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
我使用了测试项目post build命令行,但是如果有更改,请记得构建项目:
copy /Y "$(SolutionDir)$(SolutionName)\App.Debug.config" "$(TargetDir)$(ProjectName)$(TargetExt).config"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.