1

I'm trying to run a test method from a .net core unit test project over a .net standard project and when loading config file which is in the test project (as this is the current executing assembly). I'm getting a wrong config file with the current file path "C:\Users\xxx\.nuget\packages\microsoft.testplatform.testhost\15.3.0-preview-20170628-02\lib\netstandard1.5\testhost.dll.config"

 var conf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
Joe B
  • 738
  • 7
  • 28
  • Why do you think that's the wrong config? What is the execution path of your unit test? There's a reason .NET Core *doesn't use app.config at all*. You should *inject* the configuration, not hard-code its loader – Panagiotis Kanavos Nov 08 '17 at 15:04
  • oh! got what you say. first time I'm using .net core for unit testing I didn't think of it that the executing host path is different then the original. how do I inject? – Joe B Nov 08 '17 at 15:08

2 Answers2

1

You have the option to point to another config file. The credit for the source goes to the Ron Hagerman for his answer with only slight modifications. The following xunit test may help with understanding on how to set the location. It is frustrating that .net core unit tests looks for the connection string in the testhost.dll.config file in the following directory

C:\Users\[UserName]\.nuget\packages\microsoft.testplatform.testhost\[version]\lib\netstandard1.5\testhost.dll.config

[Fact]
public void AccessAppSettings_ConnectionString()
{
     //obtain the current directory for the executable
     Uri UriAssemblyFolder = new Uri(System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly()
                                           .GetName().CodeBase));
     string appPath = UriAssemblyFolder.LocalPath;
     //Change the "DataProvider.Tests.dll" to whatever your 
     //library or executable name. 
     //Note: Configuration manager will add the .config extension
     Configuration config = ConfigurationManager
            .OpenExeConfiguration(appPath + @"\" + "DataProvider.Tests.dll");

     ConnectionStringsSection section = 
                  config.GetSection("connectionStrings") as ConnectionStringsSection;

     string expectedString = $"Data Source=mysqliteDBName.sqlite3";
     //Change "mySqliteConnectionString" to your connection string name   
     var sut_connectionString = 
                 section.ConnectionStrings ["mySqliteConnectionString"].ConnectionString;

      Assert.NotNull(sut_connectionString);
      Assert.Contains(expectedString, sut_connectionString);

Sample Config file:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <connectionStrings>
        <add name="mySqliteConnectionString" connectionString="Data Source=mysqliteDBName.sqlite3" />
        <add name="otherConnectionString" connectionString="Data Source=othersqliteDBName.sqlite3" />
    </connectionStrings>
</configuration>
fradsham
  • 131
  • 2
  • 6
0

I don't think you are getting the wrong one. You are getting the Test project's config file which is the correct one since you are running the Test project.

Every project has it's own config file or none. It cannot just go and load another project's config file.

Bottom line is: just copy whatever specific config you want from your .net standard project's config file to your unit test's config file.

CubanTurin
  • 177
  • 1
  • 10
  • my App.config is in the test project look at the path I posted it the path from the nugget folder – Joe B Nov 08 '17 at 15:10