0

I'm writing some unit tests, this unit test do use code in an external library, this library expects the config file to contain some information. I know that to use App.config in my UnitTest, I need to mark my TestMethod with [DeploymentItem("App.config")], but as far as I know, this will by default look for configuration tags in the section <appSettings>. How can I specify if my App.config defines a custom config section?

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="MySettingSection" type="System.Configuration.AppSettingsSection" />
  </configSections>

  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>

  <MySettingSection>
    <add key="ApplicationName" value="My Test Application" />
  </MySettingSection>

</configuration>
Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206
Tobias Moe Thorstensen
  • 8,861
  • 16
  • 75
  • 143

1 Answers1

1

I know that to use App.config in my UnitTest, I need to mark my TestMethod with [DeploymentItem("App.config")]

This statement is wrong: App.config is deployed by default. This is true in Visual Studio 2012 and above (I can't confirm that in earlier versions because I don't have any of them installed in my machine).

How can I specify if my App.config defines a custom config section?

Add the required configuration section declarations in the App.config and it will work as you expect:

<configSections>
    <section name="YourSection" type="Assembly Qualified Path to the class representing your section" />
  </configSections>

Check this other old answer I did a long time ago (it should guide you on how configuration model works to set configuration and settings for satellite assemblies in your App.config):

Update

In some comment, OP said:

By using my example, if I write the following inside my unit test ConfigurationManager.AppSettings["ApplicationName"]; it will only return null. Is there any Attribute which defines where the UnitTest should look after the "ApplicationName"?

Note your custom declared System.Configuration.AppSettingsSection configuration section isn't the default <appSettings> accessed by ConfigurationManager.AppSettings["ApplicationName"].

In your case, you should access that section this way:

Configuration config =
              ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("MySettingSection");
string appName = appSettings["ApplicationName"];
Community
  • 1
  • 1
Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206
  • By using my example, if I write the following inside my unit test `ConfigurationManager.AppSettings["ApplicationName"];` it will only return `null`. Is there any `Attribute` which defines where the UnitTest should look after the "ApplicationName"? – Tobias Moe Thorstensen Aug 20 '14 at 14:53
  • 1
    @TobiasMoeThorstensen And this is the expected behavior, isn't it? `ConfigurationManager.AppSettings` represents the `` configuration section rather than your custom one. Now I don't understand why you declare a `System.Configuration.AppSettingsSection`. Just use ``! – Matías Fidemraizer Aug 20 '14 at 15:08
  • There is a reason why I use my custom appSection, otherwise I would use `appSettings`. The external library defines that I have to have some settings in this Section. There is no way that I can define this sort of stuff as an `Attribute` to my `TestMethod` ? – Tobias Moe Thorstensen Aug 21 '14 at 06:24
  • @TobiasMoeThorstensen Wait, but have you tried to specify that settings in the regular `` section?!?! There're a lot of apps which require app settings, but in the regular one!!! – Matías Fidemraizer Aug 21 '14 at 07:21
  • @TobiasMoeThorstensen It seems to be like a very bad library design... BTW, have you checked my update? I feel you didn't tried that... – Matías Fidemraizer Aug 21 '14 at 09:07
  • Yes, I've tried it, but I need a way to specify this in the attribute, not by "code". Thanks for you effort. – Tobias Moe Thorstensen Aug 21 '14 at 09:38
  • @TobiasMoeThorstensen I really believe that your issue is simple but I'm very sure you're stuck in your mind. Declaring config sections has no more than one approach and accessing them too. At the end of the day, I'm not 100% sure you're doing something wrong or you're not describing your issue with exact details. Give more info or no one will be able to assist you... – Matías Fidemraizer Aug 21 '14 at 10:10