cucumber / godog

Cucumber for golang
MIT License
2.31k stars 252 forks source link

Environment variables #15

Closed xlucas closed 8 years ago

xlucas commented 8 years ago

Firsteval, congrats for the good work ! Great project !

I'm currently wondering how are environment variables supported. Typically say we have an authentication feature, and two scenari in it (authentication successfull and authentication failure) but It would be preferable to inject credentials from envrironment for the valid authentication scenario. What would be the best way to do this with godog ?

l3pp4rd commented 8 years ago

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..

xlucas commented 8 years ago

Totally makes sense, thanks alot for the details !