- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
当我单独运行测试时,它们通过了,但是当我通过测试资源管理器中的“全部运行”按钮运行时,我遇到了失败,并且当重复运行后续失败的任务时,它们最终都通过了。还想指出我没有并行运行测试。测试的性质使得被测代码发出最终在自定义 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>
.然而,这并没有改变问题。问题仍然是消息没有及时到达集合中。 最佳答案
上述问题恰好与 VisualStudio 中的 XUnit Runner 有关。尽管我禁用了“不要并行运行测试”,但测试以某种方式并行运行。 @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 - 更改默认行为)
关于c# - 如何解决 XUnit 测试失败并在 "Run All"下的 NLog 目标中断言消息的情况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48274653/
我找不到任何关于我认为是一个相当明显的问题的信息。xunit.console.clr4 命令行 arg 帮助状态: usage: xunit.console.clr4 [options] usage
当我们使用 MSTest 时,我们有几个环境有自己的运行设置。由于 Microsoft 正在放弃 MSTest,我们将转向 xUnit。无论是通过运行设置还是命令行属性,我都需要一种在 xUnit 测
我试图使用 OpenCover(今天下载)来覆盖我的测试。这是我使用的命令行: OpenCover.Console.exe -target:"c:\Programmes2\xunit\xunit.co
我正在学习使用单元测试,我创建了一个项目,添加了 xunit 引用。 以及以下代码: namespace UnitTestProject { public partial class Form
Closed. This question does not meet Stack Overflow guidelines。它当前不接受答案。 想要改善这个问题吗?更新问题,以便将其作为on-topi
我有一个在 Windows 上构建的程序集 我想在 Linux 中的单声道上运行 xUnit 测试。 但是,我发现虽然其中 400 个测试可以(按顺序)运行,但某些测试要么挂起 xUnit 运行程序,
我有一个 F# 类库,其中包含使用 NuGet 安装的“xUnit.net”和“xUnit.net Runners”包。我有以下代码: module XUnitTest open Xunit [] l
I know that this question has been asked multiple times before I raise it again, However I still cou
我们使用的是 dotCover 2.7 和 xUnit.net 1.9.2。 在我的机器 (Windows 7) 和同事的机器 (Windows 8) 上,我们可以从命令行针对我们使用 xUnit.n
是否可以将 xUnit 与 LINQPad 一起使用? 能够首先为在 LINQPad 中设计的概念编写一些测试会很棒。这比添加另一个 ConsoleApp23423894238 更容易,只是为了能够快
我有一组需要共享状态的 xunit.net 测试。希望我希望这些测试能够并行运行。所以我希望运行者这样做: 创建共享夹具 运行并行所有使用该夹具的测试 在阅读 xunit 文档时,它说要在测试类之间共
我需要跳过基于类中某些 bool 条件的测试方法。是否可以?如何实现?我试过扩展 FactAttribute 但我无法获得 Test 类的实例。 我的代码如下: using System; using
我有兴趣在多个类上重用测试理论,特别是一些需要相同测试的构造函数。我最初有使用委托(delegate)来执行此功能的想法。 但是,我认为我可能正在尝试重新发明轮子,尽管 C# 具有一些功能,但我认为我
我正在为我的 DataAccessRepository(使用 Entity Framework )类编写测试用例。此类在构造函数中采用两个参数。 1)连接对象 2) Automapper 对象 现在,
xUnit 相当于 NUnit 的 [TestFixtureSetUp] ? 我们探索发现IUseFixture相当于[TestFixtureSetUp] ,但它没有按预期工作。 正如我们所探索的(在
Xunit 1.9.x 为用户提供了 DynamicSkipExample.cs 帮助他设置动态跳过 [Fact] 的示例。 事实证明,这在执行一些跨平台开发时非常有用。当由于底层上下文(操作系统、文
我正在尝试使用 xUnit.net 作为 SpecFlow 的测试运行器。来自官方下载区的 SpecFlow 1.2 二进制文件不包含 xUnit.net 提供程序,但 GitHub 上的主分支有一个
当我尝试运行我的 xUnit.net 测试时,出现此错误: [xUnit.net 00:00:00.63] xunit.UnitTest1.TestTheAnswer [FAIL] Fai
我已经转移到 xunit.net 2.0 测试版,但需要 AutoFixture 的一些功能,这仍然取决于当前的 1.9.2 稳定版本 (CompositeDataAttribute)。据我所知 Au
我已经使用 NUnit 多年了,我想尝试 XUnit。所以我安装了 XUnit 并运行了允许您通过 TD.net 运行 XUnit 的可执行文件。 我似乎不能一次运行多个测试。使用 NUnit + T
我是一名优秀的程序员,十分优秀!