-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhello_world_test.go
106 lines (96 loc) · 3.9 KB
/
hello_world_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package BelajarGolangUnitTest
import (
"fmt"
"github.com/akhtarfath/GolangUnitTestwBenchmark/helper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"runtime"
"testing"
)
// in unit test, don't using panic, use t.Error / t.Fail / t.FailNow / t.Fatal
func TestHelloWorld(t *testing.T) { // Test function must start with Test
result := helper.HelloWorld("John")
if result != "Hello John" { // If result is not equal to "Hello John"
t.Error("Result must be Hello John") // t.Error will show error message
}
}
func TestHelloWorldFathanWithRequire(t *testing.T) {
result := helper.HelloWorldFathan()
// t is *testing.T
// "Hello Fathan" is expected result
// result is actual result
// "Result must be Hello Fathan" is error message
// require will stop the test if the result is not equal to expected result
require.Equal(t, "Hello Fathan", result, "Result must be Hello Fathan")
fmt.Println("TestHelloWorldFathan with require.Equal Done")
}
func TestHelloWorldFathanWithAssert(t *testing.T) {
result := helper.HelloWorldFathan()
// t is *testing.T
// "Hello Fathan" is expected result
// result is actual result
// "Result must be Hello Fathan" is error message
// assert will continue the test if the result is not equal to expected result
assert.Equal(t, "Hello Fathan", result, "Result must be Hello Fathan")
fmt.Println("TestHelloWorldFathan with assert.Equal Done")
}
// t.Skip will skip the test
func TestHelloWorldFathanWithSkipTest(t *testing.T) {
if runtime.GOOS == "darwin" { // Skip test if OS is Mac OS
t.Skip("Can not run on Mac OS")
} else if runtime.GOOS == "linux" { // Skip test if OS is Linux
t.Skip("Can not run on Linux")
}
result := helper.HelloWorldFathan()
require.Equal(t, "Hello Fathan", result, "Result must be Hello Fathan")
}
func TestSubTest(t *testing.T) {
// John is a sub test of TestSubTest
t.Run("John", func(t *testing.T) { // t.Run will run sub test
result := helper.HelloWorld("John")
assert.Equal(t, "Hello John", result, "Result must be Hello John")
})
t.Run("John2", TestHelloWorld) // TestHelloWorld is a function that already exist
// Fathan is a sub test of TestSubTest
t.Run("Fathan", func(t *testing.T) { // t.Run will run sub test
result := helper.HelloWorldFathan()
require.Equal(t, "Hello Fathan", result, "Result must be Hello Fathan")
})
t.Run("Fathan2", TestHelloWorldFathanWithRequire) // TestHelloWorldFathanWithRequire is a function that already exist
}
// Table Test is a test that use table data
func TestHelloWorldTable(t *testing.T) {
tests := []struct { // struct for table test data
name, request, expected, message string // name of the test, request parameter, expected result
}{
{
name: "HelloWorld(John)", // name of the test
request: "John", // request parameter
expected: "Hello John", // expected result
message: "Result must be Hello John",
},
{
name: "HelloWorld(Fathan)", // name of the test
request: "Fathan", // request parameter
expected: "Hello Fathan", // expected result
message: "Result must be Hello Fathan",
},
}
// _ is index, test is data, test is a struct
for _, test := range tests { // loop for each test data
t.Run(test.name, func(t *testing.T) { // test.name is name of the test
result := helper.HelloWorld(test.request) // test.request is request parameter
assert.Equal(t, test.expected, result, "Result must be "+test.expected) // test.expected is expected result
assert.Equal(t, test.expected, result, test.message) // test.expected is expected result
})
}
}
// test Main will run before and after unit test in this package
func TestMain(m *testing.M) { // *testing.M is a type of testing package
// before unit test
fmt.Println("Before Unit Test")
// run all unit test
m.Run() // Run all unit test just once
// after unit test
fmt.Println("After Unit Test")
}