vendredi 22 janvier 2021

Go testing get caller line number

Example code:

package main

import "testing"

func TestOne(t *testing.T) {
    assertEq(t, "A", "B")
    assertEq(t, "B", "B")
    assertEq(t, "C", "X")
}

func TestTwo(t *testing.T) {
    assertEq(t, "A", "A")
    assertEq(t, "B", "B")
    assertEq(t, "C", "D")
}

func assertEq(t *testing.T, a, b string) {
    if a != b {
        t.Fatalf("%s != %s, in %s", a, b, t.Name())
    }
}

Output:

=== RUN   TestOne
    prog.go:19: A != B, in TestOne
--- FAIL: TestOne (0.00s)
=== RUN   TestTwo
    prog.go:19: C != D, in TestTwo
--- FAIL: TestTwo (0.00s)
FAIL

If I call assertEq() many times, it is difficult to find what is line where the test failed. Can I get the caller line number?

Aucun commentaire:

Enregistrer un commentaire