即使一个失败,SpecFlow也会报告所有断言

时间:2014-07-11 07:25:20

标签: bdd specflow

有人知道SpecFlow是否可以实现这一目标?本着每次测试没有多个断言的精神,我希望SpecFlow能够对待每一个"然后"作为一个单独的测试。

我有一个带有多个"然后"的情景。看起来像这样的步骤(片段):

When a summary for day <day number> is requested
Then the summary well id should be "134134"
 And the summary well name should be "My Well --oops!"
 And the summary violated rules should be <violations>
 And the summary variance should be <variance>

Examples: 
| day number | violations      | variance |
| 0          | BadTotal,HpOnLp | -33      |
| 3          | BadTotal        | -133.33  |
| 5          |                 | 0        |

第二个断言&#34;我的嘛 - 哎呀!&#34;应该失败。我想要的是SpecFlow测试后面的断言

我明白了:

When a summary for day 0 is requested
-> done: DprSummarySteps.WhenASummaryForDayIsRequested(0) (0.0s)
Then the summary well id should be "134134"
-> done: DprSummarySteps.ThenTheSummaryWellIdShouldBe("134134") (0.0s)
And the summary well name should be "My Well --oops!"
-> error: Assert.AreEqual failed. Expected:<My Well --oops!>. Actual:<My Well>. 
And the summary violated rules should be BadTotal,HpOnLp
-> skipped because of previous errors
And the summary variance should be -33
-> skipped because of previous errors
Assert.AreEqual failed. Expected:<My Well --oops!>. Actual:<My Well>. 

我想要的是什么:

When a summary for day 0 is requested
-> done: DprSummarySteps.WhenASummaryForDayIsRequested(0) (0.0s)
Then the summary well id should be "134134"
-> done: DprSummarySteps.ThenTheSummaryWellIdShouldBe("134134") (0.0s)
And the summary well name should be "My Well --oops!"
-> error: Assert.AreEqual failed. Expected:<My Well --oops!>. Actual:<My Well>. 
And the summary violated rules should be BadTotal,HpOnLp
-> done: DprSummarySteps.ThenTheViolatedRulesShouldBe("BadTotal,HpOnLp") (0.0s)
And the summary variance should be -33
-> done: DprSummarySteps.ThenTheVarianceShouldBe(-33) (0.0s)
Assert.AreEqual failed. Expected:<My Well --oops!>. Actual:<My Well>. 

3 个答案:

答案 0 :(得分:1)

我不相信你能做到这一点,但我问你的问题是你为什么要这样做?任何单元测试都会在第一次断言时失败。在断言失败后你不会期望测试继续执行,这没有什么不同。当然知道测试因某种原因失败就足够了。在这种特定情况下,您可能能够创建单独的独立断言来提供有用的信息,但在一般情况下,断言之后的断言可能完全没有意义。

如果您希望每个断言都独立于其他断言,那么您需要将其分解为多个场景,并将每个当前And步骤作为其自己的Then步骤。您可以使用后台步骤进行常规设置。

我不确定这会对您有所帮助,因为您的断言似乎都与示例有关,因此您最终需要重复这些示例。

答案 1 :(得分:0)

如果您升级到Specflow 2.4并使用Nunit,现在可以执行此操作,看到类似https://github.com/nunit/docs/wiki/Multiple-Asserts

Assert.Multiple(() =>
{
    Assert.AreEqual(5.2, result.RealPart, "Real part");
    Assert.AreEqual(3.9, result.ImaginaryPart, "Imaginary part");
});

答案 2 :(得分:0)

我已经确定了一种方法来做到这一点。那么在specflow内部发生了什么,在运行任何步骤之前,TestExecutionEngine检查Scenario的LastExecutionStatus,如果不是OK,它不会继续......所以我们可以做的是,在[AfterStep]钩子中, 添加以下行:

typeof(ScenarioContext).GetProperty("ScenarioExecutionStatus").SetValue(this.ScenarioContext, ScenarioExecutionStatus.OK);

用存在类型为 ScenarioContext 的任何对象替换 this.ScenarioContext。这将使当前状态为 OK,并允许您继续下一步。

请记住,这不会让您在一个步骤中捕获所有断言失败。