lundi 22 mai 2017

Go testing customization with testing.TB

I'm trying to customize the testing.T with my own assert method to lower the number of lines I'm writing. I tried the following, ended with an error: "wrong signature for TestCustom, must be: func TestCustom(t *testing.T)".

How can I make TestCustom use CustomTester interface with a new method, assert?

I don't want to use a 3rd-party framework.

custom_testing.go

type CustomTester struct {
        testing.TB
}

func (t *CustomTester) assert(exp interface{}, act interface{}) {
        if exp != act {
                t.Errorf("expected: %v. got: %v\n", exp, act)
        }
}

func TestCustom(t *testing.TB) {
        t.assert(3, len(foo))
}


NOTE: I also tried this, it works but, I don't want to pass t each time when I'm testing:

working_not_wanted.go

func assert(t *testing.TB, exp interface{}, act interface{}) {
        if exp != act {
                t.Errorf("expected: %v. got: %v\n", exp, act)
        }
}

func TestCustom(t *testing.T) {
        assert(t, 3, len(foo))
}

Aucun commentaire:

Enregistrer un commentaire