0

I have created a Console application project(proj name: ConsoleApp) with .Net core 3.1. I have added a specflow feature file where in I have defined my test scenario and also a class file which has my test case

[Given(@"I have started the application")]
public void GivenIHaveStartedTheApplication()
{
    TestClass testClass1 = new TestClass();
    testClass1.ReadConfigValues();
}

Also, I have added an App.config in the same ConsoleApp Project which looks like this

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="User" value="Admin" />
    <add key="Password" value="nopassword" />
  </appSettings>
</configuration>

TestClass is a C# class file which is residing in a different Class Library project(proj name: TestProject) with .Net Framework 4.7. This project has been referenced by ConsoleApp

Code for TestClass is as below

public class TestClass
{
    public void ReadConfigValues()
    {
        string user = ConfigurationManager.AppSettings["User"];
        string password = ConfigurationManager.AppSettings["Password"];
    }
}

The issue I'm facing here is that the values for both user and password are returned as null. Basically ConfigurationManager.AppSettings has a count equal to zero.

Could anyone point out if I'm missing anything?

mithun
  • 41
  • 10
  • Why do you need `app.config` in .net core app? – Pavel Anikhouski Jul 30 '20 at 08:03
  • 1
    The configuration used by your application cannot be read by the test framework, which is running as a separate process. You can either add the configuration file to the test project itself, or engage in some heavy-handed reflection to retarget things, or step away from using `ConfigurationManager` and refactor configuration code into its own class. See also [this](https://stackoverflow.com/q/344069/4137916), [this](https://stackoverflow.com/q/5674619/4137916), [this](https://stackoverflow.com/q/48666780/4137916) and probably other questions that I missed (not sure which is the best dupe). – Jeroen Mostert Jul 30 '20 at 08:03
  • @PavelAnikhouski I would like to have my application configurable. The settings added under app.config to be accessible for other class libraries – mithun Jul 30 '20 at 08:12
  • What about `appsettings.json`? – Pavel Anikhouski Jul 30 '20 at 08:14
  • Thanks @JeroenMostert for the insight – mithun Jul 30 '20 at 11:31

0 Answers0