0

I need to run my GoConvey tests as part of my build how do i make sure go test exits with error exit code (not 0)?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
danfromisrael
  • 2,982
  • 3
  • 30
  • 40
  • GoConvey supports Go's native testing package. Neither the web UI nor the DSL are required; you can use either one independently. – Bert Verhees Jul 09 '18 at 12:29
  • @Bert Verhees i run go test, see a failing test but get exit code 0... – danfromisrael Jul 09 '18 at 12:35
  • 1
    `go test` should exit with code 1 on test failures, [see this](https://github.com/golang/go/blob/master/src/testing/testing.go#L1040-L1046), and that value is used [here](https://github.com/golang/go/blob/master/src/cmd/go/go_test.go#L212-L218). For various initialisation errors it seems to use exit code 2. It would be helpful if you could provide a [mcve] which demonstrates the problem. – Martin Tournoij Jul 09 '18 at 20:05

2 Answers2

2

Ok found the bug:

I have TestMain in my code that runs before the tests:

func TestMain(m *testing.M) {
    log.SetOutput(ioutil.Discard) // Disable logs.
    m.Run()
}

m.Run should be wrapped with os.Exit so it can be used with error status codes:

func TestMain(m *testing.M) {
    log.SetOutput(ioutil.Discard) // Disable logs.
    os.Exit(m.Run())
}

that fixed the problem

danfromisrael
  • 2,982
  • 3
  • 30
  • 40
1

You can run goconvey as a normal test, you can test this very simply by writing a small test which gives a predictable result, like this

package c

import (
    "testing"
    . "github.com/smartystreets/goconvey/convey"
)

func TestConvey(t *testing.T){
    Convey("TestConvey", t, func() {
        So(false, ShouldBeTrue)
    })
}

This should be the result xxx 14:44:03 ~/GO/src/c/c > go test x Failures:

* /home/xxx/GO/src/c/c/a_test.go 
Line 10:
Expected: true
Actual:   false


1 total assertion

--- FAIL: TestConvey (0.00s)
FAIL
exit status 1
FAIL    c/c     0.006s
Bert Verhees
  • 1,057
  • 3
  • 14
  • 25