2

I've been trying to read an app.config file during unit testing following the instructions in this Using a Configuration File to Define a Data Source using Visual Studio 2010. I've read numerous posts here in the forum as well, but I'm still getting a null value when trying to read the app.config values. Below are snippets of my app.config and unit test code.

App.config

<configuration>
  <configSections>
    <section name="microsoft.visualstudio.testtools" type="Microsoft.VisualStudio.TestTools.UnitTesting.TestConfigurationSection, Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
  </configSections>
  <connectionStrings>
    <add name="DatabaseConnectionString" connectionString="Data Source=***;User Id=***;Password=***;Integrated Security=no" providerName="Oracle"/>
  </connectionStrings>

  <microsoft.visualstudio.testtools>
    <dataSources>
      <add name="MyOracleConnection" connectionString="DatabaseConnectionString" dataTableName="***" dataAccessMethod="Sequential"/>

    </dataSources>
  </microsoft.visualstudio.testtools>
</configuration>

Unit Test:

    [TestMethod()]
    [DeploymentItem("app.config")]
    public void S_ConstructorTest(){
    myObject m = new myObject();
       ...}

In my object code I'm attempting to read the config file via:

public static readonly string sConnectionString = System.Web.Configuration.WebConfigurationManager.AppSettings["sConnectionString"];

Based on what I've read in other posts here, MSDN seems to add more than is necessary for this to work, however I still keep getting null values when reading the config file. I know that there's a debate as to whether or not unit testing should access a real DB but for now I just need to get it to read the config file.

Any help would be appreciated.

Trebor
  • 793
  • 3
  • 11
  • 37
  • are you able to access the settings when you run the application normally? – lockstock Oct 13 '14 at 23:22
  • Yes, I can. It only seems to be a problem during testing. – Trebor Oct 27 '14 at 15:24
  • do you have the app.config file in the same project as your tests? – lockstock Oct 29 '14 at 00:37
  • I'm having essentially the same problem. .config file is apparently not being read in unit tests, even though it is there and named the same as the unit test DLL (only with ".config" appended). See http://stackoverflow.com/questions/31618105/unit-test-not-reading-app-config – MiloDC Jul 24 '15 at 20:35

0 Answers0