mardi 5 février 2019

How to do a setup that create state for the tests?

I want to do something like this:

func TestMain(m *testing.M) {
    foo := 42

    exitCode := m.Run(foo) // realize the value foo that i'm trying to pass to the tests
    os.Exit(exitCode)
}

func TestFoo(t *testing.T, foo int) {
    assert.Equal(t, 43, foo+1)
}

func TestBar(t *testing.T, foo int) {
    assert.Equal(t, 44, foo+2)
}

I'm trying to do this because I have a heavy setup that right now I do for every single test, so the package test suite is taking too long. If I could make this setup just once per test file, it would help a lot.

Obs: the solution doesn't have to be the proposed above. I just wanted to show the intent.

In the xUnit frameworks I would define a class and create a state with the setupBeforeClass() static method.

Aucun commentaire:

Enregistrer un commentaire