forked from akashpayneSRO/gotraining-studyguide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sub_test.go
114 lines (98 loc) · 3.37 KB
/
sub_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
107
108
109
110
111
112
113
114
// Sub Test
// --------
// Sub test helps us streamline our test functions, filters out command-line level big tests into
// smaller sub tests.
package main
import (
"net/http"
"testing"
)
// These constant gives us checkboxes for visualization.
const (
succeed = "\u2713"
failed = "\u2717"
)
// TestSub validates the http Get function can download content and
// handles different status conditions properly.
func TestSub(t *testing.T) {
tests := []struct {
name string
url string
statusCode int
}{
{"statusok", "https://www.google.com/", http.StatusOK},
{"statusnotfound", "http://rss.cnn.com/rss/cnn_topstorie.rss", http.StatusNotFound},
}
t.Log("Given the need to test downloading different content.")
{
// Range over our table but this time, create an anonymous function that takes a testing T
// parameter. This is a test function inside a test function.
// What nice about it is that we are gonna have a new function for each set of data that we
// have in our table. Therefore, we will end up with 2 different functions here.
for i, tt := range tests {
tf := func(t *testing.T) {
t.Logf("\tTest: %d\tWhen checking %q for status code %d", i, tt.url, tt.statusCode)
{
resp, err := http.Get(tt.url)
if err != nil {
t.Fatalf("\t%s\tShould be able to make the Get call : %v", failed, err)
}
t.Logf("\t%s\tShould be able to make the Get call.", succeed)
defer resp.Body.Close()
if resp.StatusCode == tt.statusCode {
t.Logf("\t%s\tShould receive a %d status code.", succeed, tt.statusCode)
} else {
t.Errorf("\t%s\tShould receive a %d status code : %v", failed, tt.statusCode, resp.StatusCode)
}
}
}
// Once we declare this function, we tell the testing tool to register it as a sub test
// under the test name.
t.Run(tt.name, tf)
}
}
}
// TestParallelize validates the http Get function can download content and
// handles different status conditions properly but runs the tests in parallel.
func TestParallelize(t *testing.T) {
tests := []struct {
name string
url string
statusCode int
}{
{"statusok", "https://www.goinggo.net/post/index.xml", http.StatusOK},
{"statusnotfound", "http://rss.cnn.com/rss/cnn_topstorie.rss", http.StatusNotFound},
}
t.Log("Given the need to test downloading different content.")
{
for i, tt := range tests {
tf := func(t *testing.T) {
// The only difference here is that we call Parallel function inside each of these
// individual sub test function.
t.Parallel()
t.Logf("\tTest: %d\tWhen checking %q for status code %d", i, tt.url, tt.statusCode)
{
resp, err := http.Get(tt.url)
if err != nil {
t.Fatalf("\t%s\tShould be able to make the Get call : %v", failed, err)
}
t.Logf("\t%s\tShould be able to make the Get call.", succeed)
defer resp.Body.Close()
if resp.StatusCode == tt.statusCode {
t.Logf("\t%s\tShould receive a %d status code.", succeed, tt.statusCode)
} else {
t.Errorf("\t%s\tShould receive a %d status code : %v", failed, tt.statusCode, resp.StatusCode)
}
}
}
t.Run(tt.name, tf)
}
}
}
// Output:
// -------
// Because we have sub tests, we can run the following to separate them:
// "go test -run TestSub -v"
// "go test -run TestSub/statusok -v"
// "go test -run TestSub/statusnotfound -v"
// "go test -run TestParallelize -v"