0

I have an integration test using XUnit that accesses the database. We need to be able to get the database connection string from the app.config file.

When running the test from the IDE it works beautifully and the connection string is found. When I run the test automatically from a cake.build the ConfigurationManager.ConnectionStrings["blahblah"] ret

var connectionStringSettings = ConfigurationManager.ConnectionStrings["FlexConnString"];
if (connectionStringSettings == null)
    {
        Console.WriteLine("ConfigManager does not return a setting for FlexConnString");
        _connectionString = "No Connection string";
    } 

The Cake Task is

Task("Run-Integration-Tests")
.IsDependentOn("Run-Unit-Tests")
.Does(() =>
{
    var testDir = "./artifacts/_tests/**/*.IntegrationTests.dll";
    Information("Start Running Integration Tests in " + testDir);
    XUnit2(testDir,
        new XUnit2Settings {
            Parallelism = ParallelismOption.All,
            HtmlReport = true,
            NoAppDomain = true,
            NUnitReport = true,
            XmlReport = true,
            ReportName = "MixTdiIntegrationTestResults",
            OutputDirectory = "./artifacts"
       });
});

This outputs ConfigManager does not return a setting for FlexConnString' when running the build.cake from powershell.

I'm not sure if this is an XUnit issue or a Cake issue.

Nkosi
  • 235,767
  • 35
  • 427
  • 472
GloryDev
  • 670
  • 5
  • 8
  • 1
    If you launch cake with diagnostic verbosity you'll get more logging and the command line used to launch xunit. You can read more about in in the below question https://stackoverflow.com/questions/38658660/how-to-enable-diagnostic-verbosity-for-cake – devlead Jun 01 '17 at 12:03

1 Answers1

2

when you are running Xunit test it looks for config file in the current working directory, you can easily check it

Information("Current Directory: {0}", System.IO.Directory.GetCurrentDirectory());

and can't find it, you need to specify WorkingDirectory in the XUnit2Settings to folder with connection string config file

XUnit2(testDir,
    new XUnit2Settings {
        Parallelism = ParallelismOption.All,
        HtmlReport = true,
        NoAppDomain = true,
        NUnitReport = true,
        XmlReport = true,
        ReportName = "MixTdiIntegrationTestResults",
        OutputDirectory = "./artifacts",
        WorkingDirectory = "[your config file path]"
   });

Need to mention another option is to change your current working directory

Environment.CurrentDirectory = "c:/";
makison
  • 373
  • 2
  • 10
  • this didnt work. the config file is in the output directory, the `Environment.Currentdirectory` reports the correct folder and the working directory in `XUnit2Settings` is correct – John Nicholas Jun 05 '18 at 08:10
  • fix for me was to set ShadowCopy to false (its always bloody shadow copy!) `ShadowCopy = false` – John Nicholas Jun 05 '18 at 08:16