2016-11-22 33 views
0

我正在使用Specflow,Visual Studio 2015和Nunit。如果測試無法再次運行,我需要。我有是否有可能根據結果再次運行相同的specflow測試?

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

如何再次啓動最後的測試?

+0

當您在Visual Studio中運行測試時,可以篩選失敗的測試並再次運行它們。舉例來說,如果你想更加自動化,你可能會想看看像TFS或TeamCity這樣的構建服務器。那些有可能有更多的邏輯圍繞着你的測試來運行它們兩次。 在測試中包含更多代碼以再次運行它們可能存在缺點,因爲這樣測試會發生變化並變得更加複雜。 – Piazzolla

+0

這不是我所需要的。有些測試可能因網絡問題而失敗,這就是爲什麼我想重新運行失敗的原因。最後,我發送了一份報告,這就是爲什麼我需要這個 – Sonja

回答

1

在NUnit中有RetryAttribute(https://github.com/nunit/docs/wiki/Retry-Attribute)。它看起來像是SpecFlow.Retry插件正在使用(https://www.nuget.org/packages/SpecFlow.Retry/)。這個插件是第三方插件,我還沒有使用它。所以不能保證這可以按照你的想法工作。

作爲替代方案,您可以使用SpecFlow + Runner(http://www.specflow.org/plus/)。這個專業運動員可以選擇重新運行失敗的測試。 (http://www.specflow.org/plus/documentation/SpecFlowPlus-Runner-Profiles/#Execution - retryFor/retryCount配置值)。


完全披露:我是SpecFlow和SpecFlow +的開發人員之一。

+0

我已經添加了Specflow.Retry,但安裝後很可能有東西被卸載,所以我不得不恢復。我要嘗試用NUnit重試 – Sonja

0

您可以隨時在斷言步驟中捕獲失敗,然後重試您測試的內容。例如:

[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 
    } 
} 
+0

不是在我的情況下,我有超過2000個測試,只有幾個地方我有Assert.IsTrue或Assert.Equals – Sonja

+1

不會拋出Assert.IsTrue異常?該如何執行? –

相關問題