我使用XUnit进行集成测试,访问数据库。我们需要能够从app.config文件中获取数据库连接字符串。
从IDE运行测试时,它可以很好地工作,并找到连接字符串。 当我从cake.build自动运行测试时,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";
}
Cake任务
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"
});
});
当从powershell运行build.cake时,此输出ConfigManager不会返回FlexConnString的设置。
我不确定这是XUnit问题还是Cake问题。
答案 0 :(得分:2)
当你运行Xunit测试时,它在当前工作目录中查找配置文件,你可以轻松检查它
Information("Current Directory: {0}", System.IO.Directory.GetCurrentDirectory());
并且找不到它,您需要在WorkingDirectory到指定连接字符串配置文件的文件夹中指定XUnit2Settings
XUnit2(testDir,
new XUnit2Settings {
Parallelism = ParallelismOption.All,
HtmlReport = true,
NoAppDomain = true,
NUnitReport = true,
XmlReport = true,
ReportName = "MixTdiIntegrationTestResults",
OutputDirectory = "./artifacts",
WorkingDirectory = "[your config file path]"
});
需要提及的另一个选择是更改当前的工作目录
Environment.CurrentDirectory = "c:/";