Can a unit test project load the target application's app.config file?

unit test configurationmanager
read appsettings in unit test
configurationmanager.appsettings null unit test
how to add web config to unit test project
c# unit test change app config
.net core unit test configuration
unit test configuration getsection
access appsettings json in unit test

I am unit testing a .NET application (.exe) that uses an app.config file to load configuration properties. The unit test application itself does not have an app.config file.

When I try to unit test a method that utilizes any of the configuration properties, they return null. I'm assuming this is because the unit test application is not going to load in the target application's app.config.

Is there a way to override this or do I have to write a script to copy the contents of the target app.config to a local app.config?

This post kind-of asks this question but the author is really looking at it from a different angle than I am.

EDIT: I should mention that I'm using VS08 Team System for my unit tests.

The simplest way to do this is to add the .config file in the deployment section on your unit test.

To do so, open the .testrunconfig file from your Solution Items. In the Deployment section, add the output .config files from your project's build directory (presumably bin\Debug).

Anything listed in the deployment section will be copied into the test project's working folder before the tests are run, so your config-dependent code will run fine.

Edit: I forgot to add, this will not work in all situations, so you may need to include a startup script that renames the output .config to match the unit test's name.

Unit Tests Target Earlier Version of .NET Framework, After you set up your test project, you will be able to run your unit tests using These include integration tests, web tests, load tests, and others. You can avoid these dependencies in your application by following the Explicit� The unit test application itself does not have an app.config file. When I try to unit test a method that utilizes any of the configuration properties, they return null . I’m assuming this is because the unit test application is not going to load in the target application’s app.config.

In Visual Studio 2008 I added the app.config file to the test project as an existing item and selected copy as link in order to make sure it's not duplicated. That way I only have one copy in my solution. With several test projects it comes in really handy!

Unit testing in .NET Core and .NET Standard, NUnit is a unit-testing framework for . NUnit-Project-Loading.jpg to a class in your project, the Test Runner application will scan it for test methods. To use the GUI application, just simply run the software and target (open� Can a unit test project load the target application's app.config file? (8) I couldn't get any of these suggestions to work with nUnit 2.5.10 so I ended up using nUnit's Project -> Edit functionality to specify the config file to target (as others have said it needs to be in the same folder as the .nunit file itself).

Whether you're using Team System Test or NUnit, the best practice is to create a separate Class Library for your tests. Simply adding an App.config to your Test project will automatically get copied to your bin folder when you compile.

If your code is reliant on specific configuration tests, the very first test I would write validates that the configuration file is available (so that I know I'm not insane) :

       <add key="TestValue" value="true" />

And the test:

public class GeneralFixture
     public void VerifyAppDomainHasConfigurationSettings()
          string value = ConfigurationManager.AppSettings["TestValue"];
          Assert.IsFalse(String.IsNullOrEmpty(value), "No App.Config found.");

Ideally, you should be writing code such that your configuration objects are passed into your classes. This not only separates you from the configuration file issue, but it also allows you to write tests for different configuration scenarios.

public class MyObject
     public void Configure(MyConfigurationObject config)
          _enabled = config.Enabled;

     public string Foo()
         if (_enabled)
             return "foo!";
         return String.Empty;

     private bool _enabled;

public class MyObjectTestFixture
     public void CanInitializeWithProperConfig()
         MyConfigurationObject config = new MyConfigurationObject();
         config.Enabled = true;

         MyObject myObj = new MyObject();

         Assert.AreEqual("foo!", myObj.Foo());

NUnit With C#, resolution work today, test projects must target a platform (desktop to have a single class library which could support multiple target platforms. unit tests must target a specific platform, just like a console or GUI application does. Unit tests are typically isolated to ensure a unit does not rely on any external code or functions. Testing can be done manually but is often automated. How unit tests work. A unit test typically comprises of three stages: plan, cases and scripting and the unit test itself. In the first step, the unit test is prepared and reviewed.

If you have a solution which contains for example Web Application and Test Project, you probably want that Test Project uses Web Application's web.config.

One way to solve it is to copy web.config to test project and rename it as app.config.

Another and better solution is to modify build chain and make it to make automatic copy of web.config to test projects output directory. To do that, right click Test Application and select properties. Now you should see project properties. Click "Build Events" and then click "Edit Post-build..." button. Write following line to there:

copy "$(SolutionDir)\WebApplication1\web.config" "$(ProjectDir)$(OutDir)$(TargetFileName).config"

And click OK. (Note you most probably need to change WebApplication1 as you project name which you want to test). If you have wrong path to web.config then copy fails and you will notice it during unsuccessful build.


To Copy from the current Project to the Test Project:

copy "$(ProjectDir)bin\WebProject.dll.config" "$(SolutionDir)WebProject.Tests\bin\Debug\App.Config"

Why doesn't support netstandard? >, You can group unit tests that target specific parts of your application into the project/solution, create a new test session, or add tests to a test� Retargeting for C# unit test projects. Create a new C# Unit Test Project project. In Solution Explorer, choose Properties from the right-click menu of your new C# test project. The properties for your C# test project are displayed. On the Application tab, choose Target framework.

This is a bit old but I found a better solution for this. I was trying the chosen answer here but looks like .testrunconfig is already obsolete.

1. For Unit Tests, Wrap the config is an Interface (IConfig)

for Unit tests, config really shouldn't be part of what your testing so create a mock which you can inject. In this example I was using Moq.

Mock<IConfig> _configMock;
_configMock.Setup(config => config.ConfigKey).Returns("ConfigValue");
var SUT = new SUT(_configMock.Object);
2. For Integration test, dynamically add the config you need
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if(config.AppSettings.Settings[configName] != null)
config.AppSettings.Settings.Add(configName, configValue);
config.Save(ConfigurationSaveMode.Modified, true);

Unit Test Sessions, You can group unit tests that target specific parts of your application or from the project/solution, create a new test session, or add tests to a� Open the Local.testsettings file in Solution Items, select Deployment, then select Add Directory to select the location your added .config file, and then you could run the test to check it. A useful article, see Can a unit test project load the target applicaion’s app.config file?

Unit Test Sessions - Help, getTargetContext() , returns the application Context of the target application. A unit tests for an Android application can be: To run a unit test, right-click on your test class in the Project window and select Run. Android system kills any process of the application under test and then loads a new instance. In summary, the test strategy should look like this: *SLT/MDG etc is not displayed here *In case of 3 Tier Landscape QAS can be replace by ACC or vice versa. Let’s understand the overall test approach in detail. Functional Unit Test – Here we test the system in terms of Connectivity, Enhancements, Basic data flow from source & target system

Developing Android unit and instrumentation tests - Tutorial, Section 8.2, "Testing Java Application Projects with Unit Tests" If you have a project that is targeted to run on your local machine, you can profile the To work with the Apache JMeter load testing tool in the IDE, use the Plugins manager� My test project already have the reference to the involved assembly and I can see the assembly (MyAssembly.dll in my case) in the out folder used by the unit test manager. Furthermore the main AppDomain already have MyAssembly.dll loaded. I can see it with a call to AppDomain.GetAssemblies().

Testing and Profiling Java Application Projects, With just a few clicks, you can set up a JUnit test that runs on the local Your project's (1) instrumented tests and (2) local JVM tests are and allows you to run only the tests that apply to a given build variant. If you want a test module to target only certain flavors or build types of an app, you can use the�

  • It's a lot easier to just add an app.config fiel to the test project -- you then don't need to play around with the .testrunconfig at all.
  • @Rowland if you do that, you have to maintain two copies of app.config. I'd rather spend 10 seconds, once, using the .testrunconfig tool than having to remember to update the app.config in both places.
  • Can't you just add a non-copying reference? (Add Existing Item...)
  • EFraim's comment should be the accepted answer, this is much simpler than anything else.
  • For EFraim's soln: Be sure to use the "Add as Link" from the command button. Otherwise you still get a copy. Also, although the question is specifically for a .Net app, this won't work for a web app as a web app's config has the wrong name (Web.Config, not App.Config)
  • Perfect answer, simple and objective! +1
  • This is the best solution and should be marked as the answer.
  • For adding existing item "as a link" you should: "In the Add Existing Item dialog box, locate and select the project item you want to link", then: "From the Open button drop-down list, select Add As Link."
  • This worked great for me. I'm trying to think of a situation where it would not work... And I've thought enough now. Thanks!
  • I agree with the spirit of passing in the configuration dependency here, this seems to have been answered, by Mark Seemann no less! over here: Failing unit tests due to missing .config file
  • There is a " missing from the line: string value = ConfigurationManager.AppSettings["TestValue]; I tried to fix it, but would have had to find a further 5 characters to fix to get stackoverflow to allow me to make an edit.