0

I have set up a test project as I normally do with an App.config file in the root of the project. I then set up folders for the different types of tests e.g. ServiceTests etc.

I've got my first set of tests in a file in that folder and trying to reference the appSettings section in the App.config, but getting nothing back. I've tried the following:

var setting = ConfigurationManager.AppSettings["MySetting"];

as well as more broadly:

var settings = ConfigurationManager.GetSection("appSettings") as AppSettingsSection;

In both cases, I get null back. Does anyone have any suggestions?

sr28
  • 4,728
  • 5
  • 36
  • 67

3 Answers3

1

The configuration manager doesn't read app.config for DLLs, only the executable that is running (in this case, the test runner). So your app.config for the test assemblies is not being found / used.

You'll have to manually find and load the XML and parse it yourself.

Or, to be more unit test friendly, hide the configuration for the system behind your own interface / service, use the app.config settings in the main application and mock it with test specific settings in your unit tests

MarcE
  • 3,586
  • 1
  • 23
  • 27
  • So are you saying that my app.config should be found but for some reason isn't? As I said in the question I often setup my test projects this way, where I simply create an app.config file in the test project and put the relevant settings in to run the tests from the main project's web.config. What I'm wondering is why this now doesn't seem to work. – sr28 Aug 16 '18 at 16:24
  • So it's a web project? That may be different, I'm not too familiar with them. In general terms though a unit test project just builds a DLL, and the config framework has never supported reading app.config for DLLs. At build time app.config for an executable is translated to "myapp.exe.config" and that is what is used. Web apps may do things differently. Personally I don't use app.config in my test projects, I mock a config service and supply test configuration via another mechanism. – MarcE Aug 16 '18 at 18:09
  • When you say you supply test configuration via "another mechanism" what do you mean? The reason I ask is that I'm mocking out my IApplicationSettings interface in the tests. I'm then simply trying to set elements of the object by pulling strings (endpoint urls) from the app.config in the test project. I can set them right there in the test setup but just wondered why this isn't working really. – sr28 Aug 17 '18 at 07:19
  • I'd do exactly that, mock it out and substitute in the test. I'd probably just bake the test endpoints into the tests, it keeps things simple.As to why it's not working, I'd be interested in why it ever worked before. I wouldn't expect an app.config to be read from a test as like I said, the app.config mechanism doesn't work for DLLs only executables. If you put settings in the app.config for the test runner exe, then you could read them. But app.config for a test project, don't know how that would work. – MarcE Aug 17 '18 at 07:29
  • You could find the current working directory for your test (exactly how depends on what test framework you're using), look in there for the app.config or some other config file of your own making, then load it up and pull the config out, if you want to leave it in a config file. I like bundling data files as embedded resources and extracting them to disk or memory at setup to avoid location related problems with data. – MarcE Aug 17 '18 at 07:36
  • So, still not sure why this works in 1 of my Web Api's but not this 1, but +1 for additional info. I'm doing some testing now on whether I should be using WebConfigurationManager instead, but at first glance it looks like a wrapper. – sr28 Aug 17 '18 at 13:53
0

I found what seems to be your answer here.

"If you are UNIT TESTING you need A COPY of the APP.CONFIG inside the UNIT TEST PROJECT"

Also worth noting a suggestion from someone on that same question, so you do not need to duplicate the actual config file: https://stackoverflow.com/a/11535980/2654498

Nick
  • 882
  • 2
  • 9
  • 31
0

Did you set Build Action to the file ?

It should be set to None and Copy to Output Directory.

enter image description here

Also, in the output folder, do you have a file named <assembly_name>.dll.config ?

Zysce
  • 1,200
  • 1
  • 10
  • 35
  • It's already got these settings. How would these impact being able to access the file if it's in the same project? – sr28 Aug 16 '18 at 16:32