C# FluentAssertions 在断言失败后继续

C# FluentAssertions continue after Failed Assertion

FluentAssertions断言失败后是否可以继续? 我有一些断言,这些断言不会阻止,只应报告但不会未通过测试 运行。

[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void TestMethod1()
    {
        using (var scope = new AssertionScope())
        {
            "This Should not Failed with an AssertException".Should().Be("Should Failed");
            "And this also not".Should().Be("Should Failed");
            "All should only be printed to the console".Should().NotBeEmpty();
        }
        "But the Test should continue".Should().Be("And Failed here with an AssertException");
    }
}

您可以将断言包装在 AssertionScope 中以在单个异常中捕获所有失败。另见 https://fluentassertions.com/introduction#assertion-scopes

对于输出端,使用 XUnit 中的 ITestOutputHelper — 这是在 XUnit 2.0+ 中获取测试日志输出的唯一方法。如果您必须将检查编写为断言,您可以提供自己的 IAssertionStrategy 实现作为 AssertionScope 的构造函数参数,并让它将断言失败消息发送到 XUnit 的测试输出而不是抛出。

注意:您至少需要 v5.9.0 个 FluentAssertions 才能完成此操作。

public class XUnitTestOutputAssertionStrategy : IAssertionStrategy
{
    private readonly ITestOutputHelper output;
    private readonly List<string> failures = new List<string>();

    public XUnitTestOutputAssertionStrategy(ITestOutputHelper output)
    {
        this.output = output;
    }

    public void HandleFailure(string message)
    {
        failures.Add(message);
    }

    public IEnumerable<string> DiscardFailures()
    {
        var snapshot = failures.ToArray();
        failures.Clear();
        return snapshot;
    }

    public void ThrowIfAny(IDictionary<string, object> context)
    {
        if (!failures.Any()) return;

        var sb = new StringBuilder();
        sb.AppendLine(string.Join(Environment.NewLine, failures));
        foreach ((string key, object value) in context)
            sb.AppendFormat("\nWith {0}:\n{1}", key, value);

        output.WriteLine(sb.ToString());
    }

    public IEnumerable<string> FailureMessages => failures;
}

public class ContrivedTests
{
    private readonly ITestOutputHelper output;

    public ContrivedTests(ITestOutputHelper output)
    {
        this.output = output;
    }

    [Fact]
    public void WhenRunningTest_WithContrivedExample_ShouldOutputThenAssert()
    {
        using (new AssertionScope(new XUnitTestOutputAssertionStrategy(output)))
        {
            "Failures will log".Should().Contain("nope", "because we want to log this");
            "Success won't log".Should().StartWith("Success", "because we want to log this too, but it succeeded");
        }

        "This line will fail the test".Should().StartWith("Bottom Text", "because I pulled a sneaky on ya");
    }
}