Skip to content

How to use failpoint in go test #85

Description

@zgcbj
[root@xxx]# cat main_test.go
package main

import (
        "testing"
"github.com/pingcap/failpoint"
)

func TestGetValue(t *testing.T) {
    failpoint.Enable("main/failpoint-name","return(4)")
        t.Log("result:", GetValue())
}
[root@xxx]# cat main.go
package main

import (
        "fmt"
        "github.com/pingcap/failpoint"
)

func GetValue() int {
        if val, _err_ := failpoint.Eval(_curpkg_("failpoint-name")); _err_ == nil {
                return val.(int)
        }

        return 1
}
func main() {

        fmt.Println("Hello world ", GetValue())
}
[root@xxx]# GOCACHE=/tmp/failpoint-cache  GO_FAILPOINTS="main/failpoint-name=return(4)" CGO_ENABLED=1 GO111MODULE=on go test-v -toolexec /usr/bin/failpoint-toolexec main_test.go main.go binding__failpoint_binding__.go
=== RUN   TestGetValue
    main_test.go:10: result: 1
--- PASS: TestGetValue (0.00s)
PASS
ok      command-line-arguments  0.006s

expect:

[root@xxx]# GOCACHE=/tmp/failpoint-cache  GO_FAILPOINTS="main/failpoint-name=return(4)" CGO_ENABLED=1 GO111MODULE=on go test-v -toolexec /usr/bin/failpoint-toolexec main_test.go main.go binding__failpoint_binding__.go
=== RUN   TestGetValue
    main_test.go:10: result: 4
--- PASS: TestGetValue (0.00s)
PASS

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions