简体   繁体   中英

Can't read app.config appSettings from unit test in JetBrains Rider

I am using ConfigurationManager.AppSettings["someKey"] to retrieve values from app.config appSettings

 <appSettings>
    <add key="someKey" value="value" />
    ...

However, when I use this in my unit tests, the ConfigurationManager.AppSettings collection is empty.

This happens only when I run the tests from JetBrains Rider IDE!
Running them from Visual Studio (even with ReSharper runner) works perfectly fine and values are loaded.

It is a .Net 5 project using XUnit framework.

What is going on with Rider here?

  1. First make sure the app.config is in the correct format. since you want to reference ConfigurationManager.AppSettings["someKey"]

the app.config should be structed this way

<configuration>
    <appSettings>
        <add key="someKey" value="value" />
    </appSettings>
</configuration>
  1. copy the app.config to the project-folder\bin\Debug\net5.0 and rename in a way that is understood by the IDE which can be achieved through adding the following lines in project file

    <Target Name="CopyCustomContent" AfterTargets="AfterBuild" >

     <Copy SourceFiles="app.config" DestinationFiles="$(OutDir)/ReSharperTestRunner.dll.config" /> <Target/>

Hope this helps!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM