Go语言表组测试示例

比基本测试多个循环。。。

package main

import (
	"testing"
	"net/http"
)

const checkMark = " OK! "
const ballotX = " ERROR! "
	
func TestDownload(t *testing.T) {
	var urls = []struct {
		url 	string
		statusCode int
	}{
		{
			"http://localhost:8000/test.html", http.StatusOK,
		},
		{
			"http://localhost:8000/test.html2", http.StatusNotFound,
		},
	}
	

	
	t.Log("Given the need to test downloading content.")
	{
		for _, u := range urls {
			t.Logf("\tWhen checking \"%s\" for status code \"%d\"", u.url, u.statusCode)
			{
				resp, err := http.Get(u.url)
				if err != nil {
					t.Fatal("\tShould be able to make the Get call.", ballotX, err)
				}
				t.Log("\t\tShould be able to make the Get call.", checkMark)
				defer resp.Body.Close()
				
				if resp.StatusCode == u.statusCode {
					t.Logf("\t\tShould receive a \"%d\" status, %v", u.statusCode, checkMark)
				} else {
					t.Errorf("\t\tShould receive a \"%d\" status. %v %v", u.statusCode, ballotX, resp.StatusCode)
				}
			}
		}
	}
}
				

  Go语言表组测试示例