Access App.Config Settings from Class Library Called through Unit Test Project

30,544

Solution 1

I just found the solution here. App.config is now being used properly when running my tests through the NUnit GUI.

Apparently if you are using the NUnit GUI and add the assembly by going through Project > Add Assembly, it doesn't access the app.config. However, if you add the assembly to the NUnit project by dragging the dll from Windows Explorer into the NUnit GUI, then it will access the app.config.

Alternatively, you can add the assembly through the GUI and then go in the NUnit GUI > Project > Edit, and set the Configuration File Name to the name of the configuration file (VS will set this to name.of.your.dll.config) and set the Project Base to the \bin\Debug directory of your project (these are the extra steps that are done in the background when you drag in the assembly vs adding it manually.

Solution 2

I'd recommend changing the design such that your business-logic layer, instead of having the responsibility to locate configuration settings, is injected with them.

Your Web app could inject settings it reads from its Web.config file, while your unit test could inject different settings (e.g. connection string to a test database, etc.)

Solution 3

Just rename app.config to name.of.your.dll.config. It works for me.

Share:
30,544
Thiago Silva
Author by

Thiago Silva

Husband, father, learner, and programmer. Trying to live life as it should be lived. Developer @ Stack Overflow since 10/2013. Currently Staff Software Engineer and Tech Lead on the Public Platform team, and Community Advocate. Email me at [email protected]. Former Team Lead & Principal Web Dev, Internal Dev Team through 4/2019.

Updated on March 07, 2020

Comments

  • Thiago Silva
    Thiago Silva about 4 years

    I have the following setup:

    • ASP.net 3.5 Web Site Project
    • C# Class Library with business logic
    • C# Class Library for unit testing

    The business logic library does all of the db access. It gets connection strings from the web.config file of the web site by accessing System.Configuration.ConfigurationManager.ConnectionStrings. When the library is called by the web site, this works fine, as the library looks for the config of the caller.

    I want to be able to test my business logic through the unit testing class library. I have put an App.config file in the root of the testing class library. From what I read, when the testing library calls data access procedures that are part of the business logic library, the connection settings from the App.config file of the testing library should be accessed and used. However, when I try to run my unit tests, I am getting errors back that indicate that the testing library's App.config file (and/or its contents) is not being accessed successfully.

    My retrieval of the config properties (from within the business logic library) looks like this:

    public SqlConnection MainConnection {
      get {
        string conn = "";
        try {
          conn = System.Configuration.ConfigurationManager.ConnectionStrings["connString"].ConnectionString;
        } catch {
          // might be calling from test project. Need to reference app settings
          conn = System.Configuration.ConfigurationManager.AppSettings["connString"];
        }
        return new SqlConnection(conn);
      }
    }
    

    When this is called from the website project, it works. From within the unit test, the conn variable is never set to anything (I have also tried System.Configuration.ConfigurationSettings.AppSettings, and using instead of with the same result). What do I need to do to make the business logic class library successfully retrieve the unit test class libraries settings, when called from within the NUnit GUI?

  • Thiago Silva
    Thiago Silva over 14 years
    Just tried this. WebConfigurationManager.OpenWebConfiguration needs a virtual path - and since I am not calling this from within the site, I can't provide a virtual path.
  • Thiago Silva
    Thiago Silva over 14 years
    My situation is that I am introducing unit testing into the project after quite a bit of work is done. I have moved all of the code from App_Code into its own stand-alone library in order to be able to do unit testing. But at this point, dependency injection is a bit out of scope.
  • John S.
    John S. about 12 years
  • Chlebta
    Chlebta almost 10 years
    sorry but I tried to do this it's not working, I'have console app and I got only the exe file no dll