As an extension of the FAQ, this document answers some important questionsabout the execution model of GoConvey, like:

    • How do I define a "Setup" method to be run before each test?
    • Why don't my nested tests run in sequential order?
      These questions, surprisingly, are related. Here's an eloquent explanationfrom one of GoConvey's usersthat sheds light on these questions:
    Consider, for example, the pseudocode:
    1. Convey A
    2. So 1
    3. Convey B
    4. So 2
    5. Convey C
    6. So 3
    I Initially thought would execute as A1B2C3, in other words, sequentially.As you all know, this is actually executed first as A1B2 and then A1C3.Once I realized this, I actually realized the power of goconvey, becauseit allows you two write n tests with log(n) statements. This "tree-based"behavioral testing eliminates so much duplicated setup code and is so mucheasier to read for completeness (versus pages of unit tests) while stillallowing for very well isolated tests (for each branch in the tree).

    In the psuedo code above, Convey A serves as a "Setup" method for Convey Band Convey C and is run separately for each. Here's a more complex example:

    1. Convey A
    2. So 1
    3. Convey B
    4. So 2
    5. Convey Q
    6. So 9
    7. Convey C
    8. So 3

    Can you guess what the output would be?

    A1B2Q9A1C3 is the correct answer.

    You're welcome to peruse the tests in the GoConvey project itself that document this behavior.

    Gotchas

    Remember that every Convey() call in Go creates a new scope. You should use Foo = &Bar{} in order to assign a new value to a previous declared variable. Using foo := &Bar{} creates a new variable in the current scope. Example:

    1. Convey("Setup", func() {
    2. foo := &Bar{}
    3. Convey("This creates a new variable foo in this scope", func() {
    4. foo := &Bar{}
    5. }
    6. Convey("This assigns a new value to the previous declared foo", func() {
    7. foo = &Bar{}
    8. }
    9. }

    If you're wondering about how to achieve "tear-down" functionality, see the page on the Reset function.