首页 > 解决方案 > 如何在 .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 以将所有日志重定向到 OutpuTestHelper 的方法吗?

标签: c#.net-corexunit.net

解决方案


我终于找到了解决方案。由于无法从 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();
    }
}

所以现在所有日志都被重定向到输出,这有助于我了解错误来自哪里!


推荐阅读