GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - issue9/assert: Go 语言 assert 断言函数

BranchesTags
Open more actions menu

Latest commit

 

History

218 Commits
218 Commits

Folders and files

NameName
Last commit message
Last commit date
.github/workflows
.github/workflows
 
 
internal
internal
 
 
rest
rest
 
 
.editorconfig
.editorconfig
 
 
.gitignore
.gitignore
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
assert.go
assert.go
 
 
assert_test.go
assert_test.go
 
 
assertion.go
assertion.go
 
 
assertion_test.go
assertion_test.go
 
 
errors.go
errors.go
 
 
errors_test.go
errors_test.go
 
 
file.go
file.go
 
 
file_test.go
file_test.go
 
 
go.mod
go.mod
 
 
number.go
number.go
 
 
number_test.go
number_test.go
 
 
text.go
text.go
 
 
text_test.go
text_test.go
 
 

Repository files navigation

assert

Go codecov license PkgGoDev Go version

assert 包是对 testing 的一个简单扩展,提供的一系列的断言函数,方便在测试函数中使用。

func TestA(t *testing.T) {
	v := true
	a := assert.New(t, false)
	a.True(v)
}

// 也可以对 testing.B 使用
func Benchmark1(b *testing.B) {
	a := assert.New(b, false)
	v := false
	a.True(v)
	for b.Loop() {
		// do something
	}
}

assert/rest 也提供了针对 HTTP 请求的测试功能

func TestHTTP( t *testing.T) {
	a := assert.New(t, false)
	
	srv := rest.NewServer(a, h, nil)
	a.NotNil(srv)
	defer srv.Close()
	
	srv.NewRequest(http.MethodGet, "/body").
		Header("content-type", "application/json").
		Query("page", "5").
		EncodingBody(&bodyTest{ID: 5}, json.Marshal).
		Do().
		Status(http.StatusCreated).
		Header("content-type", "application/json;charset=utf-8").
		EncodingBody(&bodyTest{ID: 6}, json.Unmarshal)
}

// 也可以直接使用原始数据进行测试
func TestRaw(t *testing.T) {
	req :=`POST /users HTTP/1.1
	Host: example.com
	Content-type: application/json
	
	{"username": "admin", "password":"123"}
	
	`
	
	// 期望的返回数据
	resp :=`HTTP/1.1 201
	Location: https://example.com/users/1
	`

	a := assert.New(t, false)
	rest.RawHTTP(a, nil,req, resp)
}

版权

本项目采用 MIT 开源授权许可证,完整的授权说明可在 LICENSE 文件中找到。

About

Go 语言 assert 断言函数

pkg.go.dev/github.com/issue9/assert/v6

Topics

assertassertiongogolangtesttesting

Resources

Readme
MIT license
Activity
Custom properties

Stars

17 stars

Watchers

5 watching

Forks

3 forks
Report repository

Releases

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.