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

Integration test in Go

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
Avatar for linyows linyows
June 28, 2017

Integration test in Go

Geeks Who Drink in Fukuoka -Go Go Golang Edition!- in Nulab at june 28, 2017

Avatar for linyows

linyows

June 28, 2017

More Decks by linyows

Other Decks in Programming

Transcript

  1. ͳΔ΄Ͳʜ؆୯ʹ΍Ε·͢Ͷ func TestFooAPI(t *testing.T) { expectedBody, _ := ioutil.ReadFile(“./testdata/foo/get_res.json”) res,

    _ := http.Get(endpoint + “/foo”) gotBody, _ := ioutil.ReadALL(res.Body) res.Body.Close() if gotBody != expectedBody { t.Error(“FooAPI returns wrong body”) } }
  2. 5FTU.BJO͕͋Γ·ͯ͠ͶʜॳظԽͱޙॲཧ func TestMain(m *testing.M) { setup() retCode := m.Run() os.Exit(retCode)

    } func setup() { db, _ := sql.Open(“postgres”, connInfo) db.Exec("DELETE FROM foo”) db.Exec("INSERT INTO bar(… defer db.Close() } func teardown() { db.Exec("TRUNCATE TABLE foo CASCAD… defer db.Close() }
  3. ςετͷ౓ʹࣄલࣄޙॲཧΛ͢Δ func TestFoo(t *testing.T) { defer SetupTeardown(t)() . . .

    } func SetupTeardown(t *testing.T) func() { t.Log(“Setup”) return func() { t.Log(“Teardown”) } }
  4. ͨͩ͠ɺ͢΂ͯͷςετʹ෼ذ͕ඞཁ var integration = flag.Bool(“integration”, false, “run integration test”) func

    TestMain(m *testing.M) { flag.Parse() if *integration { setup() } retCode := m.Run() if *integration { teardown() } os.Exit(retCode) } func TestIntegration1(t *testing.T) { if !*integration { t.Skip() } . . .