可以在单元测试项目加载目标应用程序的app.config文件?应用程序、单元测试、加载、目标

2023-09-02 11:50:14 作者:跟空气撒个娇

我是单元测试,使用一个app.config文件来加载配置属性.NET应用程序(.exe文件)。单元测试应用程序本身不具有app.config文件。

I am unit testing a .NET application (.exe) that uses an app.config file to load configuration properties. The unit test application itself does not have an app.config file.

当我试图单元测试,利用任何的配置属性的方法,它们返回的空的。我猜想这是因为单元测试应用程序不会在目标应用程序的App.config中加载。

When I try to unit test a method that utilizes any of the configuration properties, they return null. I'm assuming this is because the unit test application is not going to load in the target application's app.config.

有没有一种方法来覆盖这还是我写一个脚本目标的app.config的内容复制到本地的app.config?

Is there a way to override this or do I have to write a script to copy the contents of the target app.config to a local app.config?

这的后一种 - 会问这个问题,但笔者真的是从不同的角度看它比我。

This post kind-of asks this question but the author is really looking at it from a different angle than I am.

编辑:我要指出,我使用VS08团队系统为我的单元测试

I should mention that I'm using VS08 Team System for my unit tests.

推荐答案

要做到这一点是将的.config 文件中的部署部分在设备上添加的最简单方法试验。

The simplest way to do this is to add the .config file in the deployment section on your unit test.

要做到这一点,从您的解决方案项打开 .testrunco​​nfig 文件。在部署部分,添加输出的.config 从项目的构建目录中的文件(presumably 斌调试 )。

To do so, open the .testrunconfig file from your Solution Items. In the Deployment section, add the output .config files from your project's build directory (presumably binDebug).

在部署一节中列出的内容会被复制到测试项目的工作文件夹中的测试运行之前,让您的配置依赖code将会运行得很好。

Anything listed in the deployment section will be copied into the test project's working folder before the tests are run, so your config-dependent code will run fine.

编辑:我忘了补充,这将无法工作在所有情况下,所以你可能需要包括重命名输出启动脚本的.config 匹配单元测试的名字。

I forgot to add, this will not work in all situations, so you may need to include a startup script that renames the output .config to match the unit test's name.