Maarty
Maarty

Reputation: 1190

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?

Upvotes: 2

Views: 1100

Answers (1)

 Jagadeesh Reddy
Jagadeesh Reddy

Reputation: 23

  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. Add the following lines in project file so that it copies the app.config to be findable by the Resharper test runner. VS may be doing this itself, however Rider seems to need it done explicitly

     <Target Name="CopyCustomContent" AfterTargets="AfterBuild" \>
         <Copy SourceFiles="app.config" DestinationFiles="$(OutDir)/ReSharperTestRunner.dll.config" />
     <Target/>
    

Hope this helps!!

Upvotes: 2

Related Questions