[英]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. 如果编辑测试运行配置(通过双击在添加新单元测试时放入“解决方案项”解决方案文件夹中的.testrunconfig文件),则会获得测试运行配置对话框。 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. 使测试独立于.testrunconfig文件。
write this in your connectionString. 在connectionString中写下这个。 First ConnectionString.config is not exists. 第一个ConnectionString.config不存在。
<"connectionStrings configSource="ConnectionString.config"> " <“connectionStrings configSource =”ConnectionString.config“>”
open command prompt (CMD) in administrator privileged. 管理员特权的打开命令提示符(CMD)。
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.