Cucumber-JVM: Receiving results with Hooks

497 views
Skip to first unread message

Robert

unread,
Sep 5, 2011, 1:45:06 AM9/5/11
to cu...@googlegroups.com
Eager to try out the latest commit to Cucumber-JVM, I went about updating my code to the "Hooks can receive results." update.

$ git log
commit 21f3abd9fed3ef1757a012e43e7982ae3a028880
Date:   Sun Sep 4 18:36:50 2011 +0100

    Hooks can receive results. Closes #27.

Once in place, I updated my example step defs to incorporate retreiving the results of a scenario.  I based my code on what I'm interpreting to be the correct way to implement something like this -- I had a look at the ScenarioResultTest.java file for inspiration.  My step defs look like

public class HelloSteps {
    private ScenarioResult result = new ScenarioResult();
   
    @Before
    public void beforeStep() {
        System.out.println("BEFORE step: class = " + HelloSteps.class.getSimpleName());
    }
   
    @After
    public void afterStep() {
        System.out.println("AFTER step: class = " + HelloSteps.class.getSimpleName());
       
        if (result.isFailed()) {
            System.out.println("Scenario FAILED");
        } else if (result.getStatus().equals("passed")) {
            System.out.println("Scenario PASSED");
        }
    }

    @Given("^a precondition$")
    public void givenStep() {
        System.out.println("\tGiven step");
        assertTrue(true);
    }
   
    @When("^an action takes place$")
    public void whenStep() {
        System.out.println("\tWhen  step");
        assertTrue(true);
    }
   
    @Then("^the expected behavior is displayed$")
    public void thenStep() {
        System.out.println("\tThen  step");
        assertTrue(false);
    }
}


The code seems to be working ok.  I've intentionally set the "Then" step to fail.  As expected, when I run mvn clean test, the JUnit test fails. 

-------------------------------------------------------------------------------
Test set: com.xen.qa.hello.HelloTest
-------------------------------------------------------------------------------
Tests run: 3, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.569 sec <<< FAILURE!
Then the expected behavior is displayed  Time elapsed: 0.002 sec  <<< FAILURE!
java.lang.AssertionError:
    at org.junit.Assert.fail(Assert.java:91)
    at org.junit.Assert.assertTrue(Assert.java:43)
    at org.junit.Assert.assertTrue(Assert.java:54)
    at com.xen.qa.hello.HelloSteps.thenStep(HelloSteps.java:45)
    at ?.Then the expected behavior is displayed(features/hello_stuff/hello.feature:7)

However, the scenario reports a passing result. 

Running com.xen.qa.hello.HelloTest
BEFORE step: class = HelloSteps
    Given step
    When  step
    Then  step
AFTER step: class = HelloSteps
Scenario PASSED

I figure I've overlooked something - it's not the first time ;-), or perhaps I've implemented this incorrectly.


aslak hellesoy

unread,
Sep 5, 2011, 2:25:37 AM9/5/11
to cu...@googlegroups.com
You are not meant to instantiate your own result instance. (Cucumber can't magically get to this instance and change it).
Instead, let your After block take a ScenarioResult as an argument.

Aslak
 

--
You received this message because you are subscribed to the Google Groups "Cukes" group.
To view this discussion on the web visit https://groups.google.com/d/msg/cukes/-/zqcIr3OO_1EJ.
To post to this group, send email to cu...@googlegroups.com.
To unsubscribe from this group, send email to cukes+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/cukes?hl=en.

Robert

unread,
Sep 5, 2011, 2:41:53 AM9/5/11
to cu...@googlegroups.com

I had the feeling I had not done that correctly :). Thanks, Aslak.  I've altered the after block as you suggested, and everything now works as I would expect

    @After
    public void afterStep(ScenarioResult result) {

        System.out.println("AFTER step: class = " + HelloSteps.class.getSimpleName());
       
        if (result.isFailed()) {
            System.out.println("Scenario FAILED");
        } else if (result.getStatus().equals("passed")) {
            System.out.println("Scenario PASSED");
        }
    }
 

Robert

unread,
Sep 5, 2011, 11:13:06 PM9/5/11
to cu...@googlegroups.com
Now that I've got the scenario result working within the @After hook, is it also possible to somehow get the name of the scenario?  I'd like to attempt to use the scenario name (for reporting, screenshots, etc.) when a failure occurs.

 

 
 

--
You received this message because you are subscribed to the Google Groups "Cukes" group.
To view this discussion on the web visit https://groups.google.com/d/msg/cukes/-/zqcIr3OO_1EJ.
To post to this group, send email to cu...@googlegroups.com.
To unsubscribe from this group, send email to cukes...@googlegroups.com.

aslak hellesoy

unread,
Sep 6, 2011, 1:56:34 AM9/6/11
to cu...@googlegroups.com
Currently not. Cucumber has another, richer API that is used for reporting:


A typical "rich" implementation (for generating, say, HTML or PDF) would implement both interfaces.
Reporter#embedding would be used for screenshots.
There will probably be a Reporter#println method as well, for embedding arbitrary text.

There is an open ticket about how to specify your own formatter/reporter: https://github.com/cucumber/cucumber-jvm/issues/33

Now, in order to be able to embed images (or text) in your reports, we need to offer a way to invoke #embedding or #println. I'm not going to be passing the entire reporter/formatter object to an @After hook (people would just do too many silly things with it). Instead I'll expose *just* the methods you need via ScenarioResult: ScenarioResult#embed and ScenarioResult#println

How does that sound?

 

 
 

--
You received this message because you are subscribed to the Google Groups "Cukes" group.
To view this discussion on the web visit https://groups.google.com/d/msg/cukes/-/zqcIr3OO_1EJ.
To post to this group, send email to cu...@googlegroups.com.
To unsubscribe from this group, send email to cukes...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/cukes?hl=en.

--
You received this message because you are subscribed to the Google Groups "Cukes" group.
To view this discussion on the web visit https://groups.google.com/d/msg/cukes/-/BTamib47mnUJ.

To post to this group, send email to cu...@googlegroups.com.
To unsubscribe from this group, send email to cukes+un...@googlegroups.com.

Robert

unread,
Sep 6, 2011, 1:38:37 PM9/6/11
to cu...@googlegroups.com



That sounds fine.  I had simply wanted to extract the name of the scenario, which I intended to use as part of the screenshot filename that I would save to my local filesystem, but providing access to the possibility of embedding the screenshot, which is tied to the scenario being tested, directly into a report is a far better proposition.  I like the sound of that :) -- easier to tie the error to the scenario that way.


 

 

 
 

--
You received this message because you are subscribed to the Google Groups "Cukes" group.
To view this discussion on the web visit https://groups.google.com/d/msg/cukes/-/zqcIr3OO_1EJ.
To post to this group, send email to cu...@googlegroups.com.
To unsubscribe from this group, send email to cuke...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/cukes?hl=en.
Reply all
Reply to author
Forward
0 new messages