SpecFlow stop feature in case a scenario fails

和自甴很熟 提交于 2021-01-29 06:59:50

问题


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?

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

回答1:


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

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.




回答2:


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.

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.

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.



来源:https://stackoverflow.com/questions/25645845/specflow-stop-feature-in-case-a-scenario-fails

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!