Giter Club home page Giter Club logo

Comments (2)

l3pp4rd avatar l3pp4rd commented on May 23, 2024

Hi, thanks ;)

It does not relate to godog and instead - is a design issue and you should manage your state and control it from tests the same as with godog. You can implement this in many ways, for example.

  1. in main func read used environment variables to config struct and in test case change it in before scenario hook. It would require to pass config values for functions which needs these parameters
  2. Another way, would be to export and unset these variables in after scenario hook.

The example for the second, which is easier but not better design solution, would be:

Feature: user authentication
  In order to access my private resources
  As an anonymous user
  I need to be able to authenticate

  Scenario: private resources are restricted from wrong credentials
    Given my credentials are "username" and "password"
    When I send "GET" request to "/v1/requires/auth"
    Then the response code should be 403

  Scenario: I should be able to access private resource with good credentials
    Given there is user "john" with password "secret"
    And my credentials are "john" and "secret"
    When I send "GET" request to "/v1/requires/auth"
    Then the response code should be 200

Where my credentials are step, could set environment variables:

func myCredentialsAre(user, passw string) error {
    os.Setenv("AUTH_USER", user)
    os.Setenv("AUTH_PASS", passw)
    return nil
}

Given there is user step, should allow to pass these credentials, some state in storage or memory to pass authentication with correct credentials.

After the scenario, you should unset the environment:

// s is godog suite
    s.AfterScenario(func(_ interface{}, _ error) {
        os.Unsetenv("AUTH_USER")
        os.Unsetenv("AUTH_PASS")
    })

Note, this solution won't work with concurrent tests. So better is to have something like configuration structure instead of os environment directly..

from godog.

xlucas avatar xlucas commented on May 23, 2024

Totally makes sense, thanks alot for the details !

from godog.

Related Issues (20)

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.