如何在catch-block中从Assert.Multiple获取所有保存的结果?

时间:2018-04-03 13:26:29

标签: c# nunit-3.0

[Test]
public void AssertMultipleTest()
{
    try
    {
        Assert.Multiple(() =>
        {
            Assert.That(true, Is.False);
            Assert.That(7, Is.Zero);
        });
    }
    catch (Exception e)
    {
        Log.Save(e.ToString());
        throw;
    }
}

测试功能生成以下报告:

  One or more failures in Multiple Assert block:
  1)   Expected: False
  But was:  True

  2)   Expected: 0
  But was:  7

如何在catch-block中获取相同的报告?现在我只得到以下内容:

Tests.AssertMultipleTest - NUnit.Framework.MultipleAssertException: One or more failures in Multiple Assert block:
   at NUnit.Framework.Assert.Multiple(TestDelegate testDelegate)
   at AnonymizeDataTests.Tests.AssertMultipleTest() in C:\Development\TestClass.cs:line 76

1 个答案:

答案 0 :(得分:0)

也许我找到了解决问题的方法。我没有在测试中记录错误,而是跳过整个try-catch-block并将结果记录在TearDown中。像这样:

    [TearDown]
    public void TearDown()
    {
        if (TestContext.CurrentContext.Result.Outcome.Status != TestStatus.Passed)
        {
            Log.Save(TestContext.CurrentContext.Result.Message);
        }
    }

    [Test]
    public void AssertMultipleTest()
    {
        Assert.Multiple(() =>
        {
            Assert.That(true, Is.False);
            Assert.That(7, Is.Zero);
        });
    }