仅显示最后一次测试

show only the last test

我在 Selenium\Appium 中使用 ExtentReport 作为我的记者。 测试完成后,我使用 TearDownOneTimeTearDown 如下。

[TearDown]
    public void CloseDriver()
    {
        var status = TestContext.CurrentContext.Result.Outcome.Status;
        var stackTrace = "<pre>" + TestContext.CurrentContext.Result.Message + "</pre>";
        var errorMessage = TestContext.CurrentContext.Result.Message;
        if (status == NUnit.Framework.Interfaces.TestStatus.Failed)
        {
            test.Log(LogStatus.Fail, status + errorMessage);
            var ScreenShotPath = Utils.TakeScreenShot(_webdriver);
            test.Log(LogStatus.Fail, "Screen Shot Below: " + test.AddScreenCapture(ScreenShotPath));
        }
        else if (status == NUnit.Framework.Interfaces.TestStatus.Passed)
        {
            test.Log(LogStatus.Pass, status + errorMessage);
        }
        extent.EndTest(test);
       _webdriver.Quit();

        Utils.KilliExplore();
    }
[OneTimeTearDown]
    public void OneTimeTearDown()
    {
        Utils.KillIEDriver();
        extent.Flush();
        extent.Close();
    }

最近我添加了一个扩展方法来扩展 Nunit 的 Retry 属性。 那是我的扩展代码。 (顺便说一下,这是来源:https://testingrepository.com/retry-failed-tests-in-nunit/

public class CustomRetry : PropertyAttribute, IWrapSetUpTearDown
{
    private int _count;
    public CustomRetry(int count) : base(count)
    {
        _count = count;
    }

    #region IWrapSetUpTearDown Members

    public TestCommand Wrap(TestCommand command)
    {
        return new CustomRetryCommand(command, _count);
    }

    #endregion

    #region Nested CustomRetry Class

    /// <summary>
    /// The test command for the RetryAttribute
    /// </summary>
    public class CustomRetryCommand : DelegatingTestCommand
    {
        private int _retryCount;

        /// <summary>
        /// Initializes a new instance of the <see cref="CustomRetryCommand"/> class.
        /// </summary>
        /// <param name="innerCommand">The inner command.</param>
        /// <param name="retryCount">The number of repetitions</param>
        public CustomRetryCommand(TestCommand innerCommand, int retryCount)
            : base(innerCommand)
        {
            _retryCount = retryCount;
        }

        /// <summary>
        /// Runs the test, saving a TestResult in the supplied TestExecutionContext.
        /// </summary>
        /// <param name="context">The context in which the test should run.</param>
        /// <returns>A TestResult</returns>
        public override TestResult Execute(TestExecutionContext context)
        {
            int count = _retryCount;

            while (count-- > 0)
            {
                context.CurrentResult = innerCommand.Execute(context);
                var results = context.CurrentResult.ResultState;

                if (results != ResultState.Error
                    && results != ResultState.Failure
                    && results != ResultState.SetUpError
                    && results != ResultState.SetUpFailure
                    && results != ResultState.TearDownError
                    && results != ResultState.ChildFailure)
                {
                    break;
                }
            }

            return context.CurrentResult;
        }
    }

    #endregion
}

当我将 CustomeRetry 属性的数量设置为 3(例如)时,如果测试失败两次并在第 3 次通过,那么 ExtentReport 将显示 3 个测试,我想看到的是每个 test\testcase 的最后一个。 如果只测试一次 运行 那么我很好,但是对于我的例子,我只想看到这个测试通过。如果一个测试失败了 3 次,那么这个测试只有一行,并标记为失败。 有什么建议吗?

我在回答这个问题时对 ExtentReport 了解不多,但问题看起来很清楚,所以这里...

由于您的自定义包装器包装了 SetUp 和 TearDown,因此 TearDown 方法最多 运行 三次。在 Teardown 中,您调用 extent.EndTest(),因此最多调用 3 次。我想这就是为什么您的测试在报告中出现了三次。

根据您希望发生的情况,有两种解决方案。

  1. 从您的 TearDown 中删除您不想在每次重试时执行的代码,并将其放入您的包装器中。

  2. 根据测试结果使用一个条件来决定是否执行应该只执行最后一次的代码。

  3. 修改包装器,使其只包装测试方法。如果您这样做,请记住,您只会调用一次该方法的 SetUp 和 TearDown。