Go语言基础单元测试示例 - Go语言中文社区

Go语言基础单元测试示例


这个要熟悉原理,要能写。。

但现在。。。。。

注意,没有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("ttShould be able to make the Get call.", checkMark)
			defer resp.Body.Close()
			
			if resp.StatusCode == statusCode {
				t.Logf("ttShould receive a "%d" status, %v", statusCode, checkMark)
			} else {
				t.Errorf("ttShould receive a "%d" status. %v %v", statusCode, ballotX, resp.StatusCode)
			}
		}
	}
}
				

  

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/weixin_33726943/article/details/85909477
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2019-08-28 20:20:57
  • 阅读 ( 673 )
  • 分类:Go

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢