🐁

Go言語テーブル駆動テストのt.Parallel()

2022/12/26に公開約3,200字

はじめに

Go言語でテーブル駆動テストを実装したときに t.Parallel()をトップレベル関数で書くのとサブレベル関数で書くのとでどうやって挙動が変わるのかちゃちゃっと動かして見た結果の共有です。

動作環境

go version go1.19.4 linux/amd64
uname -a
Linux HP-Spectre-x360 5.15.79.1-microsoft-standard-WSL2 #1 SMP Wed Nov 23 01:01:46 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

コード

main.go

package main

import (
	"log"
	"time"
)

func main() {}

func Hello(v string) {
	time.Sleep(1000 * time.Millisecond)
	log.Printf("Hello %s\n", v)
}

main_test.go

func TestHello1(): トップレベル関数とサブレベル関数にt.Parallel()を記述
func TestHello2(): トップレベル関数にのみt.Parallel()を記述

package main

import (
	"fmt"
	"testing"
)

// トップレベル関数
func TestHello1(t *testing.T) {
	t.Parallel()

	type args struct {
		v string
	}

	tests := []struct {
		name string
		args args
	}{
		{
			name: "1",
			args: args{
				v: "1",
			},
		},
		{
			name: "2",
			args: args{
				v: "2",
			},
		},
		{
			name: "3",
			args: args{
				v: "3",
			},
		},
		{
			name: "4",
			args: args{
				v: "4",
			},
		},
		{
			name: "5",
			args: args{
				v: "5",
			},
		},
	}

	for _, tt := range tests {
		tt := tt
        // サブレベル関数
		t.Run(tt.name, func(t *testing.T) {
			t.Parallel()
			Hello(fmt.Sprintf("1-%s", tt.args.v))
		})
	}
}

// トップレベル関数
func TestHello2(t *testing.T) {
	t.Parallel()

	type args struct {
		v string
	}

	tests := []struct {
		name string
		args args
	}{
		{
			name: "1",
			args: args{
				v: "1",
			},
		},
		{
			name: "2",
			args: args{
				v: "2",
			},
		},
		{
			name: "3",
			args: args{
				v: "3",
			},
		},
		{
			name: "4",
			args: args{
				v: "4",
			},
		},
		{
			name: "5",
			args: args{
				v: "5",
			},
		},
	}

	for _, tt := range tests {
        // サブレベル関数
		t.Run(tt.name, func(t *testing.T) {
			Hello(fmt.Sprintf("2-%s", tt.args.v))
		})
	}
}

結果

go test ./... -v

=== RUN   TestHello1
=== PAUSE TestHello1
=== RUN   TestHello2
=== PAUSE TestHello2
=== CONT  TestHello1
=== RUN   TestHello1/1
=== PAUSE TestHello1/1
=== RUN   TestHello1/2
=== PAUSE TestHello1/2
=== RUN   TestHello1/3
=== PAUSE TestHello1/3
=== RUN   TestHello1/4
=== PAUSE TestHello1/4
=== RUN   TestHello1/5
=== PAUSE TestHello1/5
=== CONT  TestHello1/1
=== CONT  TestHello2
=== RUN   TestHello2/1
2022/12/27 00:55:29 Hello 2-1
=== RUN   TestHello2/2
2022/12/27 00:55:29 Hello 1-1
=== CONT  TestHello1/5
2022/12/27 00:55:30 Hello 1-5
=== CONT  TestHello1/4
2022/12/27 00:55:30 Hello 2-2
=== RUN   TestHello2/3
2022/12/27 00:55:31 Hello 1-4
=== CONT  TestHello1/3
2022/12/27 00:55:31 Hello 2-3
=== RUN   TestHello2/4
2022/12/27 00:55:32 Hello 1-3
=== CONT  TestHello1/2
2022/12/27 00:55:32 Hello 2-4
=== RUN   TestHello2/5
2022/12/27 00:55:33 Hello 1-2
--- PASS: TestHello1 (0.00s)
    --- PASS: TestHello1/1 (1.00s)
    --- PASS: TestHello1/5 (1.05s)
    --- PASS: TestHello1/4 (1.07s)
    --- PASS: TestHello1/3 (1.03s)
    --- PASS: TestHello1/2 (1.01s)
2022/12/27 00:55:33 Hello 2-5
--- PASS: TestHello2 (5.16s)
    --- PASS: TestHello2/1 (1.00s)
    --- PASS: TestHello2/2 (1.05s)
    --- PASS: TestHello2/3 (1.07s)
    --- PASS: TestHello2/4 (1.03s)
    --- PASS: TestHello2/5 (1.01s)
PASS
ok      goparalleltest  5.166s

おわりに

  • TestHello1TestHello2は平行で実行
  • TestHello1内は平行
  • TestHello2内は直列

となっているのではないでしょうか。。。

Discussion

ログインするとコメントできます