NUnit - TestContext.CurrentContext.Result.Outcome.Status 总是 "Inconclusive"

NUnit - TestContext.CurrentContext.Result.Outcome.Status is always "Inconclusive"

我注意到 NUnit 的 TestContext.CurrentContext.Result.Outcome.Status 在测试 运行 结束时总是 Inconclusive。 CurrentContext 也不知道已发生任何断言。

是否可以在 [TearDown] 之前获取测试状态?

我希望在我的测试管理 class 的 Dispose() 期间使用该值来捕获指标和其他数据以进行 post-测试诊断。

来自新 .NET Framework 4.6.1 项目的代码示例,该项目仅包含 NuGet 包 NUnit 和 FluentAssertions:

namespace TestProject
{
    using FluentAssertions;
    using NUnit.Framework;

    [TestFixture]
    public class Class1
    {
        [Test]
        public void test1()
        {
            var a = 1;
            var b = 2;
            var c = 1;

            var context = TestContext.CurrentContext;

            a.Should().Be(c);

            Assert.AreEqual(a, c);
        }
    }
}

测试结果以 Inconclusive 开头。如果跳过或忽略测试,则结果会改变,但当然不会执行。

如果执行,Outcome为Inconclusive,直到测试结束。显然,当您仍在执行测试时,它还没有完成。当拆卸开始时,测试的结果是已知的,因此它会根据测试方法本身是否成功而有所不同。当然,teardown 中的异常可能会将结果更改为 Error 状态。

最重要的是,结果字段没有用,而测试方法本身仍然是 运行。无论如何,如果您正在执行测试方法中的代码,那么测试还没有失败。否则,你不会继续执行!

你说你不能使用TearDown但是你提供的link没有处理访问测试结果的问题。你能进一步解释一下吗?看完测试结果你到底想做什么?

您可以尝试将 AssertionScopeAssertionScope.Succeeded 标志一起使用。尽管 属性 上的智能感知指定:

Gets a value indicating whether or not the last assertion executed through this scope succeeded.

AssertionScope

的用法示例
[Test]
public void Test()
{

    var a = 1;
    var b = 2;
    var c = 1;

    var context = new AssertionScope();

    try
    {
        throw new Exception("Test");
    }
    catch (Exception e)
    {
        context.FailWith(e.ToString());
    }

    var strings = context.Discard();

    Console.WriteLine(strings.StringJoin(","));

    context.Succeeded.Should().BeFalse();


    var someOtherContext = new AssertionScope();

    try
    {
        c.Should().Be(a);
    }
    catch (Exception e)
    {
        someOtherContext.FailWith(e.ToString());
    }

    var discard = someOtherContext.Discard();

    Console.WriteLine(discard.StringJoin(","));

    someOtherContext.Succeeded.Should().BeTrue();

}