简体   繁体   English

如何使用“外部”配置文件(即使用configSource)和MSTest单元测试项目?

[英]How can you use “external” configuration files (i.e. with configSource) with an MSTest unit test project?

For simplicity, I generally split a lot of my configuration (ie 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. 为简单起见,我通常将大量配置(即app.config和web.config的内容)拆分为单独的.config文件,然后使用'configSource'属性从主配置文件中引用它们。 For example: 例如:

<appSettings configSource="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.config文件中,而不是在主配置文件中使用此内联:

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

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). (我知道大多数这些都可能被认为是“集成测试”,因为它们依赖于配置系统,我也有“纯单元测试”,但那些不是问题。我真的在寻找测试是否正确检索这些配置值并以正确的方式影响行为)。

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. 由于MSTest如何编译并将输出复制到与每次测试运行不同的模糊文件夹(而不是像你想象的那样到'bin'文件夹),它似乎永远无法在测试时找到那些外部文件正在执行。 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? 有没有办法在运行时将这些外部文件复制到正确的输出文件夹中?

Found it: 找到了:

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. 如果编辑测试运行配置(通过双击在添加新单元测试时放入“解决方案项”解决方案文件夹中的.testrunco​​nfig文件),则会获得测试运行配置对话框。 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. 有一个名为“部署”的部分,您可以在解决方案中的任何位置指定文件或整个文件夹,可以在运行时将已编译的程序集复制到正确的文件夹中。

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. 通过这种方式,我现在可以在一组外部.config文件中定义大部分配置,并在每次测试运行时自动复制它们。

Test run configurations are a bit awkward when trying to run tests outside of Visual Studio. 尝试在Visual Studio外部运行测试时,测试运行配置有点尴尬。

For command line execution using MSTest they become quite cumbersome to keep "clean". 对于使用MSTest的命令行执行,保持“干净”变得非常麻烦。 They are also "global" to the solution so external files will be copied for every test project. 它们也是解决方案的“全局”,因此将为每个测试项目复制外部文件。

I much prefer the DeploymentItem attribute. 我更喜欢DeploymentItem属性。

[TestMethod]
[DeploymentItem(@"test_data.file")]
public void FooTest()
{...}

Makes the tests independent of the .testrunconfig files. 使测试独立于.testrunco​​nfig文件。

  1. write this in your connectionString. 在connectionString中写下这个。 First ConnectionString.config is not exists. 第一个ConnectionString.config不存在。

    <"connectionStrings configSource="ConnectionString.config"> " <“connectionStrings configSource =”ConnectionString.config“>”

  2. open command prompt (CMD) in administrator privileged. 管理员特权的打开命令提示符(CMD)。

  3. Create a symbolic links with the name of ConnectionString.config at bin/debug folder. 在bin / debug文件夹中创建名为ConnectionString.config的符号链接。

C:\\Windows\\Systems32> mklink "C:\\Link To Folder\\....\\ConnectionString.config" "C:\\Users\\Name\\Original Folder\\.....\\...\\Secure ConnectionString.config" C:\\ Windows \\ Systems32> mklink "C:\\Link To Folder\\....\\ConnectionString.config" "C:\\Users\\Name\\Original Folder\\.....\\...\\Secure ConnectionString.config"

finally it creates ConnectionString configuration file at the specified location. 最后,它在指定位置创建ConnectionString配置文件。 and works successfully. 并且成功地工作。

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在一个项目中使用多个应用程序配置文件? - How can I use several Application Configuration Files in one project? 如何运行单元测试项目并将结果返回到外部应用程序? - How can I run a unit test project and return the results to an external application? 如何使用MSTest从测试项目的输出文件夹以外的文件夹运行测试? - How can I run tests from a folder other than the test project's output folder, using MSTest? 我如何知道某个进程(即我的应用程序)当前正在打开哪些文件? - How can I tell what files are currently open by a process (i.e. my app)? 我可以强制MSTest为每次测试运行使用新进程吗? - Can I force MSTest to use a new process for each test run? 使用mstest,我可以针对我支持的每种语言运行单元测试套件吗? - With mstest, can I run my unit test suite against each of my supported languages? 如何创建仪表(即速度表)? - How can I create a gauge (i.e. speedometer)? 如何在单元测试中将 FakeItEasy 与 HttpClient 一起使用? - How can I use FakeItEasy with HttpClient, in a unit test? MSTest中的JavaScript单元测试结果 - JavaScript unit test results in MSTest 如何使用C#使用MsTest对属性进行单元测试? - How to unit test attributes with MsTest using C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM