码迷,mamicode.com
首页 > 编程语言 > 详细

Go语言表组测试示例

时间:2017-07-01 17:21:34      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:==   loading   技术分享   status   fat   test   ges   oca   port   

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

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语言表组测试示例

标签:==   loading   技术分享   status   fat   test   ges   oca   port   

原文地址:http://www.cnblogs.com/aguncn/p/7102562.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!