在使用 xunit 和 .net core 3.1 进行单元测试时获取记录器

最大跨度

在输出窗口中运行单元测试时,我试图从记录器获取输出,但没有显示任何内容。我在测试类中使用 ILoggingFactory。我期待从测试类和实际实现类(即 JobQueueManager)中看到我的所有日​​志。

测试文件

public class ScheduledJobDatesTest :IClassFixture<ScheduleJobTestFixture>
    {
        private ScheduleJobTestFixture ScheduleJobFixtureHelper { get; }

        Mock<IDbConnection> mockJobQueueManager = new Mock<IDbConnection>();
        ILoggerFactory logger = new LoggerFactory();

        public ScheduledJobDatesTest(ScheduleJobTestFixture scheduleJobFixture)
        {
            ScheduleJobFixtureHelper = scheduleJobFixture;

            mockJobQueueManager.SetReturnsDefault(new SqlConnection());
            logger = LoggerFactory.Create(a => a.AddConsole());
            logger.CreateLogger("Executing Test");
        }

        [Fact]
        public void ValidScheduledJobDateForNextWeeklyAppointment()
        {
            Debug.WriteLine("Testing");
            logger.CreateLogger("Test").LogInformation("Test Information");
            //Arrange
            JobQueueManager jobQueueManager = new JobQueueManager(mockJobQueueManager.Object, logger.CreateLogger<JobQueueManager>());
            var scheduledJob = ScheduleJobFixtureHelper.GetMockedScheduledJob(JobType.Week);
            //Act
            bool isTrue = jobQueueManager.IsScheduledJobDateValidForJobType(scheduledJob.JobDate);
            //Assert
            Assert.True(isTrue);
        }

}

作业队列管理器

public class JobQueueManager : IJobQueueManager
    {
        public IDbConnection DBConnection { get; }
        public ILogger<JobQueueManager> Logger { get; }

        public JobQueueManager(IDbConnection dbConnection, ILogger<JobQueueManager> logger)
        {
            DBConnection = dbConnection;
            Logger = logger;
            Logger.LogInformation("Initialized Job Queue Manager");
        }

    public bool IsScheduledJobDateValidForJobType(DateTime ScheduledJobDate)
        {
            Logger.LogDebug("Verifying  unit test");
            var currentDate = DateTime.Now;
            var nextJobDate = ScheduledJobDate;
            if (nextJobDate > currentDate)
            {
                var appointmentsDaysDifference = Math.Ceiling((nextJobDate - currentDate).TotalDays);
                if(appointmentsDaysDifference == 1)
                return true;
            }
            return false;
        }
}
格维利

您使用的是什么版本的 xUnit?

如果您使用 xUnit.net 1.x,您可能以前一直在将输出写入控制台、调试或跟踪。当 xUnit.net v2 默认开启并行化时,这种输出捕获机制不再适用;不可能知道可以并行运行的许多测试中的哪一个负责写入这些共享资源。将代码从 v1.x 移植到 v2.x 的用户应改用这两种新方法之一。

来源

似乎有很多方法可以解决这个问题,如果是我,我会考虑实施自定义 provider

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在.NET Core和Visual Studio Code中调试xUnit测试

XUnit和ASP.NET Core 1.0的依赖注入

使用.NET Core和xUnit同时针对多个框架的单元测试代码

使用xUnit测试.Net Core库

使用Moq .net Core进行单元测试文件上传

使用xUnit和asp.net核心测试我的服务

使用.NET Core使用xunit生成结果报告测试

使用JWT声明对.NET Core Web API控制器进行单元测试

.net Core 2.0 ConfigureLogging xunit测试

.NET Core 2.0和xUnit无法运行

.NET CORE MVC ViewComponent xUnit测试

如何为使用AutoMapper和依赖注入的.net core 2.0服务编写xUnit测试?

xUnit测试和.NET Core 2.0中的Automapper

在Asp.net Core中对IFormFile字段进行xunit测试

使用Xunit测试ASP.NET Core 2.0项目如何测试httpget

使用Moq和xUnit对服务进行单元测试

如何模拟ExceptionContext以使用.NET Core 3 Web API和Moq进行测试

如何使用Dapper.Contrib在.NET Core中使用xUnit编写测试?

测试枚举和字符串-.NET Core 3.1和xUnit

测试Logger的输出-xUnit .NET Core 3.1

Net Core:使用Xunit对工作单元模式和DBContext执行依赖注入

.Net Core XUnit-未使用“ dotnet测试”指定测试源文件

使用 xunit 和 moq 进行 net core api 控制器单元测试

使用 XUnit 和 AspNetCore.TestHost 测试 .net core web api 控制器文件上传

如何在 Azure DevOps 管道中使用 ASP.NET Core 依赖项运行 XUnit 测试?

使用 xUnit 和 FakeItEasy 对 Azure 函数 v3 进行单元测试

如何在 .NET Core 的 xUnit 测试中使用 NLog?

使用 xUnit 和 Moq 在 .NET 5 中对 FluentEmail 进行单元测试

使所有 EF Core 模型属性虚拟化,以便在使用 Moq 和 xUnit 的单元测试中进行模拟?