如果程序集包含app.config文件,只要它与通过NUnit-Gui执行的NUnit项目位于同一目录中,ConfigurationManager就会加载该文件。为了进行说明,请考虑以下文件夹结构。
+ TestFolder
testProject.nunit
+ AssemblyAFolder
assemblyA.dll
assemblyA.dll.config
+ AssemblyBFolder
assemblyB.dll
assemblyB.dll.configAssemblyA和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>发布于 2009-09-13 01:55:11
NUnit博客解释了配置文件加载方式的原因。基本上,他们所说的是NUnit让框架处理配置文件,而不做任何管理。
还可以使用在案例中加载的testProject.config文件来引用每个程序集的配置文件。使用appSettings文件属性添加密钥。
最后一种替代方法是使用configSource element attribute来使用其中一个程序集配置文件中的节。
希望这能有所帮助。
发布于 2012-03-14 14:50:19
N单元无法在我们的项目中找到App.config文件的路径。因此,我们需要手动告诉Nunit App.config文件在项目中的位置(显然在根文件夹上)。
在我的例子中,项目结构如下
+ProjectWEBApp//web pages
+Modules
+aspx pages
+web.Config
+projectBusinesslogic //business logic .cs files
+Modules
+.cs
+ProjectTestName// a seperate Nunit test cases project
+Modules
+App.ConfigProjectWebApp使用包含业务逻辑的projectBusinesslogic引用。+ProjectTestName使用projectBusinesslogic引用对业务逻辑进行测试。问题从这里开始,Nunit测试项目需要自己的app.config文件。它不会像在projectBusinesslogic中那样使用web.config文件,所以当您运行Nunit时,它会提示一个错误
-Null Reference exception.......object instant未设置为...........
解决方案-当您运行Nunit GUI时
这就是你的问题的简单解决方案
发布于 2009-09-19 18:53:30
configSource元素solution given by MarkLawrence就是我想要的,并且工作得很好。但是,实现此解决方案的挑战在于,在从显式NUnit项目(如my case)运行测试时,以及在隔离(没有显式项目)的情况下运行程序集的单元测试时,加载程序集配置。为了实现这一点,需要对我的文件布局进行以下修改。
+ TestFolder
testProject.nunit
testProject.config
+ AssemblyAFolder
assemblyA.dll
assemblyA.dll.config
assemblyA.dll.configfragment
+ AssemblyBFolder
assemblyB.dll
assemblyB.dll.config
assemblyB.dll.configfragment创建configfragment文件以包含曾经位于相应config文件中的程序集配置。然后,将config文件修改为只包含一个configSource元素,该元素具有到相应configfragment文件的相对路径。请注意,只有当assemblyA.dll和assemblyB.dll都需要相同的配置节时,这种方法才不起作用,因为在创建testproject.config时会出现冲突。
在尝试了这种方法之后,我决定依赖于在运行时生成程序集配置,并一起删除静态配置文件。下面是一些代码,演示了如何生成配置,并使其可访问,而不管测试中的程序集是如何加载的。
void WithConfigurationFile(Action method)
{
// Create the assembly configuration.
string settingsSection = "myConfigSectionName";
Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.Sections.Add(
settingsSection,
new ConfigSectionType(/*config element values*/);
config.Save();
try
{
// Invoke the method with the new configuration.
ConfigurationManager.RefreshSection(settingsSection);
method();
}
finally
{
// Revert the assembly configuration.
File.Delete(config.FilePath);
ConfigurationManager.RefreshSection(settingsSection);
}
}通过使用不接受路径的ConfigurationManager.OpenExeConfiguration()重载,我们从主机应用程序的工作目录加载配置,该目录根据您运行NUnit测试的方式而变化。此外,try/finally块保证您的程序集配置不会干扰其他测试,这些测试可能需要也可能不需要这样的配置。
最后,在单元测试中使用:
[Test]
void VerifyFunctionality
{
WithConfigurationFile(delegate
{
// implement unit test and assertions here
});
}我希望这对其他可能遇到类似问题的人有所帮助!
https://stackoverflow.com/questions/1412235
复制相似问题