如何解决 XUnit 测试因 "Run All" 下的 NLog 目标中的消息断言而失败的情况
How to troubleshoot the situation where XUnit tests fail with assertion of messages in NLog target under "Run All"
这是我的环境:
- Visual Studio 2017
- 项目的.NET运行时间版本是4.6.2
- XUnit 版本 2.3.1
- NLog 版本 4.4.12
- 流畅断言 4.19.4
这是问题所在:
当我 运行 测试单独通过时,但是当我 运行 通过测试资源管理器中的 "Run All" 按钮时,我失败了,当重复 运行随后失败的任务,最终全部通过。还想指出的是,我并不是 运行 并行测试。测试的性质使得被测代码发出最终在自定义 NLog 目标中结束的日志信息。这是一个示例程序,可以 运行 以重现问题。
using FluentAssertions;
using NLog;
using NLog.Common;
using NLog.Config;
using NLog.Targets;
using System;
using System.Collections.Concurrent;
using System.IO;
using Xunit;
namespace LoggingTests
{
[Target("test-target")]
public class TestTarget : TargetWithLayout
{
public ConcurrentBag<string> Messages = new ConcurrentBag<string>();
public TestTarget(string name)
{
Name = name;
}
protected override void Write(LogEventInfo logEvent)
{
Messages.Add(Layout.Render(logEvent));
}
}
class Loggable
{
private Logger _logger;
public Loggable()
{
_logger = LogManager.GetCurrentClassLogger();
}
private void Log(LogLevel level,
Exception exception,
string message,
params object[] parameters)
{
LogEventInfo log_event = new LogEventInfo();
log_event.Level = level;
log_event.Exception = exception;
log_event.Message = message;
log_event.Parameters = parameters;
log_event.LoggerName = _logger.Name;
_logger.Log(log_event);
}
public void Debug(string message)
{
Log(LogLevel.Debug,
null,
message,
null);
}
public void Error(string message)
{
Log(LogLevel.Error,
null,
message,
null);
}
public void Info(string message)
{
Log(LogLevel.Info,
null,
message,
null);
}
public void Fatal(string message)
{
Log(LogLevel.Fatal,
null,
message,
null);
}
}
public class Printer
{
public delegate void Print(string message);
private Print _print_function;
public Printer(Print print_function)
{
_print_function = print_function;
}
public void Run(string message_template,
int number_of_times)
{
for (int i = 0; i < number_of_times; i++)
{
_print_function($"{message_template} - {i}");
}
}
}
public abstract class BaseTest
{
protected string _target_name;
public BaseTest(LogLevel log_level)
{
if (LogManager.Configuration == null)
{
LogManager.Configuration = new LoggingConfiguration();
InternalLogger.LogLevel = LogLevel.Debug;
InternalLogger.LogFile = Path.Combine(Environment.CurrentDirectory,
"nlog_debug.txt");
}
// Register target:
_target_name = GetType().Name;
Target.Register<TestTarget>(_target_name);
// Create Target:
TestTarget t = new TestTarget(_target_name);
t.Layout = "${message}";
// Add Target to configuration:
LogManager.Configuration.AddTarget(_target_name,
t);
// Add a logging rule pertaining to the above target:
LogManager.Configuration.AddRule(log_level,
log_level,
t);
// Because configuration has been modified programatically, we have to reconfigure all loggers:
LogManager.ReconfigExistingLoggers();
}
protected void AssertTargetContains(string message)
{
TestTarget target = (TestTarget)LogManager.Configuration.FindTargetByName(_target_name);
target.Messages.Should().Contain(message);
}
}
public class TestA : BaseTest
{
public TestA() : base(LogLevel.Info)
{
}
[Fact]
public void SomeTest()
{
int number_of_times = 100;
(new Printer((new Loggable()).Info)).Run(GetType().Name,
number_of_times);
for (int i = 0; i < number_of_times; i++)
{
AssertTargetContains($"{GetType().Name} - {i}");
}
}
}
public class TestB : BaseTest
{
public TestB() : base(LogLevel.Debug)
{
}
[Fact]
public void SomeTest()
{
int number_of_times = 100;
(new Printer((new Loggable()).Debug)).Run(GetType().Name,
number_of_times);
for (int i = 0; i < number_of_times; i++)
{
AssertTargetContains($"{GetType().Name} - {i}");
}
}
}
public class TestC : BaseTest
{
public TestC() : base(LogLevel.Error)
{
}
[Fact]
public void SomeTest()
{
int number_of_times = 100;
(new Printer((new Loggable()).Error)).Run(GetType().Name,
number_of_times);
for (int i = 0; i < number_of_times; i++)
{
AssertTargetContains($"{GetType().Name} - {i}");
}
}
}
public class TestD : BaseTest
{
public TestD() : base(LogLevel.Fatal)
{
}
[Fact]
public void SomeTest()
{
int number_of_times = 100;
(new Printer((new Loggable()).Fatal)).Run(GetType().Name,
number_of_times);
for (int i = 0; i < number_of_times; i++)
{
AssertTargetContains($"{GetType().Name} - {i}");
}
}
}
}
上面的测试代码运行更好。在按照消息进行一些较早的故障排除后,似乎我没有调用 LogManager.ReconfigExistingLoggers();
,因为配置是以编程方式创建的(在测试 类 的构造函数中)。这是LogManager
源代码中的注释:
/// Loops through all loggers previously returned by GetLogger.
/// and recalculates their target and filter list. Useful after modifying the configuration programmatically
/// to ensure that all loggers have been properly configured.
之后所有测试 运行 都符合预期,偶尔会出现如下所示的失败:
我现在想知道在我的测试设置中是否还有什么我应该保护的,或者这是一个错误 NLog。任何关于如何修复我的测试设置或对设置进行故障排除的建议都将非常受欢迎。提前致谢。
更新
- 已将
List<LogData>
更改为 ConcurrentBag<LogData>
。然而,这并没有改变问题。问题仍然是消息没有及时到达集合中。
- 重新表述问题并将之前的代码示例替换为实际示例(可以 运行 重现问题)+ 问题的屏幕截图。
- 改进了测试,运行 更好但偶尔会由于来自 NLog 本身的异常而失败(添加了屏幕截图)。
您显示的代码非常随机,关于失败原因的详细信息也非常随机。所以也许我的建议对你的问题没有意义。
与其直接调用 TestLogTarget
,不如设置一个日志配置:
var target = new TestLogTarget() { Name = "Test" };
NLog.Config.SimpleConfigurator(target);
var logger = NLog.LogManager.GetCurrentClassLogger();
logger.Info("Hello World");
确保在消息的访问周围添加 lock
。通过在按住 lock
的同时制作 ToArray()
(或在按住 lock
的同时调用 Contains
)
请记住,NLog 是一个全局引擎,在单元测试环境中需要特别努力,其中 test-类、test-appdomains 会频繁停止和启动,因此您需要了解您的单元测试-系统和您的 nlog 系统,使它们协同工作。
上述问题恰好与VisualStudio中的XUnit Runner有关。尽管我禁用了 'DO NOT RUN TESTS IN PARALLEL',但测试以某种方式并行进行了 运行。 @rolf-kristensen 在另一个 NLog 问题(参考文献:https://github.com/NLog/NLog/issues/2525)中指出他添加了以下内容:
[assembly: Xunit.CollectionBehavior(DisableTestParallelization = true)]
在 AssemblyInfo.cs
文件中。 XUnit 的页面上也提到了此配置(参考:https://xunit.github.io/docs/running-tests-in-parallel.html - 更改默认行为)
这是我的环境:
- Visual Studio 2017
- 项目的.NET运行时间版本是4.6.2
- XUnit 版本 2.3.1
- NLog 版本 4.4.12
- 流畅断言 4.19.4
这是问题所在:
当我 运行 测试单独通过时,但是当我 运行 通过测试资源管理器中的 "Run All" 按钮时,我失败了,当重复 运行随后失败的任务,最终全部通过。还想指出的是,我并不是 运行 并行测试。测试的性质使得被测代码发出最终在自定义 NLog 目标中结束的日志信息。这是一个示例程序,可以 运行 以重现问题。
using FluentAssertions;
using NLog;
using NLog.Common;
using NLog.Config;
using NLog.Targets;
using System;
using System.Collections.Concurrent;
using System.IO;
using Xunit;
namespace LoggingTests
{
[Target("test-target")]
public class TestTarget : TargetWithLayout
{
public ConcurrentBag<string> Messages = new ConcurrentBag<string>();
public TestTarget(string name)
{
Name = name;
}
protected override void Write(LogEventInfo logEvent)
{
Messages.Add(Layout.Render(logEvent));
}
}
class Loggable
{
private Logger _logger;
public Loggable()
{
_logger = LogManager.GetCurrentClassLogger();
}
private void Log(LogLevel level,
Exception exception,
string message,
params object[] parameters)
{
LogEventInfo log_event = new LogEventInfo();
log_event.Level = level;
log_event.Exception = exception;
log_event.Message = message;
log_event.Parameters = parameters;
log_event.LoggerName = _logger.Name;
_logger.Log(log_event);
}
public void Debug(string message)
{
Log(LogLevel.Debug,
null,
message,
null);
}
public void Error(string message)
{
Log(LogLevel.Error,
null,
message,
null);
}
public void Info(string message)
{
Log(LogLevel.Info,
null,
message,
null);
}
public void Fatal(string message)
{
Log(LogLevel.Fatal,
null,
message,
null);
}
}
public class Printer
{
public delegate void Print(string message);
private Print _print_function;
public Printer(Print print_function)
{
_print_function = print_function;
}
public void Run(string message_template,
int number_of_times)
{
for (int i = 0; i < number_of_times; i++)
{
_print_function($"{message_template} - {i}");
}
}
}
public abstract class BaseTest
{
protected string _target_name;
public BaseTest(LogLevel log_level)
{
if (LogManager.Configuration == null)
{
LogManager.Configuration = new LoggingConfiguration();
InternalLogger.LogLevel = LogLevel.Debug;
InternalLogger.LogFile = Path.Combine(Environment.CurrentDirectory,
"nlog_debug.txt");
}
// Register target:
_target_name = GetType().Name;
Target.Register<TestTarget>(_target_name);
// Create Target:
TestTarget t = new TestTarget(_target_name);
t.Layout = "${message}";
// Add Target to configuration:
LogManager.Configuration.AddTarget(_target_name,
t);
// Add a logging rule pertaining to the above target:
LogManager.Configuration.AddRule(log_level,
log_level,
t);
// Because configuration has been modified programatically, we have to reconfigure all loggers:
LogManager.ReconfigExistingLoggers();
}
protected void AssertTargetContains(string message)
{
TestTarget target = (TestTarget)LogManager.Configuration.FindTargetByName(_target_name);
target.Messages.Should().Contain(message);
}
}
public class TestA : BaseTest
{
public TestA() : base(LogLevel.Info)
{
}
[Fact]
public void SomeTest()
{
int number_of_times = 100;
(new Printer((new Loggable()).Info)).Run(GetType().Name,
number_of_times);
for (int i = 0; i < number_of_times; i++)
{
AssertTargetContains($"{GetType().Name} - {i}");
}
}
}
public class TestB : BaseTest
{
public TestB() : base(LogLevel.Debug)
{
}
[Fact]
public void SomeTest()
{
int number_of_times = 100;
(new Printer((new Loggable()).Debug)).Run(GetType().Name,
number_of_times);
for (int i = 0; i < number_of_times; i++)
{
AssertTargetContains($"{GetType().Name} - {i}");
}
}
}
public class TestC : BaseTest
{
public TestC() : base(LogLevel.Error)
{
}
[Fact]
public void SomeTest()
{
int number_of_times = 100;
(new Printer((new Loggable()).Error)).Run(GetType().Name,
number_of_times);
for (int i = 0; i < number_of_times; i++)
{
AssertTargetContains($"{GetType().Name} - {i}");
}
}
}
public class TestD : BaseTest
{
public TestD() : base(LogLevel.Fatal)
{
}
[Fact]
public void SomeTest()
{
int number_of_times = 100;
(new Printer((new Loggable()).Fatal)).Run(GetType().Name,
number_of_times);
for (int i = 0; i < number_of_times; i++)
{
AssertTargetContains($"{GetType().Name} - {i}");
}
}
}
}
上面的测试代码运行更好。在按照消息进行一些较早的故障排除后,似乎我没有调用 LogManager.ReconfigExistingLoggers();
,因为配置是以编程方式创建的(在测试 类 的构造函数中)。这是LogManager
源代码中的注释:
/// Loops through all loggers previously returned by GetLogger.
/// and recalculates their target and filter list. Useful after modifying the configuration programmatically
/// to ensure that all loggers have been properly configured.
之后所有测试 运行 都符合预期,偶尔会出现如下所示的失败:
我现在想知道在我的测试设置中是否还有什么我应该保护的,或者这是一个错误 NLog。任何关于如何修复我的测试设置或对设置进行故障排除的建议都将非常受欢迎。提前致谢。
更新
- 已将
List<LogData>
更改为ConcurrentBag<LogData>
。然而,这并没有改变问题。问题仍然是消息没有及时到达集合中。 - 重新表述问题并将之前的代码示例替换为实际示例(可以 运行 重现问题)+ 问题的屏幕截图。
- 改进了测试,运行 更好但偶尔会由于来自 NLog 本身的异常而失败(添加了屏幕截图)。
您显示的代码非常随机,关于失败原因的详细信息也非常随机。所以也许我的建议对你的问题没有意义。
与其直接调用 TestLogTarget
,不如设置一个日志配置:
var target = new TestLogTarget() { Name = "Test" };
NLog.Config.SimpleConfigurator(target);
var logger = NLog.LogManager.GetCurrentClassLogger();
logger.Info("Hello World");
确保在消息的访问周围添加 lock
。通过在按住 lock
的同时制作 ToArray()
(或在按住 lock
的同时调用 Contains
)
请记住,NLog 是一个全局引擎,在单元测试环境中需要特别努力,其中 test-类、test-appdomains 会频繁停止和启动,因此您需要了解您的单元测试-系统和您的 nlog 系统,使它们协同工作。
上述问题恰好与VisualStudio中的XUnit Runner有关。尽管我禁用了 'DO NOT RUN TESTS IN PARALLEL',但测试以某种方式并行进行了 运行。 @rolf-kristensen 在另一个 NLog 问题(参考文献:https://github.com/NLog/NLog/issues/2525)中指出他添加了以下内容:
[assembly: Xunit.CollectionBehavior(DisableTestParallelization = true)]
在 AssemblyInfo.cs
文件中。 XUnit 的页面上也提到了此配置(参考:https://xunit.github.io/docs/running-tests-in-parallel.html - 更改默认行为)