Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

App.config in Test Projects

I'm building an ASP.NET app in VS2010. I have a number of separate assemblies (class libraries) and corresponding Test projects for each.

In one of the class libraries I use an App.config file to store settings. The assembly itself uses the following code to retrieve settings:

 string tmp = ConfigurationManager.AppSettings["mySetting"]; 

The problem is that when I try to create a Unit Test in a separate test project, the test does not pick up the setting in the App.config file. If I COPY the App.config file into the Test project, it works.

How can I ensure that each assembly uses its own copy of an App.config file. It would introduce problems if I had to copy config files around. There's an additional problem because there may be multiple config files, one per assembly - how could they all co-exist in a single test project anyway?

Thanks!

like image 962
jqwha Avatar asked Apr 15 '11 09:04

jqwha


People also ask

What is app config?

App. Config is an XML file that is used as a configuration file for your application. In other words, you store inside it any setting that you may want to change without having to change code (and recompiling). It is often used to store connection strings.

Where is app config file located?

The application configuration file usually lives in the same directory as your application. For web applications, it is named Web. config. For non-web applications, it starts life with the name of App.

What is the difference between app config and web config?

Web. Config is used for asp.net web projects / web services. App. Config is used for Windows Forms, Windows Services, Console Apps and WPF applications.


1 Answers

Well, if you need a App.config file shared across several projects, I would simply "Add as Link" the original App.config file in each project.

Let's say you have ProjectA with the original App.config. Then you have ProjectATest1 and ProjectATest2. In each of the TestX project:

  1. right click on the solution name in Visual Studio
  2. select "Add Existing Item"
  3. navigate to the ProjectA App.config and select it
  4. click in the "Add" button down arrow
  5. select "Add as Link"

It'll create a App.config shortcut in each TestX project. If you change it, it will change everywhere.

Hope this helps.

like image 55
nunaxe Avatar answered Sep 28 '22 06:09

nunaxe