Can a unit test project load the target application's app.config file?
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.
How do I get a c# Unit Test to use App.Config?
You have to tell the configuration manager what file to load. Don't rely on the file matching the exe name, etc. Just keep the name as app.config.
System.Configuration.ConfigurationFileMap configMap = new ConfigurationFileMap("./app.config");
System.Configuration.Configuration configuration = System.Configuration.ConfigurationManager.OpenMappedMachineConfiguration(configMap);
string value = configuration.AppSettings["TestKey”];
Can't read app.config in C# .NET Core unit test project with ConfigurationManager
Looking through the github issue's comments, I found a work around that can go in the msbuild file...
<Target Name="CopyCustomContent" AfterTargets="AfterBuild">
<Copy SourceFiles="app.config" DestinationFiles="$(OutDir)\testhost.dll.config" />
</Target>
This makes it easier to verify existing tests under .NET Core before porting the configuration data over to json configuration files.
Edit
If running under Resharper, the previous answer doesn't work as Resharper proxies the assembly, so you need
<Target Name="CopyCustomContent" AfterTargets="AfterBuild">
<Copy SourceFiles="app.config" DestinationFiles="$(OutDir)\ReSharperTestRunner64.dll.config" />
</Target>
App.config in Test Projects
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:
- right click on the solution name in Visual Studio
- select "Add Existing Item"
- navigate to the ProjectA App.config and select it
- click in the "Add" button down arrow
- 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.
app.config not beeing loaded in .Net Core MSTests project
When you execute the tests, the entry assembly is not an assembly with your tests. You can check it by adding following line to your test and debugging it:
var configLocation = Assembly.GetEntryAssembly().Location;
In my case configLocation
was c:\Users\myusername\.nuget\packages\microsoft.testplatform.testhost\15.3.0-preview-20170628-02\lib\netstandard1.5\testhost.dll
So ConfigurationManager
expects to find app.config
at testhost.dll.config
in specified directory. I've copied it to this location and the test passed ok (after slight modification of the config, see below).
Another problem is that your app.config is not fully correct. configSections
element should be the first in <configuration>
root. So just remove configSections
element as it's empty or adjust your app.config
in the following way:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
</configSections>
<appSettings>
<add key="TestKey" value="20" />
</appSettings>
</configuration>
Of course, it's a bad way to place config file near testhost.dll
. You could change the path from which ConfigurationManager
loads application config with ConfigurationManager.OpenExeConfiguration
call:
[TestMethod]
public void UnitTest1()
{
// Put your Test assembly name here
Configuration configuration = ConfigurationManager.OpenExeConfiguration(@"SimpleTestsUnits.dll");
Assert.AreEqual("20", configuration.AppSettings.Settings["TestKey"].Value);
}
But unfortunately this approach requires modification of your code under test.
Access App.Config Settings from Class Library Called through Unit Test Project
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.
Related Topics
No Console Output When Using Allocconsole and Target Architecture X86
What's the Best Strategy for Equals and Gethashcode
Query Extremely Slow in Code But Fast in Ssms
How to Change the Background Color of All Other Forms from One Form
Convert Bitmaps to One Multipage Tiff Image in .Net 2.0
Detecting Network Connection Speed and Bandwidth Usage in C#
Could Not Find an Implementation of the Query Pattern
Compare Nullable Types in Linq to SQL
How to Delete a File Which Is Locked by Another Process in C#
Windows 7 and Vista Uac - Programmatically Requesting Elevation in C#
Why Isn't Arraylist Marked [Obsolete]
Getting Attribute Value of an Xml Document Using C#
How to Initialize a List of Strings (List<String>) with Many String Values
Does Using Tasks (Tpl) Library Make an Application Multithreaded