Last active
April 2, 2017 16:29
-
-
Save jwieringa/f8e2459e87ffe2654eb9138a34766e24 to your computer and use it in GitHub Desktop.
Learning about subtests in Golang with Parallel execution
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
// Sequential | |
// $ go test | |
// PASS | |
// ok 7.312s | |
// Parallel On | |
// $ go test | |
// PASS | |
// ok 0.985s | |
package tests | |
import ( | |
"fmt" | |
"net/http" | |
"testing" | |
) | |
// Config | |
var host string = "opendata.arcgis.com" | |
func TestTime(t *testing.T) { | |
testCases := []struct { | |
proto string | |
resp string | |
path string | |
}{ | |
{"https", "204 OK", "/datasets"}, | |
{"https", "200 OK", "/sites"}, | |
{"https", "200 OK", "/api/v2/sites"}, | |
{"https", "200 OK", "/api"}, | |
{"https", "200 OK", "/sites"}, | |
{"https", "200 OK", "/utilities"}, | |
{"https", "202 Accepted", "/datasets/ddd4129848f54e1a92f7d4080284b27a_0/geohash"}, | |
{"https", "200 OK", "/torii-provider-arcgis/redirect.html"}, | |
{"https", "200 OK", "/datasets/#{dataset}"}, | |
{"https", "200 OK", "/datasets"}, | |
{"https", "200 OK", "/"}, | |
{"https", "200 OK", "/robots.txt"}, | |
} | |
for _, tc := range testCases { | |
tc := tc | |
client := &http.Client{} | |
t.Run(fmt.Sprintf("%s in %s", tc.path, tc.resp), func(st *testing.T) { | |
st.Parallel() | |
uri := tc.proto + "://" + host + tc.path | |
req, err := http.NewRequest("GET", uri, nil) | |
if err != nil { | |
t.Fatalf("couldn't build request %s%s: error %s", uri, err) | |
} | |
resp, err := client.Do(req) | |
if err != nil { | |
t.Fatalf("couldn't request %s: error %s", uri, err) | |
} | |
defer resp.Body.Close() | |
if got := resp.Status; got != tc.resp { | |
t.Errorf("%s responded with %s; expected %s", uri, got, tc.resp) | |
} | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment