测试项目中的 App.config [英] App.config in Test Projects
问题描述
我正在 VS2010 中构建一个 ASP.NET 应用程序.我有许多单独的程序集(类库)和相应的测试项目.
I'm building an ASP.NET app in VS2010. I have a number of separate assemblies (class libraries) and corresponding Test projects for each.
在其中一个类库中,我使用 App.config 文件来存储设置.程序集本身使用以下代码来检索设置:
In one of the class libraries I use an App.config file to store settings. The assembly itself uses the following code to retrieve settings:
string tmp = ConfigurationManager.AppSettings["mySetting"];
问题是,当我尝试在单独的测试项目中创建单元测试时,该测试没有选择 App.config 文件中的设置.如果我将 App.config 文件复制到测试项目中,它就可以工作.
The problem is that when I try to create a Unit Test in a separate test project, the test does not pick up the setting in the App.config file. If I COPY the App.config file into the Test project, it works.
如何确保每个程序集都使用自己的 App.config 文件副本.如果我不得不复制配置文件,它会带来问题.还有一个问题,因为可能有多个配置文件,每个程序集一个 - 它们怎么能在一个测试项目中共存?
How can I ensure that each assembly uses its own copy of an App.config file. It would introduce problems if I had to copy config files around. There's an additional problem because there may be multiple config files, one per assembly - how could they all co-exist in a single test project anyway?
谢谢!
推荐答案
好吧,如果您需要在多个项目之间共享一个 App.config 文件,我会简单地添加为链接"原始<每个项目中的 em>App.config 文件.
Well, if you need a App.config file shared across several projects, I would simply "Add as Link" the original App.config file in each project.
假设您拥有带有原始 App.config 的 ProjectA.然后你有 ProjectATest1 和 ProjectATest2.在每个 TestX 项目中:
Let's say you have ProjectA with the original App.config. Then you have ProjectATest1 and ProjectATest2. In each of the TestX project:
- 在 Visual Studio 中右键单击解决方案名称
- 选择添加现有项目"
- 导航到 ProjectA App.config 并选择它
- 点击添加"按钮向下箭头
- 选择添加为链接"
它将在每个 TestX 项目中创建一个 App.config 快捷方式.如果你改变它,它就会到处改变.
It'll create a App.config shortcut in each TestX project. If you change it, it will change everywhere.
希望这会有所帮助.
这篇关于测试项目中的 App.config的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!