Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't read app.config in C# .NET Core unit test project with ConfigurationManager

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

Both projects with an app.config

like image 480
Broots Waymb Avatar asked Feb 07 '18 14:02

Broots Waymb


People also ask

How read app config file in C# Windows application?

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.

What is app config in C?

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.

How do I access AppSettings in C#?

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.


1 Answers

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> 
like image 149
Paul Hatcher Avatar answered Sep 19 '22 18:09

Paul Hatcher