gotestyourself / gotestsum

'go test' runner with output optimized for humans, JUnit XML for CI integration, and a summary of the test results.
Apache License 2.0
2.03k stars 119 forks source link

Add env file support #39

Open sagikazarmark opened 5 years ago

sagikazarmark commented 5 years ago

It would be nice to add an option for importing environment variables from an env file.

In integration tests it's a common pattern to use tokens and access keys from the environment and skip tests when they are missing. In local development environments this is commonly solved by using env files.

Although it's relatively easy to use godotenv for that, it adds an extra dependency, hence more complexity to the environment, so I think it would be a good fit for gotestsum.

dnephin commented 5 years ago

Support for setting environment variables for the test run is an interesting idea. There are already a few options for solving this problem that seem to work pretty well:

Do you know if there is a standard or a spec that documents the format of these files? Is there a library that implements parsing an env file that is well supported? I wouldn't want to implement an env file parser in gotestsum.

I also wonder if env file is the right thing to support. If the goal is to run tests directly with gotestsum with minimal arguments then maybe a config file that allows setting env vars, args to go test, and any gotestsum flags from the config would be more appropriate.

sagikazarmark commented 5 years ago

Is there a library that implements parsing an env file that is well supported?

Well, godotenv is actually a library as well, it only happens to provide a binary as well. The original dotenv implementation is from Ruby IIRC.

I also wonder if env file is the right thing to support.

A config file might work as well, but that locks you in on gotestsum. Env file is still somewhat universal. That being said, I would be happy with a gotestsum specific config file as well. (gotestsum could also accept -e flags, like docker commands for quick environment variable support)

mpictor commented 5 years ago

I wouldn't want to implement an env file parser in gotestsum.

The env files I'm familiar with (jenkins) are just java properties files. If that's the format you're talking about, it's quite simple - each line is VAR=value, so you can split on newlines and assign to exec.Command.Env - that's just a []string, same as os.Environ() returns.

bjwrk commented 1 year ago

We inherited some tests that use env vars too, the Go test cache is a real trap for this scenario. We're already using godotenv, but the footgun for us is the test cache. It's not practical for us to rearchitect our test cases at this stage, so we always need to remember to pass -count=1, which people often forget, or didn't know about in the first place.

tanzyy96 commented 5 months ago

What has seemed to work for me is godotenv -f .env gotestsum ./.... So you can programmatically swap out the env files for different environments, and also set certain tests to skip if the api keys aren't provided.