I've created a simple unit test project to read an app.config file. Target framework is Core 2.0. I also created a Core 2.0 console app, to sanity-check myself to make sure I wasn't doing anything weird (same test passed as expected in a .NET 4.6.1 unit test project).
The console app reads the app.config fine, but the unit test method fails and I cannot figure out why. Both are using a copy of the same app.config (not added as a link) and both have the System.Configuration.ConfigurationManager v4.4.1 NuGet package installed.
The App.config
<?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="Test1" value ="This is test 1."/> <add key="Test2" value ="42"/> <add key="Test3" value ="-42"/> <add key="Test4" value="true"/> <add key="Test5" value="false"/> <add key="Test6" value ="101.101"/> <add key="Test7" value ="-1.2345"/> </appSettings> </configuration>
The Unit Test
using Microsoft.VisualStudio.TestTools.UnitTesting; using System.Configuration; namespace ConfigTest { [TestClass] public class UnitTest1 { [TestMethod()] public void ConfigTest() { foreach (string s in ConfigurationManager.AppSettings.AllKeys) { System.Console.WriteLine(s); System.Diagnostics.Debug.WriteLine(s); } //AllKeys.Length is 0? Should be 7... Assert.IsTrue(ConfigurationManager.AppSettings.AllKeys.Length == 7); } } }
The Console App
using System; using System.Configuration; namespace ConfigTestApp { class Program { static void Main(string[] args) { foreach (string s in ConfigurationManager.AppSettings.AllKeys) { Console.WriteLine(s); System.Diagnostics.Debug.WriteLine(s); } //Outputs 7 as expected Console.WriteLine(ConfigurationManager.AppSettings.AllKeys.Length); } } }
Given that I'm still pretty new to the whole .NET Core world, am I doing something totally incorrect here? I sort of just feel crazy at the moment...
Add the App. config file to your project. After creating a . NET Framework project, right-click on your project in Solution Explorer and choose Add > New Item. Choose the Application Configuration File item and then select Add.
App. Config is an XML file that is used as a configuration file for your application. In other words, you store inside it any setting that you may want to change without having to change code (and recompiling). It is often used to store connection strings.
To access these values, there is one static class named ConfigurationManager, which has one getter property named AppSettings. We can just pass the key inside the AppSettings and get the desired value from AppSettings section, as shown below.
Looking through the github issue's comments, I found a work around that can go in the msbuild file...
<Target Name="CopyCustomContent" AfterTargets="AfterBuild"> <Copy SourceFiles="app.config" DestinationFiles="$(OutDir)\testhost.dll.config" /> </Target>
This makes it easier to verify existing tests under .NET Core before porting the configuration data over to json configuration files.
Edit
If running under Resharper, the previous answer doesn't work as Resharper proxies the assembly, so you need
<Target Name="CopyCustomContent" AfterTargets="AfterBuild"> <Copy SourceFiles="app.config" DestinationFiles="$(OutDir)\ReSharperTestRunner64.dll.config" /> </Target>
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