common icon indicating copy to clipboard operation
common copied to clipboard

Being Able to Continue Next Steps on @Then Failures

Open slaout opened this issue 10 years ago • 53 comments

Summary

Add the possibility to being able to mark some "Then" step-definitions so that when they fail, they still mark the result as failed, but continue executing the next steps of the scenario.

Current Behavior

Sometimes, you have such Cucumber scenario:

When I do some extensive computation like loading a heavy web page
Then I check thing 1
And I check thing 2
And I check thing 3
And So on

In this particular situation:

  • all four "Then" have no impact on the rest of the execution of the scenario
  • they are all independent
  • if "I check thing 1" fails, the other 3 "Then"s are in a Schrödinger state: they may be OK, or they may fail, but we will know for sure only after we fix and re-execute the failed step, possibly many times if there are many errors, until all is fine. These retry/fix rountrips can be costly if the number of scenarios and steps are big and if execution is slow, like when testing a heavy website (a pretty common use case of Cucumber).

Expected Behavior

The step-definitions developer should be able to tell that one particular step is an assertion that have no impact on the following steps of the scenario it's used in.
He/she could then add an annotation on that step-definition to allow Cucumber to continue the next steps executions if that one fails.

See the discussion on Google Groups.

Possible Solution

I've made this Cucumber fork for our own needs:
github.com/slaout/cucumber-jvm

Please read the README.md for an explanation and our return of experience on this subject.

It's only working on the Java backend.

See the discussion on Google Groups for people proposing to port it to Ruby, for instance.

Please tell me if the fork is a good starting point for you and you would like me to transform this fork into a Pull Request.

slaout avatar Sep 23 '16 09:09 slaout

If we implement this, it should be done in all implementations (Java, Ruby, JavaScript, others...).

Annotations is a Java-only thing. Not sure how this would be specified in annotation-less languages like Ruby. Maybe this?

Given "some step", {error: :continue} do
end

What do people think?

aslakhellesoy avatar Sep 23 '16 10:09 aslakhellesoy

I read in the thread that the core Cucumber team considers this a useful feature. Is it because you want to expand the user base at the cost of promoting best practices? Or am I missing why this is not a hack?

I do understand why teams would find this a simpler approach than fixing the test code, but in my experience shortcuts cause problems down the line.

paoloambrosio avatar Sep 23 '16 11:09 paoloambrosio

@aslakhellesoy If you come up with a simple flag for Ruby (eg. {error: :continue} instead of something like {exception1.class: :continue, exception2.class: continue}), then I'm totally fine with the Java implementation to become just @ContinueOnFailure (without any Exception.class array as parameter) instead of @ContinueNextStepsFor({AssertionError.class, NoSuchElementException.class }).

In practice, we always use this same set of two Exception classes for consistency, simplicity of development, and a not-error-prone development workflow. And sometimes, developers went to some crazy stuff to transform a NullPointerException into an AssertionError.

So we can keep it simple with only @ContinueOnFailure and nothing else.

slaout avatar Sep 23 '16 12:09 slaout

Another option is to use a similar mechanism as for pending steps, that is not require that something is specified on the step definitions, but rather a specific exception type built into each Cucumber implementation that mean, "step failed, but continue". That should at least be straight forward to implement in all the Cucumber implementations (also in languages without Annotations).

brasmusson avatar Sep 29 '16 18:09 brasmusson

I like @brasmusson's suggestion, because it's more portable. For example, for Java8 there are no annotations - what do we do there?

@paoloambrosio I did say in the mailing list thread that I'm open to adding this, but I'm still very much on the fence. Thanks for challenging this. You're making a good point that it's better to fix the code, and in the past I've held a strong position to keep Cucumber an opinionated tool. I would never use this feature myself - it's at odds with my ethics ;-)

@mattwynne what do you think? It's a bit of a slippery slope this one. If we add it aren't we essentially saying that flickering tests are ok? (They most certainly are not in my book).

aslakhellesoy avatar Sep 29 '16 19:09 aslakhellesoy

I think that @brasmusson 's idea is elegant as an implementation in Cucumber but not much for the user in all languages. Users with flickering tests probably want to use the same assertion library but triggering a different behaviour. In this case they would have to surround everything in a try/catch/rethrow block. Sure, some languages might allow that to be defined as a higher order function (i.e. magicFix { standard assertions } ), but in others like Java 6 the syntax would be horrible.

I still haven't heard why we are trying to promote bad practices with this feature instead of convincing users that it will just mask their broken pipeline and process, so I would me more in favour of introducing official extension points so that external plugins could decide to make Cucumber continue on failure in certain cases (like if a specific exception is raised... that would work well since the plugin doesn't need to support all languages and implementations). Is this worth investigating?

paoloambrosio avatar Sep 30 '16 05:09 paoloambrosio

...trying to promote bad practices with this feature...

@paoloambrosio I would not say it is bad practice. It allows you to see what all went wrong in a given scenario instead of just the first thing that went wrong. This type of reporting errors in tests is quiet common in golang, for example.

muhqu avatar Sep 30 '16 06:09 muhqu

The Google Test framework has both ASSERT_THAT and EXPECT_THAT. If checking a person data object with:

  ASSERT_THAT(<name is correct>);
  ASSERT_THAT(<address is correct>);
  ASSERT_THAT(<email is correct>);

then if the address is not correct, the test will not continue and I will not know whether the email was correct or not. If using:

  EXPECT_THAT(<name is correct>);
  EXPECT_THAT(<address is correct>);
  EXPECT_THAT(<email is correct>);

then if the address is not correct, the email will also be checked and I will know if only the address is the problem, or if both the address and the email are wrong. That is valuable information when trying to fix the issue. In JUnit the same thing can be achieved with @Rule annotations and ErrorCollectors (IIRC there is a request to support this in Cucumber-JVM).

Users with flickering tests

@paoloambrosio I see this feature as also supporting "EXPECT_THAT" checks in Cucumber, not as something that can be activated for flickering tests.

brasmusson avatar Sep 30 '16 06:09 brasmusson

@muhqu can you help me with an example of when it's useful? I'm struggling to see the value without examples and no one has given any valid ones in the threads I've read.

I can understand how this could be useful at the unit level when asserting multiple aspects of a domain object. This in Cucumber's terms would be using a table argument (that was indeed the first suggestion from George in the thread linked above).

Packing multiple unrelated assertions in a unit testing is IMO as bad as unrelated Thens in Cucumber, and should be avoided.

If the assertions are related, does it really help if you see all failures or only the first one?

paoloambrosio avatar Sep 30 '16 07:09 paoloambrosio

@paoloambrosio here is an example showing what I mean when I said this type of reporting errors in tests is quiet common in golang.

I think especially in Cucumber/Gherkin it makes totally sense to bail out on the first error when running a scenario's Given, When and their And steps, as these are intended to set the ground for the scenario. However, with Then steps we're starting to check for expectations and there is a clear benefit of letting the user know all the expectations that have been failed.

Here's a simple example:

Scenario: User Login
   Given I am a registered user of this site
     And I am on the homepage
    When I enter my credentials in the nav-bar login form
     And I submit the form by hitting return
    Then I should see my inbox
     And I should see my name in the navbar
     And I should see ...

Assuming these tests run on some selenium cluster to test the behavior on various different browsers and devices… the time to run the tests will be significant. Therefor the tester has an interest to know as much as possible about what expectations have not been met. Don't you agree on that?

muhqu avatar Sep 30 '16 08:09 muhqu

Here is another return about several months of using the patch.

I just made another pass on all our @Then steps. The conclusion: almost all of them have the @ContinueNextSteps annotations (with the same declared exceptions, for simplicity and less-error-prone sake). The only sentences that do not have the annotations are sentences like:

  • System redirects to the Xxxx page
  • On block Xxxx, an error message "Yyyy" appears
  • The frame Xxxx is unfolded

With such sentences, failure means the remaining of the scenario will fail.

Beside the "This @ContinueOnFailure is helpful in many projects, tools and languages" vs. "This @ContinueOnFailure can be used as bad practices" debate, there is another debate: if used, it needs a lot of care to make sure to not forget to tag the right steps, and use the right exceptions. We solved the "and use the right exceptions" part by a dumb "we always use the same exception set". We still have the "it needs a lot of care to make sure to not forget to tag the right steps" part.

To answer this second part, considering we use the annotation in almost all @Then steps, and to take into account all Cucumber supported languages, why not:

Forget about the annotation, and offer a new runtime option "Continue next steps on @Then step failures"? Much like the strict=false option (which is also a bad practice to allow it to be false, by the way)

It would come with a warning "be careful, this is a bad practice" when enabled. And for the rare cases it would not make sense to continue execution, I don't care: next steps will fail too quite quickly.

slaout avatar Oct 14 '16 15:10 slaout

For me the benefit of this feature is for teams where the cost of the Given / When parts of the scenario is high - dependencies on slow external services or a deep workflow process. In this context it's often pragmatic to make a handful of checks while you're in that spot.

What I worry about is how we'd clearly feed back the results. Cucumber implementations are built with the assumption that each scenario has only one result. Capturing and communicating multiple failure results for one scenario seems to me would require some deep internal changes. Can you explain more about how this works in your fork?

mattwynne avatar Oct 25 '16 07:10 mattwynne

@mattwynne the fork is really simple: if you exclude groupId change, documentation change, the Java annotation class declaration, redirection of the annotation from layer to layer... the "heavy" refactoring only comes down to that change (yes, there even was a skipNextStep boolean already in Runtime.java):

core/src/main/java/cucumber/runtime/Runtime.java
-                skipNextStep = true;
+                if (!match.continueNextStepsAnyway(t)) {
+                    skipNextStep = true;
+                }

The whole patch difference is here: https://github.com/cucumber/cucumber-jvm/compare/master...slaout:continue-next-steps-for-exceptions-1.2.4

Here is a real example from our project; this is a Jenkins report (we did NOT fork the Jenkins plugin for that, it works out of the box):

image

  • The page takes 16 seconds to load fully (with async analytics JSs, heavy images and videos... For a real user, most things are loaded in a few seconds, but Selenium waits everything is loaded before continuing the scenario)
  • There are 7 "Then" assertions, roughly 0.1 second each
  • It takes 17 seconds to run, instead of 2 minutes if each "Then" was in its own scenario
  • Each failed step has its own error message (here, I unfolded one, and my mouse is on the other one): thankfully, errors are attached to the failed step instead of the whole scenario, and all report tools already support this fork without knowing it :-)
  • JUnit view on Eclipse or IntelliJ are also capable to displaying the same sort of results where each step (viewed as a single JUnit test) can fail: see the screen capture of https://github.com/slaout/cucumber-jvm
  • Attachements to the scenario are still at the end of the scenario (here logs, screenshot and video link)

slaout avatar Oct 25 '16 13:10 slaout

@mattwynne wrote: [...] Cucumber implementations are built with the assumption that each scenario has only one result. […]

That is still true. A scenario fails when at least one step failed. Not when the last executed step failed.

However, I'm wondering what status a scenario would have when it contains failing and pending steps... @slaout, can you let us know?

muhqu avatar Oct 26 '16 15:10 muhqu

@muhqu That's a good question, as I always run in strict mode.

In such cases, the result is FAILED in strict mode.

But in non-strict mode, you made a good point, because it is inconsistent: SKIPPED for the scenario, and FAILED for the feature file.

In Strict Mode

On JUnit: image

As well as in the HTML reports: image

In Non-Strict Mode:

On JUnit: image

In HTML reports: exact same result as in strict mode, but only because the feature is not highlighted with any color, I think.

slaout avatar Oct 26 '16 16:10 slaout

I had a similar request a while ago in behave. Initially, I suggested the EXPECT_THAT() approach that was suggested by @brasmusson above. But when I tried that approach, it was simpler to provide the basic functionality to user (and allow the user to use this functionality when needed).

SEE ALSO:

jenisys avatar Oct 26 '16 22:10 jenisys

I'm going to propose an approach to the issue of continuing steps even when they fail. Many will condemn it, but works quite well. It's just easier then going into endless debates.

From time to time I met the need to have a soft assert, just like TestNG has, but jUnit folks can't stand it. Sounds like the same dispute going on here.

So, do that: implement a soft assert class in your framework. You might need to add the burden of creating a step to "turn on" and off the soft assert, and a step to check that assert class and fail, if necessary. Then, use the @Before hook to reset the soft assertion to off when each test start.

cbaldan avatar Nov 15 '16 14:11 cbaldan

Hi,

We are losing this feature in our test projects, coudl you please tell us if it's planned for next release ?

Thanks

elarbi avatar Feb 01 '17 11:02 elarbi

@elarbi no, this isn't planned for any upcoming releases.

aslakhellesoy avatar Feb 01 '17 12:02 aslakhellesoy

hope this will be supported soon, just FYI, xcode XCTest supports a test case could continue after a failure. https://developer.apple.com/reference/xctest/xctestcase/1496260-continueafterfailure

djwgit avatar Feb 23 '17 02:02 djwgit