Closed nkbai closed 3 years ago
Some environment variables are required in unit/integration test, we recommend to use make test
or make gotest
. You can find more information in the Makefile
.
package testfail
import (
"errors"
"github.com/pingcap/failpoint"
"testing"
)
type ttt struct {
}
func (t ttt) Call() error{
failpoint.Inject("failpoint-test", func() {
failpoint.Return(nil)
})
return errors.New("must fail")
}
func TestCall(t*testing.T){
tt:=ttt{}
failpoint.Enable("failpoint-test","return(nil)")
err:=tt.Call()
if err!=nil{
t.Error(err)
}
}
how to make this test success? I cannot find any example ,tell me how to use this library.
@nkbai failpoint.Enable("failpoint-test","return(nil)")
means the failpoint failpoint-test
has been activited and the Call
will return nil
. I cannot understand your intentions.
but this test is failed, and tt.Call return error ("must fail")
You must use failpoint-ctl enable
to rewrite the source file.
@nkbai You can reference the TiDB project.
@nkbai for the first question, you can take a look at the target make test
and it actually expands to
GO111MODULE=on GO_FAILPOINTS="failpoint-env1=return(10);failpoint-env2=return(true)" GO_FAILPOINTS_HTTP=":23389" CGO_ENABLED=1 GO111MODULE=on go test -p 4 -v ./...
... for using failpoint
in your own project, yes you can take a look at how pingcap/tidb and tikv/client-go use it. The former uses a code generation way while the latter doesn't require code generation.
We should provide more document on its usage, though.
I'll close this issue as the document part is tracked by https://github.com/pingcap/failpoint/issues/29
Bug Report
go get github.com/pingcap/failpoint/ cd failpoint go test .