samedi 24 septembre 2016

Using Golang TestMain on a mutiple folder project

My project folder is something like this.

$ tree src/backend/

src/backend/
├── cats
│   ├── cat_test.go
│   ├── handler.go
│   ├── routes.go
│   └── tools.go
├── cmd
│   ├── populate.go
│   ├── root.go
│   └── runserver.go
├── iot
│   └── device
│      ├── all.go
│      ├── all_test.go
│      ├── router.go
│      ├── types.go
│      └── update.go
├── main.go
├── main_test.go
└── root_test.go

On main_test.go I start the httptest server

func TestMain(m *testing.M) {
    setup()
    exitVal := m.Run()
    teardown()
    os.Exit(exitVal)
}

func setup() {
    flag.StringVar(&util.ServerFlag, "server", "s", "server to set request for")
    flag.Parse()
    viper.SetDefault("testing", true)
    util.BasicStart()
    iot.RunDependencies()
    cats.SyncS3()
    router := cmd.InitRootRouter()
    util.SetTestServer(httptest.NewServer(router))
}

func teardown() {
    log.Println("[TESTS] - Teardown completed")

}

All the tests on root_test.gowork perfctly when I run go test -v ./src/backend/.... But the test at src/backend/cat_test.go fail due to the fact that the teardown method has already been called by the time they run. How can I make the server wait for all test to be run before shutting down?

Aucun commentaire:

Enregistrer un commentaire