How do I configure my settings file to work with unit tests?

I have a library I'm writing unit tests for. The library is used by two applications: one a Windows service, the other a command-line application that does some registry read-writes. Each has a slightly different App.config file that is loaded from the library at start-up. For example:

public RetentionService() { SettingHive = new Hive(); TimingService = new RetentionTimingService(SettingHive); AppSettingsReader asr = new AppSettingsReader(); object appsListObj = asr.GetValue(@"blocking-process-list", Type.GetType(@"System.String")); string appsList = appsListObj.ToString(); _BlockingAppNames = RetentionService.ListFromList(appsList); string targetList = asr.GetValue(@"target-files", Type.GetType(@"System.String")).ToString(); _TargetLogs = RetentionService.ListFromList(targetList); }

When I try to use this library from a unit test, it fails to load because the application loading the library (presumably nunit) doesn't have a *.exe.config file with the appropriate keys.

What's a better way to do this? I'd like the library to load the settings from each application's *.exe.config in production, but from a third location if running a unit test.


If your unit tests are designed to test the code, then don't depend on the config file at all. Extract your dependency out of your classes and use dependency injection to inject the data in. That way, you can stub your configuration class.

If you are actually just testing your configuration file, you should be able to load it explicitly using ConfigurationManager, although I wouldn't suggest unit testing configuration data. It's a better candidate for smoke testing.


Alternatively, just add an app.config file to your unit testing project that contains the relevant information.


Your best bet may be to wrap up access to the config data within a proxy class that you can redirect as needed at runtime -- don't use the builtin APIs directly.


  • lodash _.get function in typescript
  • How can I use 'addChildEventListener()' accurately for following purpose?
  • JavaFX TableView click sorters not working?
  • Get current day's steps during datapointListener google Fit
  • Serial communication over USB converter in Python - how to aproach this?
  • How to prevent TreeItem selection?
  • Cannot connect to native local socket on android 5.1
  • Confirm box return value from alertify plugin and jquery
  • Disable Kendo Autocomplete
  • Adding elements to a huge XML file
  • Object and struct member access and address offset calculation
  • Mysql - How to search for 26 records that each begins with the letter of the alphabet?
  • Calling Worksheet functions from vba in foreign language versions of Excel
  • Retrieve list of sent friend requests from friend_request FQL table
  • Declaring variable dynamically in VB.net
  • GridView breaks while scrolling
  • How can I sort a a table with VBA with given text condition?
  • Limiting recursion to certain level - Duplicate rows
  • java inputstream
  • MongoDb aggregation
  • one Local Olampyad Questions on Informatic in 2011
  • Replace value with Factor in r data.table
  • Could not find rake using whenever rails
  • Splitting given String into two variables - php
  • Align navbar back button on right side
  • How to add a column to a Pandas dataframe made of arrays of the n-preceding values of another column
  • How to apply VCL Styles to DLL-based forms in Inno Setup?
  • How to format a variable of double type
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • Comma separated Values
  • ExecuteAsync RestSharp to allow backgroundWorker CancellationPending c#
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • Understanding cpu registers
  • How do I configure my settings file to work with unit tests?
  • Turn off referential integrity in Derby? is it possible?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Add sale price programmatically to product variations
  • Binding checkboxes to object values in AngularJs
  • Unable to use reactive element in my shiny app
  • How do I use LINQ to get all the Items that have a particular SubItem?