Skip to content

godogx/vars

Folders and files

NameName
Last commit message
Last commit date

Latest commit

02ea3ed · Jul 4, 2022

History

3 Commits
Jul 1, 2022
Jul 4, 2022
May 16, 2022
Jul 1, 2022
Jul 1, 2022
Jul 1, 2022
Jul 1, 2022
Jul 1, 2022
Jul 1, 2022
Jul 1, 2022
Jul 1, 2022
Jul 4, 2022
Jul 1, 2022

Repository files navigation

vars

Build Status Coverage Status GoDevDoc Time Tracker Code lines Comments

This library provides godog step definitions to manage variables shared between steps and API for other libraries.

Usage

Register steps to godog scenario context.

vs := vars.Steps{}

suite := godog.TestSuite{}
suite.ScenarioInitializer = func(s *godog.ScenarioContext) {
    vs.Register(s)
    
    // Other configuration and step definitions.
}

Use steps in feature files.

Feature: Variables

  Scenario: Setting and asserting variables
    # Assert variable have not been set.
    # Every variable name starts with var prefix, "$" by default.
    Given variable $foo is undefined
    # Set assign value to variable.
    # Every value is declared as JSON.
    When variable $foo is set to "abcdef"
    # Assert current value of variable.
    Then variable $foo equals to "abcdef"

    # Set values to multiple variables.
    # Values are decoded into `any` with JSON decoder.
    # Beware that both integers and floats will be decoded as `float64`.
    When variables are set to values
      | $bar  | "abc"             |
      | $baz  | {"one":1,"two":2} |
      | $qux  | 123               |
      | $quux | true              |
    # Assert current values of multiple variables.
    Then variables are equal to values
      | $bar  | "abc"             |
      | $baz  | {"one":1,"two":2} |
      | $qux  | 123               |
      | $quux | true              |

Libraries can pass variables using context. For example httpsteps can set variable from API response and then dbsteps can use that value to query database.

// Libraries, that are unaware of each other, can use vars to communicate general state between themselves.
s.Step("^I do foo$", func(ctx context.Context) context.Context {
    return vars.ToContext(ctx, "$fooDone", true)
})

s.Step("^foo is done$", func(ctx context.Context) error {
    if done, ok := vars.FromContext(ctx)["$fooDone"]; ok {
        if b, ok := done.(bool); ok && b {
            return nil
        }
    }

    return errors.New("foo is not done")
})
    # Use vars in custom steps.
    When I do foo
    Then foo is done