如何使用"外部"配置文件(即带有configSource)与MSTest的单元测试项目? [英] How can you use "external" configuration files (i.e. with configSource) with an MSTest unit test project?

查看:195
本文介绍了如何使用"外部"配置文件(即带有configSource)与MSTest的单元测试项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为简单起见,我一般分裂了很多我的配置(即的app.config和web.config文件中的内容),分为不同的.config文件,然后从使用'configSource'属性的主配置文件中引用它们。例如:

For simplicity, I generally split a lot of my configuration (i.e. the contents of app.config and web.config) out into separate .config files, and then reference them from the main config file using the 'configSource' attribute. For example:

<appSettings configSource="appSettings.config"/>

然后将具有这行中的主配置文件的所有键/值对在该appSettings.config文件,而不是:

and then placing all of the key/value pairs in that appSettings.config file instead of having this in-line in the main config file:

<appSettings>
    <add key="FirstKey" value="FirstValue"/>
    <add key="SecondKey" value="SecondValue"/>
    ...
</appSettings>

这通常伟大工程与应用程序本身,而是我遇到问题试图编写单元测试,无论出于何种原因,需要得到从存储在这些外部文件中的一个配置节一定值时。 (据我所知,大多数这些将likley被认为是集成测试,因为它们依赖于系统的配置上,和我有纯单元测试为好,但这些都是没问题的。我真的很来测试这些配置值正确检索并以正确的方式影响行为)。

This typically works great with the application itself, but I run into problems when attempting to write unit tests that, for whatever reason, need to get at some value from a configuration section that is stored in one of these external files. (I understand that most of these would likley be considered "integration tests", as they are relying on the Configuration system, and I do have "pure unit tests" as well, but those are the not the problem. I'm really looking to test that these configuration values are retrieved correctly and impact behavior in the correct way).

由于MSTest的如何编译和复印件输出到模糊,看文件夹,可从每个测试运行(而不是在'bin文件夹像你想象的)不同,它似乎永远能找到这些外部文件而测试执行。我试着瞎搞与交建的行动,使这项工作,但没有运气。有没有办法让复制到正确的输出文件夹在运行时,这些外部文件?

Due to how MSTest compiles and copies the output to obfuscated-looking folders that are different from every test run (rather than to the 'bin' folder like you might think), it never seems to be able to find those external files while the tests are executing. I've tried messing around with post build actions to make this work but with no luck. Is there a way to have these external files copied over into the correct output folder at run time?

推荐答案

发现:

如果您编辑测试运行配置(通过双击该被放进了解决方案项解决方案文件夹,当你添加一个新的单元测试.testrunco​​nfig文件),你会得到一个测试运行配置对话框。有一节还有所谓的部署在这里,你可以从任何地方可以复制出与编译的程序集在运行时正确的文件夹的解决方案specifiy文件或整个文件夹。

If you edit the test run configuration (by double clicking the .testrunconfig file that gets put into the 'Solution Items' solution folder when you add a new unit test), you get a test run configuration dialog. There's a section there called 'Deployment' where you can specifiy files or whole folders from anywhere in the solution that can be copied out with the compiled assemblies at run time to the correct folder.

在这种方式,我可以现在实际上只是定义了我的大部分配置在一组外部.config文件,并让它们自动地复制出来,在每个测试运行。

In this way, I can now actually just define most of my configuration in one set of external .config files and have them automatically copied out at the run of each test.

这篇关于如何使用&QUOT;外部&QUOT;配置文件(即带有configSource)与MSTest的单元测试项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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