How do you handle setting up an environment for integration tests?

I’m working on a program which interacts with Consul. Consul is relatively simple to run locally, it’s just a static binary with minimal config necessary, so I was considering launching an instance when running tests, to allow tests to pass that interact with the API.

Thoughts? how do you normally setup an environment for integration tests?

I use environment variables.
You can write a function to read the environment vars.

   func checkEnv(t *testing.T) {
        var (
            EnvVar = os.Getenv("ENV_VAR")
            EnvVar2 = os.Getenv("ENV_VAR2")
        )
        if EnvVar == "" || EnvVar2 == "" {
            t.Skip("Skipping test because env variables are not set")
        }
    }

If the environment is more complex You can write a loadEnv(t) to return a struct if necessary.

What I’m going to go with is using build tags, as suggested in this post (in “Testing and Validation”). Basically I’ll create files containing integration tests using a format such as something_integration_test.go. At the top of those files I’ll have // +build integration.

I can then run all tests using: go test -tags=integration ./... or I can exclude integration tests by just running go test ./...

I am using a Makefile to run my tests which will setup and launch Consul (an external service needed by some tests):

consul:
  @yumdownloader consul
  @rpm2cpio consul-0.5.2.rpm | cpio -idmv
  @rm -rf /tmp/{tmp,raft,serf}
  @./consul/consul agent -server -bootstrap --data-dir=/tmp --ui-dir=consul/dist &
  @sleep 5

itest: consul
  @go test -tags=integration ./...
1 Like

Sounds good.
Also Consul has a package You might find helpful: testutil

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.