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

StackOverflow https://stackoverflow.com/questions/152866

  •  02-07-2019
  •  | 
  •  

Question

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"/>

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>

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?

Was it helpful?

Solution

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.

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.

OTHER TIPS

Test run configurations are a bit awkward when trying to run tests outside of 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.

I much prefer the DeploymentItem attribute.

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

Makes the tests independent of the .testrunconfig files.

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

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

  2. open command prompt (CMD) in administrator privileged.

  3. Create a symbolic links with the name of ConnectionString.config at bin/debug folder.

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.

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top