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

时间:2023-01-13 13:31:48

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:

为简单起见,我通常将大量配置(即app.config和web.config的内容)拆分为单独的.config文件,然后使用'configSource'属性从主配置文件中引用它们。例如:

<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. 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?

由于MSTest如何编译和复制输出到与每次测试运行不同的模糊文件夹(而不是像你想象的那样到'bin'文件夹),它似乎永远无法在测试时找到那些外部文件正在执行。我已经尝试过使用后期构建操作来完成这项工作,但没有运气。有没有办法在运行时将这些外部文件复制到正确的输出文件夹中?

3 个解决方案

#1


11  

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. 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.

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

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文件中定义大部分配置,并在每次测试运行时自动复制它们。

#2


10  

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". They are also "global" to the solution so external files will be copied for every test project.

对于使用MSTest执行命令行,保持“干净”变得非常麻烦。它们也是解决方案的“全局”,因此将为每个测试项目复制外部文件。

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文件。

#3


1  

  1. write this in your connectionString. First ConnectionString.config is not exists.

    在你的connectionString中写这个。第一个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.
  4. 在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. and works successfully.

最后,它在指定位置创建ConnectionString配置文件。并且成功地运作。

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

#1


11  

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. 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.

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

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文件中定义大部分配置,并在每次测试运行时自动复制它们。

#2


10  

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". They are also "global" to the solution so external files will be copied for every test project.

对于使用MSTest执行命令行,保持“干净”变得非常麻烦。它们也是解决方案的“全局”,因此将为每个测试项目复制外部文件。

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文件。

#3


1  

  1. write this in your connectionString. First ConnectionString.config is not exists.

    在你的connectionString中写这个。第一个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.
  4. 在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. and works successfully.

最后,它在指定位置创建ConnectionString配置文件。并且成功地运作。

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