You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ConfigurationManager doesn't find config file with "dotnet test" #22720
Reacted by tvoisin, Alban Dericbourg, Jules Bovet, Arnaud PERIN, Eben, Alex Zaytsev, Frédéric Delaporte, Evgeny Morozov, Alexandru Mărculescu, James Crowley and 4 more
The app.config in effect is testhost.dll.config, not MyTests.dll.config which @gboucher90 needs.
With .NET Framework the NUnit adapter can fix this by running the test assembly in a new AppDomain with AppDomainSetup.ConfigurationFile set to MyTests.dll.config, but what's the equivalent for .NET Core? If there is no equivalent, what can @gboucher90 do to work around this?
I still don't understand the problem / ask here: Is there need to share the app.config between normal execution and NUnit execution? Is that the underlying problem?
Who named it 'MyTests.dl.config"? That does not look like "normal execution name". Can it be renamed?
@karelz The author wants to load his app.config while testing, for any of a number of possible reasons. Maybe that's the only way to initialize a component needed for the test, or maybe there are settings that need to be validated. In any case, consider a test project named MyTests. When you build, MyTests.dll and MyTests.dll.config appear in bin\Debug.
When you run VSTest, the entry assembly is testhost.dll. ConfigurationManager knows nothing about the NUnit VSTest adapter or VSTest itself and therefore it tries to load the entry assembly's config as usual and loads testhost.dll.config from the folder which contains testhost.dll rather than loading MyTests.dll.config from the folder which contains MyTests.dll.
That means the test will fail because the configuration it directly or indirectly relies on is unavailable.
I have many tests (mainly integration ones) which relies on App.config data. Using .Net Framework NUnit is able to create a new app domain and specify the entry point. The later is used by System.Configuration.ConfigurationManager to find the correct App.config file.
This is not possible anymore and the code under test fails to find the configuration.
Can your test projects copy the app.config into the "correct one"?
Can you open config file explicitly using ConfigurationManager.OpenExeConfiguration in your app?
We're facing this issue too and it's a blocker for our .net core migration. The easiest way to check is to have this inside your test code
var myConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).FilePath;
And you'll see that instead of referencing C:\myapp\bin\Debug\netcoreapp2.0\myapp.dll.config it references C:\myapp\bin\Debug\netcoreapp2.0\testhost.dll.config
What is the intended workaround? What about a patched release?
Reacted by Sid Shaytay, Guillaume Boucher, Evgeny Morozov, Thomas Galliker, Rob Spoor, Onur Celebi and madelson
+1 ... also seeing this, following for a fix date.
EDIT: Our temporary workaround involved adding this to the test project's *.csproj file which copies the app.config (you should already have this) to testhost.dll.config
The intended workaround for this sort of scenario is that you load data from a specific path. ConfigurationManager.OpenMappedExeConfiguration() will let you open any configuration file you want. Is there a reason that won't work for you?
@JeremyKuhne User level code changes to address behavioral differences between .NET framework + nunit vs .NET core + nunit smells like a kludge. Same concern but to a lesser extent on changes to the build system to account for those differences .NET F/W vs .NET Core differences.
I'm new to how vstest and testhost interplay but if they are inserting themselves in the middle, shouldn't they be responsible for maintaining transparency about this abstraction from a user code perspective? Perhaps testhost can itself call ConfigurationManager.OpenMappedExeConfiguration() to fix this ?
Closing. As stated, the recommendation here is to explicitly construct a Configuration object via OpenMappedExeConfiguration(). One can copy into testhost.dll.config as mentioned if in a pinch.
I would like to see this issue solved in a batter way. One of the main goals of integration testing is to insure that the Data Access Layer is working properly. And we all use the appsettings.json to store the connection string.
Then how can I test my business logic residing in Data Access Layer?
Just noting that this is a significant block to our .NET Core migration too - yes, we can rewrite and refactor, but it's essentially another breaking change to deal with.
I've tried the testhost.dll.config hack but as far as I can see that has to go to a path outside the whole project directory (C:\Users\XXX.nuget\packages\microsoft.testplatform.testhost\15.3.0\lib\netstandard1.5\testhost.dll) in my case, rather than the OutDir as suggested by @SidShetye (unless I'm missing something?) - which turns a cludge into something I wouldn't want to rely on at all?
Just give this one a try, it worked for me: var config = ConfigurationManager.OpenExeConfiguration(".nuget/packages/microsoft.testplatform.testhost/15.0.0/lib/netstandard1.5/testhost.dll");
+1 ... also seeing this, following for a fix date.
EDIT: Our temporary workaround involved adding this to the test project's *.csproj file which copies the app.config (you should already have this) to testhost.dll.config
I was noticing that there were some issues with the .NET Core 3.1 SDK not being found after the VS Preview install, and I had to install the x86 SDK manually. I'm not sure how all that got changed, but it would be nice if the runtime was using the 64-bit SDK again the way it's supposed to be.
Here's a snippet that should work for both x86 and x64 cases.
Place this in your unit test global setup:
#if NETCOREAPPusingSystem.Configuration;usingSystem.IO;usingSystem.Reflection;
#endif
...// In your global setup:
#if NETCOREAPPstringconfigFile=$"{Assembly.GetExecutingAssembly().Location}.config";stringoutputConfigFile=ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).FilePath;File.Copy(configFile,outputConfigFile,true);
#endif
Reacted by James White
ghost locked as resolved and limited conversation to collaborators on Dec 21, 2020
Activity
[-]ConfigurationManager doesn't find confg file with "dotnet test"[/-][+]ConfigurationManager doesn't find config file with "dotnet test"[/+]gboucher90 commentedon Jul 11, 2017
Should we override the entry assembly in NUnit Test Adapter?
https://github.com/NeutroniumCore/Neutronium/blob/master/Tests/Infra/Tests.Infra.HTMLEngineTesterHelper/Window/AssemblyHelper.cs
jnm2 commentedon Feb 27, 2018
The app.config in effect is testhost.dll.config, not MyTests.dll.config which @gboucher90 needs.
With .NET Framework the NUnit adapter can fix this by running the test assembly in a new AppDomain with AppDomainSetup.ConfigurationFile set to MyTests.dll.config, but what's the equivalent for .NET Core? If there is no equivalent, what can @gboucher90 do to work around this?
karelz commentedon Feb 27, 2018
I still don't understand the problem / ask here: Is there need to share the app.config between normal execution and NUnit execution? Is that the underlying problem?
Who named it 'MyTests.dl.config"? That does not look like "normal execution name". Can it be renamed?
jnm2 commentedon Feb 27, 2018
@karelz The author wants to load his app.config while testing, for any of a number of possible reasons. Maybe that's the only way to initialize a component needed for the test, or maybe there are settings that need to be validated. In any case, consider a test project named MyTests. When you build,
MyTests.dllandMyTests.dll.configappear inbin\Debug.When you run VSTest, the entry assembly is
testhost.dll.ConfigurationManagerknows nothing about the NUnit VSTest adapter or VSTest itself and therefore it tries to load the entry assembly's config as usual and loadstesthost.dll.configfrom the folder which containstesthost.dllrather than loadingMyTests.dll.configfrom the folder which containsMyTests.dll.That means the test will fail because the configuration it directly or indirectly relies on is unavailable.
gboucher90 commentedon Feb 27, 2018
Yes it is exactly what @jnm2 explained.
I have many tests (mainly integration ones) which relies on App.config data. Using .Net Framework NUnit is able to create a new app domain and specify the entry point. The later is used by System.Configuration.ConfigurationManager to find the correct App.config file.
This is not possible anymore and the code under test fails to find the configuration.
karelz commentedon Feb 28, 2018
Can your test projects copy the app.config into the "correct one"?
Can you open config file explicitly using
ConfigurationManager.OpenExeConfigurationin your app?jnm2 commentedon Feb 28, 2018
I don't think so. This would mean overwriting the testhost.exe.config that ships with VSTest in the dotnet sdk path.
karelz commentedon Feb 28, 2018
I would expect the entry assembly to be in the output directory (maybe copied from somewhere). Is that not the case?
jnm2 commentedon Feb 28, 2018
@karelz I haven't observed the copying of a test host. This project does not copy a test host to the output directory:
It copies
MyTests.dllandNUnit3.TestAdapter.dllwith its dependenciesMono.Cecil.dllandnunit.engine.netstandard.dll.Running from Test Explorer
Environment.CommandLine:
C:\Users\Joseph\.nuget\packages\microsoft.testplatform.testhost\15.5.0\lib\netstandard1.5\testhost.dll --port 59436 --endpoint 127.0.0.1:059436 --role client --parentprocessid 20316 --telemetryoptedin trueAssembly.GetEntryAssembly().Location:
C:\Users\Joseph\.nuget\packages\microsoft.testplatform.testhost\15.5.0\lib\netstandard1.5\testhost.dllRunning via
dotnet testEnvironment.CommandLine:
C:\Users\Joseph\.nuget\packages\microsoft.testplatform.testhost\15.5.0\lib\netstandard1.5\testhost.dll --port 59496 --endpoint 127.0.0.1:059496 --role client --parentprocessid 11844 --telemetryoptedin falseAssembly.GetEntryAssembly().Location:
C:\Users\Joseph\.nuget\packages\microsoft.testplatform.testhost\15.5.0\lib\netstandard1.5\testhost.dlldanmoseley commentedon Feb 28, 2018
@JeremyKuhne thoughts on this?
okwolf commentedon Mar 3, 2018
We're facing this issue too and it's a blocker for our .net core migration. The easiest way to check is to have this inside your test code
And you'll see that instead of referencing
C:\myapp\bin\Debug\netcoreapp2.0\myapp.dll.configit referencesC:\myapp\bin\Debug\netcoreapp2.0\testhost.dll.configWhat is the intended workaround? What about a patched release?
SidShaytay commentedon Mar 7, 2018
+1 ... also seeing this, following for a fix date.
EDIT: Our temporary workaround involved adding this to the test project's
*.csprojfile which copies theapp.config(you should already have this) totesthost.dll.configJeremyKuhne commentedon Mar 7, 2018
The intended workaround for this sort of scenario is that you load data from a specific path. ConfigurationManager.OpenMappedExeConfiguration() will let you open any configuration file you want. Is there a reason that won't work for you?
SidShaytay commentedon Mar 7, 2018
@JeremyKuhne User level code changes to address behavioral differences between .NET framework + nunit vs .NET core + nunit smells like a kludge. Same concern but to a lesser extent on changes to the build system to account for those differences .NET F/W vs .NET Core differences.
I'm new to how vstest and testhost interplay but if they are inserting themselves in the middle, shouldn't they be responsible for maintaining transparency about this abstraction from a user code perspective? Perhaps
testhostcan itself callConfigurationManager.OpenMappedExeConfiguration()to fix this ?7 remaining items
JeremyKuhne commentedon Mar 19, 2018
Closing. As stated, the recommendation here is to explicitly construct a
Configurationobject viaOpenMappedExeConfiguration(). One can copy intotesthost.dll.configas mentioned if in a pinch.obayit commentedon Jun 27, 2018
I would like to see this issue solved in a batter way. One of the main goals of integration testing is to insure that the Data Access Layer is working properly. And we all use the appsettings.json to store the connection string.
Then how can I test my business logic residing in Data Access Layer?
jamescrowley commentedon Jul 19, 2018
Just noting that this is a significant block to our .NET Core migration too - yes, we can rewrite and refactor, but it's essentially another breaking change to deal with.
I've tried the testhost.dll.config hack but as far as I can see that has to go to a path outside the whole project directory (C:\Users\XXX.nuget\packages\microsoft.testplatform.testhost\15.3.0\lib\netstandard1.5\testhost.dll) in my case, rather than the OutDir as suggested by @SidShetye (unless I'm missing something?) - which turns a cludge into something I wouldn't want to rely on at all?
keyrad-abasi commentedon Aug 8, 2018
Just give this one a try, it worked for me:
var config = ConfigurationManager.OpenExeConfiguration(".nuget/packages/microsoft.testplatform.testhost/15.0.0/lib/netstandard1.5/testhost.dll");danmoseley commentedon Sep 4, 2018
https://github.com/dotnet/corefx/issues/32095 created to find another solution.
Ignore Mapping_from_the_default_file_path on .NET Core
Ignore Mapping_from_the_default_file_path on .NET Core
Ignore Mapping_from_the_default_file_path on .NET Core
nsmithdev commentedon Dec 10, 2019
With dotnet 3.1 I'm having to reference
testhost.x86.dll.configThe statement below was very helpful for verifying the config path used
robertmclaws commentedon Dec 22, 2019
I was noticing that there were some issues with the .NET Core 3.1 SDK not being found after the VS Preview install, and I had to install the x86 SDK manually. I'm not sure how all that got changed, but it would be nice if the runtime was using the 64-bit SDK again the way it's supposed to be.
Daniel-Khodabakhsh commentedon Apr 29, 2020
Here's a snippet that should work for both x86 and x64 cases.
Place this in your unit test global setup: