wpf SpecFlow 停止功能,以防场景失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25645845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 12:27:26  来源:igfitidea点击:

SpecFlow stop feature in case a scenario fails

c#wpfspecflow

提问by Timeless

I've started using SpecFlow rather recently in a WPF C# application, for UI testing. I have a feature that runs a series of chained scenarios, and I'd like to know if there is a way of stopping the execution of the feature if one scenario fails. I'm aware that the execution can be stopped if debugging, but I'm wondering if I can stop it while running. I've been thinking of trying to stop it in the [AfterScenario()] method. Can this be done?

我最近开始在 WPF C# 应用程序中使用 SpecFlow 进行 UI 测试。我有一个运行一系列连锁场景的功能,我想知道如果一个场景失败,是否有办法停止该功能的执行。我知道如果调试可以停止执行,但我想知道是否可以在运行时停止它。我一直在考虑尝试在 [AfterScenario()] 方法中阻止它。这能做到吗?

[AfterScenario()]
public static void After()
{
    if (TestContext.CurrentContext.Result.State != TestState.Success)
    {
        //stop feature from going to the next scenario
    }
}

回答by Brantley Blanchard

Using Feature Context, you could probably have each sentence start with something like this...

使用Feature Context,您可能可以让每个句子以这样的开头...

Given Previous tests did not fail

In that sentence, you verify the current feature context doesn't have a false value. That might look something like this...

在这句话中,您验证当前特征上下文没有错误值。那可能看起来像这样......

bool testsPass = FeatureContext.Current["TestsPass"];
Assert.IsTrue(testsPass);

You would have to remember to set this value before any assert. Off hand, that might look something like this...

您必须记住在任何断言之前设置此值。顺便说一句,这可能看起来像这样......

bool testPassed = //something to test
FeatureContext.Current["testsPass"] = testPassed;
Assert.IsTrue(testPassed);

As the comment said, typically it's not a good idea to expect scenarios to always run in a particular order. Depending on the runner, they may not do as expected.

正如评论所说,通常期望场景始终以特定顺序运行并不是一个好主意。根据跑步者的不同,他们可能不会按预期进行。

Note: on second look, a BeforeScenario might work better but I would still suggest using a @serial tag or something to indicate that's what it's doing.

注意:再看一遍, BeforeScenario 可能会更好,但我仍然建议使用 @serial 标签或其他东西来表明它正在做什么。

回答by Sam Holder

I don't know if stopping the entire feature run is possible, after all really all that specflow does is generate tests in the framework of your choice which are then run by some test runner. No unit test runner I know will allow a complete abort of all other tests if one fails. But that doesn't mean that what you want isn't possible.

我不知道是否可以停止整个功能运行,毕竟 specflow 所做的就是在您选择的框架中生成测试,然后由某个测试运行器运行。如果一个测试失败,我知道没有任何单元测试运行器会允许所有其他测试完全中止。但这并不意味着您想要的东西是不可能的。

I can think of a way to 'fake' what you want, but its a bad idea. you could set some sort of flag in the AfterScenario (like creating a file on the disk or setting a mutex) and then checking for this flag in the BeforeScenario and failing fast (with a message like 'skipping test as previous failure detected') if the flag exists. You could clear the flag on the BeforeFeature to ensure that the tests always start clean.

我可以想出一种“伪造”你想要的东西的方法,但这是一个坏主意。您可以在 AfterScenario 中设置某种标志(例如在磁盘上创建文件或设置互斥锁),然后在 BeforeScenario 中检查此标志并快速失败(出现类似“检测到先前失败时跳过测试”的消息)标志存在。您可以清除 BeforeFeature 上的标志以确保测试始终以干净的方式开始。

Like I said I think this is a bad idea and you should really reconsider how your tests work.

就像我说的,我认为这是一个坏主意,您应该真正重新考虑测试的工作方式。

Based on the extra information given in your last comment it seems that even though you recreate your db for the feature and then run multiple scenarios on the clean db, actually each scenario needs its own clean database. Could you not create a new database for each scenario and then have a single scenario create all the data it needs in its given and then test only a single thing? This seems much more scalable and maintainable in the long term to me.

根据您上一条评论中给出的额外信息,似乎即使您为该功能重新创建数据库,然后在干净的数据库上运行多个场景,实际上每个场景都需要自己的干净数据库。难道你不能为每个场景创建一个新的数据库,然后让一个场景在给定的情况下创建它需要的所有数据,然后只测试一个东西吗?从长远来看,这对我来说似乎更具可扩展性和可维护性。

I have done something similar to this before and created a new database for each scenario and it has worked ok.

我之前做过类似的事情,并为每个场景创建了一个新的数据库,它工作正常。