Modularizing long tests

449 views
Skip to first unread message

Hans Westerbeek

unread,
Nov 23, 2011, 12:07:00 PM11/23/11
to spockframework
Hi all,

My question is about a geb-spock Spec but I think my question applies
to Spock in general.
My GebSpec tests end-to-end behaviour on a system that is more complex
than anyone likes :)

I have a very long system test with about 20 when/then pairs, and it
is parameterized.

I would like modularize make elements of this long test. My first
instinct was to put those blocks in private methods, but that doesn't
work, because I can't use the when: and then: labels inside those
private methods.

Do you guys have a recommended approach?

Cheers,
Hans

Dan Durkin

unread,
Nov 23, 2011, 1:02:15 PM11/23/11
to spockfr...@googlegroups.com
Can you use @Stepwise to split them into separate features?

> --
> You received this message because you are subscribed to the Google Groups "Spock Framework - User" group.
> To post to this group, send email to spockfr...@googlegroups.com.
> To unsubscribe from this group, send email to spockframewor...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/spockframework?hl=en.
>
>

Hans Westerbeek

unread,
Nov 23, 2011, 3:48:59 PM11/23/11
to spockfr...@googlegroups.com
Thanks Dan, @Stepwise was new to me. I'm not sure if i'm going to use
it because I want to dive into the domain a bit more first and see if
these guys are going to insist that every test is end-to-end :)

Cheers,
Hans

Hans Westerbeek

unread,
Dec 14, 2011, 8:23:46 AM12/14/11
to Spock Framework - User
Hi all,

Time for me to revisit this issue...

As I posted before, I have very long GebReportingSpec features, but
the question I have relates to Spock's core features, that's why I
think this is the appropriate list. Let me describe the situation. I
was asked to implement a bunch of end-to-end functional tests that
each cover at least 8 pages and will visit some of those multiple
times. You can imagine that, if I apply no means of modularizing some
of the steps, I will end up with endless sequences of when/then and a
lot of duplicated lines.

My first instinct was to put some of these steps into closures, which
I could then re-use across multiple feature-specs, the advantage being
that the 'current geb page' would remain intact and I could refer to
it.

But I found out that Spock would not do anything with any of the when/
then blocks, most notably with the 'then' blocks, whose assertions
were not seen as assertions anymore. For now, I am using classic JUnit
assertTrue statements inside those closures. But that is not the nice
Spock stuff I am after :)

So, FYI, my test look like this
class SomeSpec extends GebReportingSpec(){
def 'my feature spec'(){
given:
// some situation
buildingBlock()
// dosomething else that sets this test apart
anotherBuildingBlock
}

// a bunch more feature specs that also reuse the blocks below

def buildingBlock = {
// do geb stuff that gets reused
// do JUNIT style asserts
Assert.assertTrue (whatever)
}

def anotherBuildingBlock1 = {
// do other geb stuff that also reused

}

}

So, I am looking for a way to modularize my tests that allows:
-1. keeping those long 8 page scenarios. (these are just required by
the nature of the system under test)
-2. code-reuse for some of the basic steps in those scenarios
-3. Continue to use when/then inside the aforementioned closures

Does anyone have a recipe for this?

Cheers,
Hans

Paul Poling

unread,
Jan 21, 2014, 7:57:04 PM1/21/14
to spockfr...@googlegroups.com
Does Hans, or anyone, have tips on how to do this?  

I'm trying to do what Hans was, also using Geb+Spock, and I don't see a clean way of separating sets of repeated Geb testSteps so that I can reuse them across various features(methods).

Note that @Stepwise isn't sufficient because it doesn't allow reuse across multiple features.  Also, it introduces feature(test) interdependence, which is generally undesirable.  


Thanks!
paul

pnie...@gmail.com

unread,
Jan 21, 2014, 8:14:15 PM1/21/14
to spockfr...@googlegroups.com
On 22 Jan 2014, at 01:57, Paul Poling <pcpo...@gmail.com> wrote:

Does Hans, or anyone, have tips on how to do this?  

I'm trying to do what Hans was, also using Geb+Spock, and I don't see a clean way of separating sets of repeated Geb testSteps so that I can reuse them across various features(methods).

Note that @Stepwise isn't sufficient because it doesn't allow reuse across multiple features.  

Instead of reusing blocks, I’d focus on strong Geb page/module abstractions and reuse those. If necessary, `assert` statements can be used anywhere (Spock helper methods, page/module methods).

Also, it introduces feature(test) interdependence, which is generally undesirable.  

`@Stepwise` methods are dependent by design (not by accident), and it’s much better to have one method per step than one huge method for the whole workflow. It’s easier to understand, and will give you better feedback.

Cheers,
Peter

signature.asc
Reply all
Reply to author
Forward
0 new messages