C# Xunit的App.config
我正在为一些助手类编写一些测试,这些类依赖于一些配置设置,通常存储在执行项目的App.config或Web.config中 配置如下所示:C# Xunit的App.config,c#,xunit.net,C#,Xunit.net,我正在为一些助手类编写一些测试,这些类依赖于一些配置设置,通常存储在执行项目的App.config或Web.config中 配置如下所示: <?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="FileNamePattern" value="\\d{8}_\\w{4:20}\.png"/> <!-- and the l
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="FileNamePattern" value="\\d{8}_\\w{4:20}\.png"/>
<!-- and the likes -->
</appSettings>
</configuration>
我正在使用GUI运行程序(xUnit.GUI.clr4.exe)和xUnit控制台运行程序(在Jenkins CI服务器上)运行xUnit 1.9。目前,我可以通过手动设置xunit.gui.clr4.exe.config和xunit.console.exe.config文件将这些配置值“注入”到测试环境中;然而,这是乏味和容易出错的
我还可以在夹具中模拟这些配置设置。但是在10个不同的文件中使用相同的夹具是相当重复的
有没有更好的方法可以用xUnit模拟这些配置设置,例如为测试项目提供App.config文件?如果您的代码假定它们位于
App.config
中,那么xUnit.net通过提供一个来支持将它们连接到那里(通常,当测试位于DLL文件中时,这意味着您将在项目输出中获得一个AssemblyName.DLL.config
文件,如果在加载时存在该文件,则运行程序将其加载为设置)
显然,首先使用DI原则来删除这些依赖项并没有什么坏处,但我要说的是,在您真正开始测试之前,不要去搞乱代码
为了保持干燥,请将app.config放在中间位置,并将其作为链接添加(通过对话框中打开按钮上的箭头)。(是的,有很多不喜欢的地方-只有在您觉得这是最不邪恶的方法时才使用。)
需要注意的一点是,更改不会在GUI runner中重新加载,除非您要求重新加载程序集。从更复杂的项目和团队工作的角度来看,我建议:
public class MyTest : IDisposable
{
public IServiceProvider Services { get; private set; }
public MyProjectOptions Options { get; private set; }
public Logger Logger { get; private set; }
private void Configure()
{
// appsettings.workspace.json for custom developer configuration
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.AddJsonFile("appsettings.workspace.json", optional: true)
.Build();
Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.LiterateConsole()
.WriteTo.RollingFile("logs/{Date}-log.txt")
.CreateLogger();
Options = configuration.GetSection("MyProject").Get<MyProjectOptions>();
var services = new ServiceCollection();
services.AddSingleton<ILogger>(s => Logger);
// other DI logic and initializations ...
//services.AddTransient(x => ...);
Services = services.BuildServiceProvider();
}
public MyTest()
{
Configure();
// ... initialize data in the test database ...
var data = Services.GetService<TestDataService>();
data.Clean();
data.SeedData();
}
public void Dispose()
{
// ... clean-up data in the test database ...
var data = Services.GetService<TestDataService>();
data.Clean();
}
}
公共类MyTest:IDisposable
{
公共IServiceProvider服务{get;private set;}
公共MyProjectOptions选项{get;private set;}
公共记录器{get;private set;}
私有void Configure()
{
//用于自定义开发人员配置的appsettings.workspace.json
var configuration=new ConfigurationBuilder()
.SetBasePath(目录.GetCurrentDirectory())
.AddJsonFile(“appsettings.json”)
.AddJsonFile(“appsettings.workspace.json”,可选:true)
.Build();
Logger=新的LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.LiterateConsole()文件
.WriteTo.RollingFile(“logs/{Date}-log.txt”)
.CreateLogger();
Options=configuration.GetSection(“MyProject”).Get();
var services=newservicecolection();
services.AddSingleton(s=>Logger);
//其他DI逻辑和初始化。。。
//services.AddTransient(x=>…);
Services=Services.BuildServiceProvider();
}
公共MyTest()
{
Configure();
//…初始化测试数据库中的数据。。。
var data=Services.GetService();
data.Clean();
data.SeedData();
}
公共空间处置()
{
//…清除测试数据库中的数据。。。
var data=Services.GetService();
data.Clean();
}
}
我会创建一个中间ISettings。您可以将app.config加载到其中。然后,您需要做的就是使用类似Moq的框架模拟接口。我非常喜欢尽可能地抽象配置文件。当我将配置重命名为TestNamespace.dll.config并将其设置为始终复制到构建目录时,配置工作正常tory.谢谢!@ThachMai如果将其保留为app.config不会自动工作,我会感到惊讶-我个人一直怀疑它,但我从未发现生成系统实际上没有进行复制/重命名的情况-打开msbuild日志并查看一下(请记住,在重新加载之前不会提取它)。我会尝试一下,下周二访问源代码后再与您联系。事实上,App.config会自动重命名为TestNamespace.dll.config。太棒了:)