单元测试项目能否加载目标应用程序的 app.config 文件? [英] Can a unit test project load the target application's app.config file?

查看:24
本文介绍了单元测试项目能否加载目标应用程序的 app.config 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在对使用 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.

当我尝试对使用任何配置属性的方法进行单元测试时,它们返回 null.我假设这是因为单元测试应用程序不会加载到目标应用程序的 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 Team System 进行单元测试.

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 文件.在部署部分,从项目的构建目录(大概是 bin\Debug)中添加输出 .config 文件.

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 bin\Debug).

在运行测试之前,部署部分中列出的任何内容都将被复制到测试项目的工作文件夹中,因此您的依赖于配置的代码将正常运行.

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.

这篇关于单元测试项目能否加载目标应用程序的 app.config 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆