Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. [TestFixture]
  2. public class SuccessTests
  3. {
  4. [SetUp] public void Init()
  5. { /* Load test data */ }
  6. }
  7.  
  8. func TestMain(m *testing.M) {
  9. mySetupFunction()
  10. retCode := m.Run()
  11. myTeardownFunction()
  12. os.Exit(retCode)
  13. }
  14.  
  15. // package_test.go
  16. package main
  17.  
  18. func init() {
  19. /* load test data */
  20. }
  21.  
  22. package math
  23.  
  24. func Sum(a, b int) int {
  25. return a + b
  26. }
  27.  
  28. package math
  29.  
  30. import "testing"
  31.  
  32. func setupTestCase(t *testing.T) func(t *testing.T) {
  33. t.Log("setup test case")
  34. return func(t *testing.T) {
  35. t.Log("teardown test case")
  36. }
  37. }
  38.  
  39. func setupSubTest(t *testing.T) func(t *testing.T) {
  40. t.Log("setup sub test")
  41. return func(t *testing.T) {
  42. t.Log("teardown sub test")
  43. }
  44. }
  45.  
  46. func TestAddition(t *testing.T) {
  47. cases := []struct {
  48. name string
  49. a int
  50. b int
  51. expected int
  52. }{
  53. {"add", 2, 2, 4},
  54. {"minus", 0, -2, -2},
  55. {"zero", 0, 0, 0},
  56. }
  57.  
  58. teardownTestCase := setupTestCase(t)
  59. defer teardownTestCase(t)
  60.  
  61. for _, tc := range cases {
  62. t.Run(tc.name, func(t *testing.T) {
  63. teardownSubTest := setupSubTest(t)
  64. defer teardownSubTest(t)
  65.  
  66. result := Sum(tc.a, tc.b)
  67. if result != tc.expected {
  68. t.Fatalf("expected sum %v, but got %v", tc.expected, result)
  69. }
  70. })
  71. }
  72. }
  73.  
  74. % go test -v
  75. === RUN TestAddition
  76. === RUN TestAddition/add
  77. === RUN TestAddition/minus
  78. === RUN TestAddition/zero
  79. --- PASS: TestAddition (0.00s)
  80. math_test.go:6: setup test case
  81. --- PASS: TestAddition/add (0.00s)
  82. math_test.go:13: setup sub test
  83. math_test.go:15: teardown sub test
  84. --- PASS: TestAddition/minus (0.00s)
  85. math_test.go:13: setup sub test
  86. math_test.go:15: teardown sub test
  87. --- PASS: TestAddition/zero (0.00s)
  88. math_test.go:13: setup sub test
  89. math_test.go:15: teardown sub test
  90. math_test.go:8: teardown test case
  91. PASS
  92. ok github.com/kare/go-unit-test-setup-teardown 0.010s
  93. %
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement