Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Go1.24時代の ユニットテスト品質向上

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
Avatar for shotaro watanabe shotaro watanabe
September 27, 2025
400

Go1.24時代の ユニットテスト品質向上

Go Conference2025 2日目に登壇した際の資料です。

Avatar for shotaro watanabe

shotaro watanabe

September 27, 2025
Tweet

Transcript

  1. ユニットテストが信頼できなかった話② - goroutineリークでCIが不安定 テスト1 OK テスト2 OK テスト4 OK or

    Fail テスト3 Leak テスト4で エラーが出ている...? テスト1〜3のどこかで リークしている...? 5
  2. testing.T.Context() 15 t.Cleanup() の実行前 または テスト終了時に contextをキャンセルしてくれる 1. contextを含むテストが書きやすく &

    読みやすくなった 2. goroutineを扱う複数のテストを実行する場合に デバッグが楽になる
  3. ユニットテストが信頼できなかった話②(再掲) - goroutineリークでCIが不安定 テスト1 OK テスト2 OK テスト4 OK or

    Fail テスト3 Leak テスト4で エラーが出ている...? テスト1〜3のどこかで リークしている...? 16
  4. testing.T.Context() // 1. キャンセル可能なコンテキストを作成 ctx, cancel := context.WithCancel(context.Background()) // 2.

    テスト終了時にcancel()を呼びだす defer cancel() go doSomething(ctx) // 1. t.Context()を呼びだす ctx := t.Context() go doSomething(ctx) 17
  5. 実践指針・文化醸成 - ユニットテストのTipsを集めてチームに共有 - httptest, miniredis, synctest, … - 自分で書いてコードレビューを依頼する(叩き台があると書きやすい

    ) - 勉強会で共有 - まずは新規実装にだけ テストを書き、体力を付ける - 既存のコードにテストを書くよりも精神的に楽 (良い事例があれば教えてください!) 23