如何指示 NUnit 从特定目录加载程序集的 dll.config 文件?
如果程序集包含 app.config 文件,只要它与通过 NUnit-Gui 执行的 NUnit 项目位于同一目录中,ConfigurationManager 就会加载它。为了说明这一点,请考虑以下文件夹结构。
+ TestFolder
testProject.nunit
+ AssemblyAFolder
assemblyA.dll
assemblyA.dll.config
+ AssemblyBFolder
assemblyB.dll
assemblyB.dll.config
AssemblyA
和 AssemblyB
都执行调用 ConfigurationManager
的代码。如果我在 NUnit-Gui 中独立运行这些测试程序集,ConfigurationManager 将正确解析本地配置文件。
但是,如果我将 testProject.nunit
加载到 NUnit-Gui(其中包含对 AssemblyA
和 AssemblyB
的引用),ConfigurationManager 在
TestFolder
中查找配置文件,无论当前正在执行哪个程序集。
有没有一种方法可以指示 NUnit 将应用程序配置重新加载到当前程序集目录中的配置?
以下是 testProject.nunit
的内容:
<NUnitProject>
<Settings activeconfig="Debug" />
<Config name="Debug" binpathtype="Auto">
<assembly path="AssemblyAFolder\assemblyA.dll" />
<assembly path="AssemblyBFolder\assemblyB.dll" />
</Config>
</NUnitProject>
If an assembly contains an app.config file, ConfigurationManager
will load it as long as it is in the same directory as the NUnit project that is executing through NUnit-Gui. To illustrate consider the following folder structure.
+ TestFolder
testProject.nunit
+ AssemblyAFolder
assemblyA.dll
assemblyA.dll.config
+ AssemblyBFolder
assemblyB.dll
assemblyB.dll.config
Both AssemblyA
and AssemblyB
exercise code that calls into ConfigurationManager
. If I run these test assemblies independently in NUnit-Gui, ConfigurationManager
will correctly resolve the local configuration files.
However, if I load testProject.nunit
into NUnit-Gui (which contains references to both AssemblyA
and AssemblyB
), ConfigurationManager
looks for the configuration file in TestFolder
regardless of which assembly is currently executing.
Is there a way to direct NUnit to reload the application configuration to the one present in the current assembly's directory?
Here are the contents of testProject.nunit
:
<NUnitProject>
<Settings activeconfig="Debug" />
<Config name="Debug" binpathtype="Auto">
<assembly path="AssemblyAFolder\assemblyA.dll" />
<assembly path="AssemblyBFolder\assemblyB.dll" />
</Config>
</NUnitProject>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Nunit 无法在我们的项目中找到 App.config 文件的路径。因此,我们需要手动告诉 Nunit App.config 文件放置在我们项目中的位置(显然是在根文件夹中)。
就我而言,项目结构如下,
ProjectWebApp 使用包含业务逻辑的projectBusinesslogic 的引用。
+ProjectTestName 使用projectBusinesslogic 的引用来对业务逻辑进行测试。
问题从这里开始,Nunit 测试项目需要自己的 app.config 文件。它不会像projectBusinesslogic那样使用web.config文件,所以当你运行Nunit时它会提示错误
解决方案-
当您运行 Nunit GUI
,这就是您问题的简单解决方案
Nunit is unable to locate the path of App.config File in our project. So we need to manually tell the Nunit where the App.config File is placed in our project (obviously on the root folder).
In my case , the project structure is as following
the ProjectWebApp uses the references of projectBusinesslogic which contains the business logic.
the +ProjectTestName uses the reference of projectBusinesslogic to perform test on the business logic.
The problems start here, Nunit test project needs its own app.config file. it won't use web.config file as in case of projectBusinesslogic,so when you run Nunit it will prompt an error
Solution-
When you run the Nunit GUI
and that is the simple solution for your problem
configSource
元素 MarkLawrence 给出的解决方案正是我一直在寻找的,并且效果很好。然而,实现此解决方案的挑战是在从显式 NUnit 项目运行测试时加载程序集配置(如 我的案例),并且在单独运行程序集的单元测试时(没有明确的项目)。为了实现这一点,需要对我的文件布局进行以下修改。创建 configfragment 文件以包含曾经位于相应 config 文件中的程序集配置。然后,
config
文件被修改为仅包含configSource
元素以及对应configfragment
文件的相对路径。请注意,此方法唯一不起作用的情况是当assemblyA.dll 和
assemblyB.dll 都需要相同的配置节时,因为在创建 <代码>testproject.config
。在尝试了这种方法之后,我决定依靠在运行时生成程序集配置,并将静态配置文件全部删除。下面是一些代码,演示如何生成配置,并使其可访问,无论如何加载被测程序集。
通过使用不接受路径中,我们从主机应用程序的工作目录加载配置,该目录根据您运行 NUnit 测试的方式而变化。此外,try/finally 块保证您的程序集配置不会干扰其他测试,这些测试可能需要也可能不需要此类配置。
最后,在单元测试中使用:
我希望这可以帮助其他可能遇到类似问题的人!
The
configSource
element solution given by MarkLawrence is what I was looking for, and works nicely. The challenge in implementing this solution however, is to make the assembly configuration load when running tests from both an explicit NUnit project (as in my case), AND when running the unit tests for an assembly in isolation (no explicit project). To accomplish this, the following modifications to my file layout were required.The
configfragment
files are created to contain the assembly configuration that was once in the correspondingconfig
files. Afterwards, theconfig
files are modified to contain only aconfigSource
element with a relative path to the correspondingconfigfragment
file. Note that the only time that this approach doesn't work is whenassemblyA.dll
andassemblyB.dll
both require the same configuration section, as a conflict will arise when creatingtestproject.config
.After experimenting with this approach I decided to rely on generating the assembly configuration at runtime, and remove the static configuration files all together. Here is some code that demonstrates how to generate the configuration, and make it accessible regardless of how the assembly-under-test is loaded.
By using the ConfigurationManager.OpenExeConfiguration() overload that does not accept a path, we load the configuration from the host application's working directory, which changes depending on how you run your NUnit tests. Also, the try/finally block guarantees that your assembly configuration will not interfere with other tests, which may or may not require such configuration.
Finally, to use within a unit test:
I hope this helps others who may have encountered similar issues!
NUnit 博客解释了为什么配置文件会这样加载。基本上他们所说的是 NUnit 让框架处理配置文件,但不进行任何管理。
您还可以使用将在您的案例中加载的
testProject.config
文件来引用每个程序集的配置文件。使用 appSettings 文件属性添加密钥。最后一种替代方法是使用
configSource
元素属性,用于使用程序集配置文件之一中的部分。希望这有帮助。
The NUnit blog explained of why the config files load the way they do. Basically what they said was that NUnit lets the framework handle the config files and doesn't do any of the management.
You can also use the
testProject.config
file that would be loaded in your case, to reference the config files for each of the assemblies. Using the appSettings file attribute to add keys.One final alternative is to use the
configSource
element attribute to use the section in one of the assemblies config files.Hope this helps.
在 .nunit 文件的配置级别中使用 configfile 属性:
Use the configfile attribute in the Config level in your .nunit file:
实际上,如果您使用 NUnit 和 Visual Studio 中的运行程序,您可以将您的值粘贴到测试项目的 App.config 中。然后将此行添加到您的构建后事件中:
copy /Y “$(ProjectDir)App.config” “$(TargetDir)$(TargetFileName).config”
当您在测试中访问 ConfigurationManager 时,NUnit 将传递以下值在初始化方法中将您的 app.config 更改为 .Net。
Actually, if you are using NUnit and a runner within Visual Studio, you can stick your value in an App.config in your test project. Then add this line to your Post-build event:
copy /Y “$(ProjectDir)App.config” “$(TargetDir)$(TargetFileName).config”
When you access the ConfigurationManager in your tests, NUnit will pass the values in your app.config to .Net in the initialize method.