Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testing: TestMain not working sequentially #54118

Closed
pjebs opened this issue Jul 28, 2022 · 1 comment
Closed

testing: TestMain not working sequentially #54118

pjebs opened this issue Jul 28, 2022 · 1 comment

Comments

@pjebs
Copy link
Contributor

pjebs commented Jul 28, 2022

Package Structure:

/pkg
       x_test.go
/pkg/sub-package
       y_test.go

x_test.go

import "testing"
import "github.com/ory/dockertest"

func TestMain(m *testing.M) {
    pool, _ := dockertest.NewPool("")
    resource, _ := pool.Run("mysql", "5.7", []string{"MYSQL_ROOT_PASSWORD=secret"}) // Create and run container
    defer pool.Purge(resource) // Kill container
    
    m.Run()
}

func TestXXX(t *testing.T) {
// Do some tests using mysql container running
}

y_test.go

import "testing"
import "github.com/ory/dockertest"

func TestMain(m *testing.M) {
    pool, _ := dockertest.NewPool("")
    resource, _ := pool.Run("mysql", "5.7", []string{"MYSQL_ROOT_PASSWORD=secret"}) // Create and run container
    defer pool.Purge(resource) // Kill container
    
    m.Run()
}

func TestYYY(t *testing.T) {
// Do some tests using mysql container running
}

If I run the tests in each directory separately, it works perfectly:

  1. It creates mysql container.
  2. It Runs the tests.
  3. It kills the docker container
  4. Tests end and test results returned

If however I run go test -v ./... from the parent directory:

The tests error out at pool.Run saying that mysql container already exists.

I believe the correct behaviour should be that TestMain should be run independently and sequentially. Only After outer TestMain returns should the inner packages be tested.

That way the containers are already killed before the inner packages are tested, giving the opportunity for the inner package to create their own container (and cleanup themself)

@pjebs pjebs changed the title testing: TestMain not working properly testing: TestMain not working sequentially Jul 28, 2022
@bcmills
Copy link
Contributor

bcmills commented Jul 28, 2022

This is working as documented. Per https://pkg.go.dev/cmd/go#hdr-Testing_flags:

The 'go test' command may run tests for different packages
in parallel as well, according to the setting of the -p flag
(see 'go help build').

You can use the -p flag to control the number of processes that go test runs in parallel (see https://pkg.go.dev/cmd/go#hdr-Compile_packages_and_dependencies), so -p 1 would run the tests sequentially.

That said, it is almost always preferable to structure the tests such that each invocation of the test binary works with its own (hermetic) resources.

@bcmills bcmills closed this as not planned Won't fix, can't repro, duplicate, stale Jul 28, 2022
@golang golang locked and limited conversation to collaborators Jul 28, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants