只显示最后一次测试

时间:2018-02-04 18:11:09

标签: c# selenium nunit extentreports

我在Selenium \ Appium中使用ExtentReport作为我的记者。 测试完成后,我使用 gst-launch-1.0 -v tcpclientsrc host=SAME_IP port=5000 ! gdpdepay ! rtph264depay ! avdec_h264 ! videoconvert ! queue ! jpegenc ! multifilesink location=output_%05d.jpgTearDown,如下所示。

OneTimeTearDown

最近我添加了一个扩展方法,扩展了Nunit的[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(); } 属性。 那是我的扩展代码。 (顺便提一下:https://testingrepository.com/retry-failed-tests-in-nunit/

Retry

当我将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 } 属性的数量设置为3(例如)时,如果测试失败两次并在第3次传递,则CustomeRetry将显示3次测试,我希望看到的是每个test \ testcase的最后一个。 如果测试只运行一次,那么我很好,但就我的例子而言,我只是希望看到这个测试通过。如果测试失败了3次,那么这个测试只有一行,并标记为失败。 有什么建议怎么做?

1 个答案:

答案 0 :(得分:1)

我在没有ExtentReport的大量知识的情况下回答这个问题,但问题看起来很清楚,所以这里就是......

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

根据您的想法,有两种解决方案。

  1. 从您的TearDown中删除您不希望为每次重试执行的代码并将其放入包装中。

  2. 使用基于测试结果的条件来决定是否执行应该只执行上次的代码。

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

相关问题