Created
April 27, 2023 01:01
-
-
Save kitlabcode/0bc69fb1702eda54d08b874570cb37a0 to your computer and use it in GitHub Desktop.
Setup and teardown unit test in go.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "log" | |
| "testing" | |
| ) | |
| func doubleMe(x float64) float64 { | |
| return x * 2 | |
| } | |
| // You can use testing.T, if you want to test the code without benchmarking | |
| func setupSuite(tb testing.TB) func(tb testing.TB) { | |
| log.Println("setup suite") | |
| // Return a function to teardown the test | |
| return func(tb testing.TB) { | |
| log.Println("teardown suite") | |
| } | |
| } | |
| // Almost the same as the above, but this one is for single test instead of collection of tests | |
| func setupTest(tb testing.TB) func(tb testing.TB) { | |
| log.Println("setup test") | |
| return func(tb testing.TB) { | |
| log.Println("teardown test") | |
| } | |
| } | |
| func TestDoubleMe(t *testing.T) { | |
| teardownSuite := setupSuite(t) | |
| defer teardownSuite(t) | |
| table := []struct { | |
| name string | |
| input float64 | |
| expected float64 | |
| }{ | |
| {"one", 1, 2}, | |
| {"minus one", -1, -2}, | |
| {"zero", 0, 0}, | |
| {"minus one hundred", -100, -200}, | |
| {"one hundred", 100, 200}, | |
| } | |
| for _, tc := range table { | |
| t.Run(tc.name, func(t *testing.T) { | |
| teardownTest := setupTest(t) | |
| defer teardownTest(t) | |
| actual := doubleMe(tc.input) | |
| if actual != tc.expected { | |
| t.Errorf("expected %f, got %f", tc.expected, actual) | |
| } | |
| }) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment