我在 Selenium\Appium 中使用 ExtentReport 作为我的记者。测试完成后,我正在使用TearDown
andOneTimeTearDown
如下。
[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(eg) 时,如果一个测试失败两次并在第三次通过,那么ExtentReport
将显示 3 个测试,我想看到的是每个测试\测试用例的最后一个. 如果测试只运行一次,那我很好,但就我的例子而言,我只想看到这个测试通过。如果一个测试失败了 3 次,那么这个测试只有一行,并标记为失败。任何建议如何做到这一点?