mercredi 10 juin 2020

How testing.Run() ensure that sub tests complete before outer test function returns?

Second subtest in the below test is a failing test:

func TestHello(t *testing.T) {
    t.Run("saying hello to people", func(t *testing.T) {
        got := Hello("Chris")
        want := "Hello, Chris"

        if got != want {
            t.Errorf("got %q want %q", got, want)
        } else {
            fmt.Println("test 1 completed")
        }
    })

    t.Run("say 'Hello, world' when an empty string is supplied", func(t *testing.T) {
        got := Hello("")
        want := "Hello, World"

        if got != want {
            t.Errorf("got %q want %q .", got, want)
        } else {
            fmt.Println("test 2 completed")
        }
    })
    fmt.Println("Two tests completed")
}

So, the output is:

$ go test github.com/myhub/cs61a/hello/v5
test 1 completed
Two tests completed
--- FAIL: TestHello (0.00s)
    --- FAIL: TestHello/say_'Hello,_world'_when_an_empty_string_is_supplied (0.00s)
        hello_test.go:25: got "Hello, " want "Hello, World" .
FAIL
FAIL    github.com/myhub/cs61a/hello/v5       0.002s
FAIL
$
$

Based on above output, outer test(TestHello()) prints Two tests completed before second test failure is printed on stdout.

So, my understanding is, t.Run() did not ensure that sub tests(go routines) gets processed before outer test(TestHello()) completes.


Does t.Run() ensure that sub tests(on goroutine) complete before outer test(TestHello) completes?

Aucun commentaire:

Enregistrer un commentaire