how best to organize test stories with common setup?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

how best to organize test stories with common setup?

Frank Weber


I started with a single story and extended it along the way.
Now I would like to reorganize it differently.
I plan to split the attached file via separate def "..." in one test
class

So via setupSpeck() these stories can share the same setup, right?
Or what would you recommend?

Regards

Frank


class SearchingInventory {

// setupSpeck() {
// move stuff over from the the where: part
// }

def "retrieving Guitars from Inventory"() {

        when: "guitar with spec1 and price1 exist in Inventory"
                Inventory inventory = new Inventory()
                inventory.addGuitar(new Guitar(serialNr1, price1, spec1))

        and: "two guitars with spec1 and price2 exist in Inventory"
                inventory.addGuitar(new Guitar(serialNr2, price2, spec1))
                inventory.addGuitar(new Guitar(serialNr3, price2, spec1))

        and: "guitar with price1 and other spec exist in inventory"
                inventory.addGuitar(new Guitar(otherSerialNr, price1, otherSpec))


        then: "three guitars should be retrieved for the matching spec1"
                List<Guitar> matchingGuitars = inventory.search(null, null, spec1)
                matchingGuitars.size() == 3

        and: "one guitar should be retrieved with unique serialNr1"
                matchingGuitars = inventory.search(serialNr1, null, null)
                matchingGuitars.size() == 1

        and: "one guitar should be retrieved with price1 and spec1"
                matchingGuitars = inventory.search(null, price1, spec1)
                matchingGuitars.size() == 1

        and: "two guitars should be retrieved with price2 and spec1"
                matchingGuitars = inventory.search(null, price2, spec1)
                matchingGuitars.size() == 2

        and: "four acoustic guitars should be retrieved for partial spec:
type = acutical"
                matchingGuitars = inventory.search(null, null, partialSpec)
                matchingGuitars.size() == 4


        where: "guitar properties serialNo, price and spec are defined
as ..."
                price1 = 100
                price2 = 200

                serialNr1 = 111
                serialNr2 = 222
                serialNr3 = 333

                spec1 = new InstrumentSpec(Builder.COLLINGS, "CJ", Type.ACOUSTIC,
Wood.INDIAN_ROSEWOOD, Wood.SITKA, 6)

        otherSerialNr = 444
                otherSpec = new InstrumentSpec(Builder.FENDER, "JP", Type.ACOUSTIC,
Wood.ALDER, Wood.ADIRONDACK, 4)

        // add partial spec matches, i.e. all acustic guitars
        partialSpec = new InstrumentSpec(null, null, Type.ACOUSTIC,
null, null, null )

        }
}

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Spock Framework" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.de/group/spockframework?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: how best to organize test stories with common setup?

Peter Niederwieser
Administrator

 From what I can see in the code, you could remove the where-block and  
move the assignments to the field level:

class SearchingInventory {
   def price1 = 100
   def price2 = 200
   def spec1 = new InstrumentSpec(Builder.COLLINGS, "CJ",  
Type.ACOUSTIC, Wood.INDIAN_ROSEWOOD, Wood.SITKA, 6)
   // etc.

   def "scenario 1"() {}

   def "scenario 2"() {}

   def "scenario 3"() {}
}

setup() and setupSpeck() are only needed for more complex  
initialization. setup() is called before each method (scenario),  
setupSpeck() only before the first method. Unless your scenarios need  
to communicate through fields or you create very expensive objects  
(e.g. a database connection), setup() is preferable (isolation  
principle). Note that setupSpeck() may only read and write @Shared  
fields (see documentation for more information).

If the class gets too big, consider splitting it up into multiple  
independent classes. Here you can leverage the fact that Groovy allows  
multiple public classes per file.

Hope this helps.

Cheers,
Peter

On Aug 14, 2009, at 12:13 PM, Frank Weber wrote:

>
>
> I started with a single story and extended it along the way.
> Now I would like to reorganize it differently.
> I plan to split the attached file via separate def "..." in one test
> class
>
> So via setupSpeck() these stories can share the same setup, right?
> Or what would you recommend?
>
> Regards
>
> Frank
>
>
> class SearchingInventory {
>
> // setupSpeck() {
> // move stuff over from the the where: part
> // }
>
> def "retrieving Guitars from Inventory"() {
>
> when: "guitar with spec1 and price1 exist in Inventory"
> Inventory inventory = new Inventory()
> inventory.addGuitar(new Guitar(serialNr1, price1, spec1))
>
> and: "two guitars with spec1 and price2 exist in Inventory"
> inventory.addGuitar(new Guitar(serialNr2, price2, spec1))
> inventory.addGuitar(new Guitar(serialNr3, price2, spec1))
>
> and: "guitar with price1 and other spec exist in inventory"
> inventory.addGuitar(new Guitar(otherSerialNr, price1, otherSpec))
>
>
> then: "three guitars should be retrieved for the matching spec1"
> List<Guitar> matchingGuitars = inventory.search(null, null, spec1)
> matchingGuitars.size() == 3
>
> and: "one guitar should be retrieved with unique serialNr1"
> matchingGuitars = inventory.search(serialNr1, null, null)
> matchingGuitars.size() == 1
>
> and: "one guitar should be retrieved with price1 and spec1"
> matchingGuitars = inventory.search(null, price1, spec1)
> matchingGuitars.size() == 1
>
> and: "two guitars should be retrieved with price2 and spec1"
> matchingGuitars = inventory.search(null, price2, spec1)
> matchingGuitars.size() == 2
>
> and: "four acoustic guitars should be retrieved for partial spec:
> type = acutical"
> matchingGuitars = inventory.search(null, null, partialSpec)
> matchingGuitars.size() == 4
>
>
> where: "guitar properties serialNo, price and spec are defined
> as ..."
> price1 = 100
> price2 = 200
>
> serialNr1 = 111
> serialNr2 = 222
> serialNr3 = 333
>
> spec1 = new InstrumentSpec(Builder.COLLINGS, "CJ", Type.ACOUSTIC,
> Wood.INDIAN_ROSEWOOD, Wood.SITKA, 6)
>
>        otherSerialNr = 444
> otherSpec = new InstrumentSpec(Builder.FENDER, "JP", Type.ACOUSTIC,
> Wood.ALDER, Wood.ADIRONDACK, 4)
>
>        // add partial spec matches, i.e. all acustic guitars
>        partialSpec = new InstrumentSpec(null, null, Type.ACOUSTIC,
> null, null, null )
>
> }
> }
>
> >


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Spock Framework" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.de/group/spockframework?hl=en
-~----------~----~----~----~------~----~------~--~---