I need to read a setting from the appsettings section (defined in app.config) in a unit test. We're using mstest in this project.
Say this is the app.config:
<configuration>
<appSettings>
<add key="MyAppSetting" value="MyAppSettingValue"/>
</appSettings>
</configuration>
Here's the corresponding test, which passes in this setup:
[TestClass]
public class ConfigurationTests
{
[TestMethod]
public void can_read_appsettings()
{
string value = ConfigurationManager.AppSettings.Get("MyAppSetting");
Assert.AreEqual("MyAppSettingValue", value);
}
}
Now when I try to move the appSettings section to a custom.config file, this test fails.
This is what my app.config file looks like now:
<configuration>
<appSettings file='Custom.config' />
</configuration>
I added the Custom.config file to my project (with build action 'copy always'):
<appSettings>
<add key="MyAppSetting" value="MyAppSettingValue"/>
</appSettings>
When doing the same in a console application, this works. Is there a way to make this work in a unit test assembly as well?
I found the answer. Using mstest, I needed to mark the 'Custom.config' file as a deployment item in the 'localtestrun.testrunconfig' file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With