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

Add callbacks to be run after and before each test #318

Merged
merged 3 commits into from
Dec 17, 2016
Merged

Add callbacks to be run after and before each test #318

merged 3 commits into from
Dec 17, 2016

Conversation

esdrasbeleza
Copy link
Contributor

I need to store and analyse data about my tests: suite names, test names, and test duration. Since I can't do that directly using go test, I modified testify for that.

The following sample suite

type TestifySuite struct {
    suite.Suite

    debugFile *os.File
}

func (t *TestifySuite) SetupSuite() {
    t.debugFile, _ = os.Create("debug.log")
}

func (t *TestifySuite) TearDownSuite() {
    t.debugFile.Close()
}

func (t *TestifySuite) BeforeTest(suiteName, testName string, when time.Time) {
    data := fmt.Sprintf("%s - %s - Started at %s\n", suiteName, testName, when.Format(time.RFC1123))
    t.debugFile.WriteString(data)
}

func (t *TestifySuite) AfterTest(suiteName, testName string, when time.Time) {
    data := fmt.Sprintf("%s - %s - Finished at %s, passed: %t\n", suiteName, testName, when.Format(time.RFC1123), !t.T().Failed())
    t.debugFile.WriteString(data)
}

func (t *TestifySuite) Test_Something_OK() {
    assert.Equal(t.T(), 2, 1+1)
}

func (t *TestifySuite) Test_Something_Fail() {
    assert.Equal(t.T(), 2, 3)
}

func Test(t *testing.T) {
    testify := &TestifySuite{}
    suite.Run(t, testify)
}

will produce this log file:

TestifySuite - Test_Something_Fail - Started at Sun, 26 Jun 2016 18:14:33 BRT
TestifySuite - Test_Something_Fail - Finished at Sun, 26 Jun 2016 18:14:33 BRT, passed: false
TestifySuite - Test_Something_OK - Started at Sun, 26 Jun 2016 18:14:33 BRT
TestifySuite - Test_Something_OK - Finished at Sun, 26 Jun 2016 18:14:33 BRT, passed: true

We can use these functions to store test results in database, generate files for continuous integration etc.

@ernesto-jimenez
Copy link
Member

Hi @esdrasbeleza, sorry it took so long to reply. I've been very busy.

I would rather do this without sending the times. You can call time.Now() within the callback.

@esdrasbeleza
Copy link
Contributor Author

@ernesto-jimenez, I removed the when from callback signatures.

@ernesto-jimenez
Copy link
Member

@esdrasbeleza 👍 thanks!

@ernesto-jimenez ernesto-jimenez merged commit cd206b0 into stretchr:master Dec 17, 2016
jonnyreeves pushed a commit to jonnyreeves/testify that referenced this pull request Jan 5, 2017
Add callbacks to be run after and before each test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants