Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [TestFixture]
- public class SuccessTests
- {
- [SetUp] public void Init()
- { /* Load test data */ }
- }
- func TestMain(m *testing.M) {
- mySetupFunction()
- retCode := m.Run()
- myTeardownFunction()
- os.Exit(retCode)
- }
- // package_test.go
- package main
- func init() {
- /* load test data */
- }
- package math
- func Sum(a, b int) int {
- return a + b
- }
- 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)
- }
- })
- }
- }
- % go test -v
- === RUN TestAddition
- === RUN TestAddition/add
- === RUN TestAddition/minus
- === RUN TestAddition/zero
- --- PASS: TestAddition (0.00s)
- math_test.go:6: setup test case
- --- PASS: TestAddition/add (0.00s)
- math_test.go:13: setup sub test
- math_test.go:15: teardown sub test
- --- PASS: TestAddition/minus (0.00s)
- math_test.go:13: setup sub test
- math_test.go:15: teardown sub test
- --- PASS: TestAddition/zero (0.00s)
- math_test.go:13: setup sub test
- math_test.go:15: teardown sub test
- math_test.go:8: teardown test case
- PASS
- ok github.com/kare/go-unit-test-setup-teardown 0.010s
- %
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement