c# - 如何在 .net Core 中将日志从 TestServerFixture 重定向到 ITestOutputHelper?
问题描述
我目前在 VSTS 中的构建机器上的测试失败了,真的很难知道发生了什么。我有很多由 API 生成的日志,这些日志可能真的很有帮助。
我的目的是将我的 API 中生成的所有日志重定向到 OutputTestHelper。
使用 Xunit,我有一个 TestServerFixture:
public class TestServerFixture : IDisposable
{
public TestServer Server { get; }
public HttpClient Client { get; }
public T GetService<T>() => (T)Server.Host.Services.GetService(typeof(T));
public TestServerFixture()
{
var builder = new WebHostBuilder()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseEnvironment("Development")
.ConfigureAppConfiguration((hostingContext, config) =>
{
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables();
})
.UseStartup<TestStartup>()
.ConfigureLogging((hostingContext, logging) =>
{
logging.AddXunit(output); //How Can I get ITestOuputHelper?
});
Server = new TestServer(builder);
Client = Server.CreateClient();
}
public void Dispose()
{
Server.Dispose();
Client.Dispose();
}
}
读这个:
我有一个扩展允许我将 Xunit ITestOutputHelper 添加到需要ITestOutputHelper 作为参数的记录器( AddXunit() )。
问题是,我找不到在 TestServerFixture 中获取它的方法。我试过了:
也许在 TestServerFixture 的构造函数中有一些可用的 Injection 可以实例化 ITestOutputHelper ?
public TestServerFixture(ITestOutputHelper output)
=> 失败也许有办法从 IloggingBuilder.Services theITestOutputHelper (like
loggingBuilder.Services.Resolve<ITestOutputHelper>()
) => FAILED中解决也许我可以从TestStartup 中的Configure
public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory, ITestOutputHelper output)
方法中得到它(比如)=> FAILED
您知道一种配置 TestServerFixture 以将所有日志重定向到 OutpuTestHelper 的方法吗?
解决方案
我终于找到了解决方案。由于无法从 Injection 访问 ITestOutpuHelper,因此我没有其他方法可以为每个 Test 类创建一个新的 TestServerFixture。
我之前拥有的:
public class Users_Tests
{
[...]
public Users_Tests(
TestServerFixture server
ITestOutputHelper output
)
{
[...]
}
}
我现在拥有的:
public class Users_Tests
{
[...]
public Users_Tests(
ITestOutputHelper output
)
{
TestServerFixture = new TestServerFixture(output);
[...]
}
}
所以我的 TestServiceFixture 现在是:
public class TestServerFixture : IDisposable
{
public TestServer Server { get; }
public HttpClient Client { get; }
public T GetService<T>() => (T)Server.Host.Services.GetService(typeof(T));
public TestServerFixture(ITestOutputHelper output)
{
var builder = new WebHostBuilder()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseEnvironment("Development")
.ConfigureAppConfiguration((hostingContext, config) =>
{
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables();
})
.UseStartup<TestStartup>()
.ConfigureLogging((hostingContext, logging) =>
{
logging.AddXunit(output);
});
Server = new TestServer(builder);
Client = Server.CreateClient();
}
public void Dispose()
{
Server.Dispose();
Client.Dispose();
}
}
所以现在所有日志都被重定向到输出,这有助于我了解错误来自哪里!
推荐阅读
- mysql - 如何使用外键制作复合键?
- json - 未处理的异常:NoSuchMethodError:在 null 上调用了方法“[]”(json 解析)
- c# - 如何使用从 razor 视图插入到 viewbag 中的 linq join 函数结果?
- google-sheets - 我想在谷歌表格中添加整行的单元格值
- xamarin - Xamarin MVVM 双击执行命令两次,IsBusy 不起作用
- javascript - 反应原生:有办法使用 axios.put 吗?
- sql - 对另一列的 count(value_1) / count(value_2) 大于 x 的列求和的有效方法
- reactjs - 让 Nextjs 忽略 babel.config.js
- java - 在根 JavaFX TreeView 的子项上禁用上下文菜单
- amazon-web-services - 从 DynamoDB 中清除/删除多个项目