Archive for June, 2018

How to unit test a config file, (ConfigurationSection)

June 17th, 2018 Comments off

To unit test a ConfigurationSection in c# you need to do a couple of thing in your unit test
Of course, we do not want to change anything in the assembly being tested.

  • Create your own ConfigurationSection class
  • ‘Fake’ your own configuration file.
  • Test that you have the expected behaviour
    • For required values
    • Optional values

In your test application, create your own configuration loader class
It uses your configuration section as a template.

Of course we also need to clean up things a little.
The file we created needs to be removed, you could also have this in your test teardown.

Creating the fake test file.

Now testing the config is fairly straight forward, (using NUnit in this case)

(using the sample config content, my parser does not escape it properl)

<?xml version=""1.0"" encoding=""utf-8""?>
<section name=""blah"" type=""BlahConfiguration,MyApp.Blah"" />

So now you can create your tests and just pass the values you want to test, default values for example, that values are loaded properly and so on.