Generic section handler – HowTo


After being on vacation for the last two weeks and therefore suffering a little from internet abstinence syndrome,  I am back with a vengeance. No not really but I do feel refreshed and ready to take on anything.

So to get me a little in to the “flow” here is a post that I was planning on publishing before vacation time struck.

It is nothing special just a little testing helper I wrote to get some much needed tests done.

If you are developing a .NET component that is going to be part of a bigger application (or solution), then chances are high that you will provide a so called “custom configuration section”. There are other ways to configure a component but a custom configuration section is quite elegant.

So youz start to develop your custom configuration section. I hope that I do not have to cover the nuts and bolts of how to get this done. What I want to convey here is the aswer to the question how to test this.

After you implement you object structure you are faced with quite a nice little problem of how to verify that your configuration is “bullet prof”? There are two answers to this question:

  1. You write the XML configuration for all the possible permutations and combination’s of how your configuration section could be used
  2. You test each configuration “element” on its own and verify that each functions ass desired.

No. 2 is definitely the better option and therefore should be used. And there is another argument for option no. 2: If the configuration section is relatively complex then the number of combination goes through the roof. If you ever put the effort into testing custom configuration section then you know that this is not easily achieved.

But neither the less lets get to explaining how this is done. My testing framework of choice is nUnit so all code samples will be done using it.

I take that you have your configuration elements already defined. So now all you need to do is to write a simple generic configuration section handler

using System.Configuration;
using NUnit.Framework;

namespace Tests.Helpers
{
 public class GenericSectionHandler : ConfigurationSection where T : ConfigurationElement
 {
 private const string ELEMENT_NAME = "TestElement";

 [ConfigurationProperty(ELEMENT_NAME, IsRequired = true)]
 public T GetConfiguration
 {
 get
 {
 return (T) this[ELEMENT_NAME];
 }
 }

 public static T Get(string sectionName)
 {
 var sectionHandler = ConfigurationManager.GetSection(sectionName) as GenericSectionHandler;
 if(sectionHandler == null)
 Assert.Fail("section [{0}] does not exist", sectionName);

 return sectionHandler.GetConfiguration;
 }
 }
}

After you have that in place inside your code you have to add the configuration group to your test project configuration file. This could look like the following:

<sectionGroup name="SectionTests">
 MyTest" type="Tests.Helpers.GenericSectionHandler`1[[MyApplication.Configuration.MySection, MyApplication]], Test"/>
</sectionGroup>

Now you can just add your configuration scenario.

<SectionTests>
  <MyTest>
    <TestElement>
      <!-- YOUR CONFIGURATION ELEMENT -->
    </TestElement>
  </MyTest>
</SectionTests>

Now you can use your configuration section scenario in your actual unit tests.

public void MySectionTests()
{
  var mySection = GenericSectionHandler.Get("SectionTests/MyTest");
  // do your assertions
}

And that is that.

Not the best post I ever had I know but since I was not blogging for quite some time I hope that this will be a nice start of getting into the habit of blogging 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s