Thursday, August 4, 2011

How to read from App.Config in a C# unit test project.

using System.Configuration;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace UnitTestProject
{
   [TestClass]
   public class UnitTest
   {
      [TestMethod]
      public void TestMethod()
      {
         AppSettingsReader appSettingsReader = new AppSettingsReader();
         string baz = (string)appSettingsReader.GetValue("foo", typeof (string));
         Assert.AreEqual(baz, "bar");
      }
   }
}

 
 

The build action on the App.config file needs to be "Content" and the "Copy to Output Directory" setting needs to be "Copy if newer." More: App.config should be configured as you might expect to accomodate appSettings. Mine looks like this:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
   <appSettings>
      <add key="foo" value="bar" />
   </appSettings>
</configuration>

 
 

Addendum 7/23/2013: Guys, what is above has been spliced in from this this. As this blog posting used to just have the orange code below which is bad. I have tried to put a link on this bad blog posting to the good blog posting, but everyone just finds the bad blog posting instead so now the good stuff is shoved into it. If you would like to see the bad stuff...

Ignore the rest of this old article. It is bad. Again, look at this instead please.

 
 
 
 
 
 
 
 
 

In the past I have struggled to read from App.Config in a test project. Here is a pretty good way to go about it:

<?xml version="1.0" encoding="utf-8"?>

<configuration>

   <configSections>

      <section name="Foo" type="App.Core.Folder,App.Core,Version=1.0.0.0"/>

   </configSections>

   <Foo>

      <MyConfig bar="baz" />

   </Foo>

</configuration>

 
The build action on the App.config file (above) needs to be "Content" and the "Copy to Output Directory" setting needs to be "Copy if newer." A test will look like so:

using System.Configuration;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace App.Tests

{

   [TestClass]

   public class TestTest

   {

      [TestMethod]

      public void TestMethod1()

      {

         var qux = (MySection)ConfigurationManager.GetSection("Foo");

         Assert.AreEqual(qux.config.bar, "baz");

      }

   }

}

 
MySection looks like so:

using System.Configuration;

namespace App.Core

{

   public class MySection : ConfigurationSection

   {

      [ConfigurationProperty("MyConfig")]

      public MyElement config

      {

         get

         {

            MyElement myConfig = (MyElement)base["MyConfig"];

            return myConfig;

         }

      }

   }

}

 
MyElement looks like so:

using System.Configuration;

namespace App.Core

{

   public class MyElement : ConfigurationElement

   {

      #region ctor

      public MyElement()

      {

      }

      #endregion

      public override bool IsReadOnly()

      {

         return false;

      }

      [ConfigurationProperty("bar", IsRequired = true)]

      public string Bar

      {

         get { return (string)this["bar"]; }

         set { this["bar"] = value; }

      }

   }

}

No comments:

Post a Comment