Created
November 22, 2016 17:53
-
-
Save kare/ec85d88b9b5f975936b67cd1bebdd621 to your computer and use it in GitHub Desktop.
Go unit test setup and teardown math_test.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 math | |
import "testing" | |
func setupTestCase(t *testing.T) func(t *testing.T) { | |
t.Log("setup test case") | |
return func(t *testing.T) { | |
t.Log("teardown test case") | |
} | |
} | |
func setupSubTest(t *testing.T) func(t *testing.T) { | |
t.Log("setup sub test") | |
return func(t *testing.T) { | |
t.Log("teardown sub test") | |
} | |
} | |
func TestAddition(t *testing.T) { | |
cases := []struct { | |
name string | |
a int | |
b int | |
expected int | |
}{ | |
{"add", 2, 2, 4}, | |
{"minus", 0, -2, -2}, | |
{"zero", 0, 0, 0}, | |
} | |
teardownTestCase := setupTestCase(t) | |
defer teardownTestCase(t) | |
for _, tc := range cases { | |
t.Run(tc.name, func(t *testing.T) { | |
teardownSubTest := setupSubTest(t) | |
defer teardownSubTest(t) | |
result := Sum(tc.a, tc.b) | |
if result != tc.expected { | |
t.Fatalf("expected sum %v, but got %v", tc.expected, result) | |
} | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment