【问题标题】:Is it possible to run same specflow test again based on outcome?是否可以根据结果再次运行相同的规范流测试?
【发布时间】:2021-10-15 23:23:05
【问题描述】:

我正在使用 Specflow、Visual Studio 2015 和 Nunit。如果测试再次运行失败,我需要。我有

[AfterScenario]
public void AfterScenario1()
{
    if (Test Failed  and the counter is 1)
    {
        StartTheLastTestOnceAgain();
    }
}

如何重新开始上次测试?

【问题讨论】:

  • 当您在 Visual Studio 中运行测试时,您可以过滤失败的测试并再次运行它们。例如,如果您想更加自动化,您可能需要研究 TFS 或 TeamCity 等构建服务器。那些有可能围绕您的测试有更多的逻辑来运行它们两次。在测试中包含更多代码以再次运行它们可能会带来不利影响,因为测试会发生变化并变得更加复杂。
  • 这不是我需要的。由于网络问题,某些测试可能会失败,这就是为什么我想重新运行那些失败的测试。最后我要发送一份报告,这就是我需要这个的原因

标签: c# testing specflow


【解决方案1】:

在 NUnit 中有 RetryAttribute (https://github.com/nunit/docs/wiki/Retry-Attribute)。看起来 SpecFlow.Retry 插件正在使用它(https://www.nuget.org/packages/SpecFlow.Retry/)。这个插件是一个 3rd 方插件,我还没有使用它。所以不能保证这会如你所愿。

作为替代方案,您可以使用 SpecFlow+Runner (http://www.specflow.org/plus/)。这个专业的跑步者可以选择重新运行你失败的测试。 (http://www.specflow.org/plus/documentation/SpecFlowPlus-Runner-Profiles/#Execution - retryFor/retryCount 配置值)。


完全披露:我是 SpecFlow 和 SpecFlow+ 的开发者之一。

【讨论】:

  • 我添加了 Specflow.Retry 但安装后很可能某些东西被卸载了,所以我不得不恢复。我将尝试使用 NUnit 重试
  • 我无法使用 SpecFlow 重新运行测试。Retry stackoverflow.com/questions/65586334/…
【解决方案2】:

您总是可以在断言步骤中捕获失败,然后重试您的测试目标。比如:

[Given(@"I'm on the homepage")]
public void GivenImOnTheHomepage()
{
    go to homepage...
}
[When(@"When I click some button")]
public void WhenIClickSomeButton()
{
    click button...
}
[Then(@"Something Special Happens")]
public void ThenSomethingSpecialHappens()
{
    var theRightThingHappened = someWayToTellTheRightThingHappened();
    var result = Assert.IsTrue(theRightThingHappened);
    if(!result)
    {
       thenTrySomeStepsAgainHere and recheck result using another assert
    }
}

【讨论】:

  • 不是我的情况,我有超过 2000 次测试,只有少数地方我有 Assert.IsTrue 或 Assert.Equals
  • 不会抛出 Assert.IsTrue 异常吗? if ever 是如何执行的?
猜你喜欢
  • 2018-05-27
  • 2015-12-28
  • 1970-01-01
  • 2020-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-26
  • 1970-01-01
相关资源
最近更新 更多