Coder Social home page Coder Social logo

godogx / expandvars Goto Github PK

View Code? Open in Web Editor NEW
0.0 0.0 0.0 70 KB

Expand variables in cucumber/godog tests

License: MIT License

Makefile 8.44% Go 78.91% Gherkin 12.65%
testing go golang bdd integration-testing gherkin cucumber functional-testing godog godog-extension

expandvars's Introduction

Variables Expander for Cucumber Steps

GitHub Releases Build Status codecov Go Report Card GoDevDoc

A lifesaver expander for cucumber/godog because, sometimes, you have to use variables in your steps.

Prerequisites

  • Go >= 1.16

Install

go get github.com/godogx/expandvars

Usage

Initiate a new StepExpander with expandvars.NewStepExpander() then add it to ScenarioInitializer by calling StepExpander.RegisterContext(*testing.T, *godog.ScenarioContext)

package main

import (
    "fmt"
    "math/rand"
    "strings"
    "testing"

    "github.com/cucumber/godog"
    "github.com/godogx/expandvars"
    "github.com/stretchr/testify/assert"
)

func TestIntegration(t *testing.T) {
    expander := expandvars.NewStepExpander(
        strings.NewReplacer("$TO", "Berlin"),
        expandvars.Pairs{
            "HUSBAND": "John",
        },
        func() expandvars.Pairs {
            return expandvars.Pairs{
                "RAND": fmt.Sprintf("%d", rand.Int63()),
            }
        },
        expandvars.BeforeScenario(func() expandvars.Pairs {
            return expandvars.Pairs{
                "SCENARIO_RAND": fmt.Sprintf("%d", rand.Int63()),
            }
        }),
        func(s string) string {
            return strings.ReplaceAll(s, "$FROM", "Paris")
        },
        expandvars.Expander(func(s string) string {
            return strings.ReplaceAll(s, "$TRANSPORT", "by bus")
        }),
        // OS env vars.
        expandvars.EnvExpander,
    )

    suite := godog.TestSuite{
        Name: "Integration",
        ScenarioInitializer: func(ctx *godog.ScenarioContext) {
            expander.RegisterContext(ctx)
        },
        Options: &godog.Options{
            Strict:    true,
            Randomize: rand.Int63(),
        },
    }

    // Run the suite.
}

In your tests, just use $VARIABLE_NAME in the step or the argument, like this:

    Scenario: var is replaced
        Given var NAME is replaced in step text: $NAME

        Then step text is:
        """
        map var NAME is replaced in step text: John
        """

        Given var NAME is replaced in step argument (string)
        """
        NAME=$NAME
        """

        Then step argument is a string:
        """
        NAME=John
        """

        Given env var NAME is replaced in step argument (table)
            | col 1   | col 2 | col 3   |
            | value 1 | $NAME | value 3 |

        Then step argument is a table:
            | col 1   | col 2 | col 3   |
            | value 1 | John  | value 3 |
    Scenario: .github files
        Then there should be only these files in "$TEST_DIR/.github":
        """
        - workflows:
            - golangci-lint.yaml
            - test.yaml
        """

Expanders

The expanders could be any of these:

  1. A Replacer interface
type Replacer interface {
    Replace(string) string
}
  1. A Replacer func(string) string function.

    For example, you could use os.ExpandEnv or its alias expandvars.EnvExpander

  2. A map or vars (without the $) map[string]string

var _ = expandvars.NewStepExpander(expandvars.Pairs{
    "HUSBAND": "John",
    "WIFE":    "Jane",
})
  1. A provider that provides a map of vars (without the $) map[string]string. The provider will be called every step.
var _ = expandvars.NewStepExpander(func() expandvars.Pairs {
    return map[string]string{
        "RAND": fmt.Sprintf("%d", rand.Int63()),
    }
})
  1. A BeforeScenario provides a map of vars (without the $) map[string]string. The provider will be called only once before every scenario.

Note: If you need expandvars.EnvExpander or os.ExpandEnv, put it in the end of the chain. Because it replaces not-found vars with empty strings, other expanders won't have a chance to do their jobs if you put it in the beginning.

expandvars's People

Contributors

dependabot[bot] avatar nhatthm avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.