1. 程式人生 > >Go語言基礎單元測試示例

Go語言基礎單元測試示例

== stat 示例 語言 單元 ould true def 函數

這個要熟悉原理,要能寫。。

但現在。。。。。

註意,沒有main函數,以_test.go結尾,命令go test -v

package main

import (
	"testing"
	"net/http"
)

const checkMark = " OK! "
const ballotX = " ERROR! "
	
func TestDownload(t *testing.T) {
	url := "http://localhost:8000/test.html"
	statusCode := 200
	
	t.Log("Given the need to test downloading content.")
	{
		t.Logf("\tWhen checking \"%s\" for status code \"%d\"", url, statusCode)
		{
			resp, err := http.Get(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 == statusCode {
				t.Logf("\t\tShould receive a \"%d\" status, %v", statusCode, checkMark)
			} else {
				t.Errorf("\t\tShould receive a \"%d\" status. %v %v", statusCode, ballotX, resp.StatusCode)
			}
		}
	}
}
				

  技術分享

Go語言基礎單元測試示例