{"id":34119812,"url":"https://github.com/tinyhubs/et","last_synced_at":"2026-03-10T09:02:19.774Z","repository":{"id":57528079,"uuid":"90520971","full_name":"tinyhubs/et","owner":"tinyhubs","description":"et is saving your time for writing test code.","archived":false,"fork":false,"pushed_at":"2018-07-22T09:12:31.000Z","size":52,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-06-20T03:49:37.450Z","etag":null,"topics":["assert","expect","extensibility","go","golang","test","testing"],"latest_commit_sha":null,"homepage":"https://tinyhubs.github.io/et/","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tinyhubs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-05-07T09:08:35.000Z","updated_at":"2018-07-22T09:12:32.000Z","dependencies_parsed_at":"2022-08-31T07:01:29.713Z","dependency_job_id":null,"html_url":"https://github.com/tinyhubs/et","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/tinyhubs/et","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tinyhubs%2Fet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tinyhubs%2Fet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tinyhubs%2Fet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tinyhubs%2Fet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tinyhubs","download_url":"https://codeload.github.com/tinyhubs/et/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tinyhubs%2Fet/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30328272,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-10T05:25:20.737Z","status":"ssl_error","status_checked_at":"2026-03-10T05:25:17.430Z","response_time":106,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["assert","expect","extensibility","go","golang","test","testing"],"created_at":"2025-12-14T21:19:52.477Z","updated_at":"2026-03-10T09:02:19.741Z","avatar_url":"https://github.com/tinyhubs.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 概要\n\n[![Build Status](https://travis-ci.org/tinyhubs/et.svg?branch=master)](https://travis-ci.org/tinyhubs/et)\n[![GoDoc](https://godoc.org/github.com/tinyhubs/et?status.svg)](https://godoc.org/github.com/tinyhubs/et)\n[![Language](https://img.shields.io/badge/language-go-lightgrey.svg)](https://github.com/tinyhubs/et)\n[![License](https://img.shields.io/badge/license-New%20BSD-yellow.svg?style=flat)](LICENSE)\n[![codecov](https://codecov.io/gh/tinyhubs/et/branch/master/graph/badge.svg)](https://codecov.io/gh/tinyhubs/et)\n[![goreport](https://www.goreportcard.com/badge/github.com/tinyhubs/et)](https://www.goreportcard.com/report/github.com/tinyhubs/et)\n\n`et` 全名是`EasyTest`,它是一个有着丰富断言函数的,可扩展的单元测试辅助库.\n\n# 安装\n\n`et`无额外的包依赖,您可以直接下载代码到本地,也可以通过下面的命令获取:\n\n```bash\n$ go get -u github.com/tinyhubs/et\n```\n\n# 示例\n\n```go\npackage examples\n\nimport (\n\t\"testing\"\n\t\"github.com/tinyhubs/et/assert\"\n)\n\nfunc TestAssert_Equal(t *testing.T) {\n\tassert.Equal(t, \"123\", \"456\")\n}\n\nfunc TestAssert_Equali(t *testing.T) {\n\tassert.Equali(t, \"Expect-the-values-is-equal\", \"123\", \"456\")\n}\n```\n\n上面的例子的输出如下,每个失败的用例的第二行堆栈信息就是断言出错的的assert或者是expect的代码的位置.\n\n```text\n\tet-core.go:16:\n\t\t/Users/llj/mygithub/src/github.com/tinyhubs/et/examples/example1_test.go:9\n\t\tExpect:123, Actual:456\n\tet-core.go:18:\n\t\t/Users/llj/mygithub/src/github.com/tinyhubs/et/examples/example1_test.go:13\n\t\tExpect-the-values-is-equal\n\t\tExpect:123, Actual:456\n```\n\n# 功能\n## 基本断言\n\n- 相等检测: `assert.Equal`\n\n```go\nassert.Equal(t, \"123\", \"456\")\n```\n\n- 布尔检测: `assert.True`\n\n```go\nassert.True(t, \"123\" == \"456\")\n```\n\n- 检测是否会抛异常: `assert.Panic`\n\n```go\nassert.Panic(t, func() { /* Do nothing. */ })\n```\n\n- 检测是否匹配正则表达式: `assert.Match`\n\n```go\nassert.Match(t, `^[a-zA-Z0-9-_]+@timo\\.com$`, \"libbylg@126.com\")\n```\n\n- 检测是否为nil: `assert.Nil`\n\n```go\nassert.Nil(t, bytes.NewBufferString(\"\"))\n```\n\n## assert vs expect\n\n`et`提供了assert和expect两套接口,所有在assert中存在的函数,except中也会存在.\n比如,assert里面有`assert.Equal`,那么也会存在`except.Equal`.\nassert系接口和expect系的接口也可以组合使用,以便实现更丰富的功能.\n\nassert和expect的区别就是assert会立即中断当前用例的执行,而expect会一直执行下去直到用例执行完毕:\n\n```go\npackage examples\n\nimport (\n\t\"testing\"\n\t\"github.com/tinyhubs/et/expect\"\n\t\"github.com/tinyhubs/et/assert\"\n)\n\nfunc Test_2_Expect_Equal(t *testing.T) {\n\texpect.Equal(t, \"123\", \"456\")\n\texpect.Equal(t, 333, 7788)\n\texpect.Equal(t, \"sina\", \"sina\")\n} //  stoped here\n\nfunc Test_2_Assert_Equal(t *testing.T) {\n\tassert.Equal(t, \"123\", \"456\") //  stoped here\n\tassert.Equal(t, 333, 7788)\n\tassert.Equal(t, \"sina\", \"sina\")\n}\n```\n\n这个两个用例的输出结果如下:\n\n```text\n\tet-core.go:28:\n\t\t/Users/llj/mygithub/src/github.com/tinyhubs/et/examples/example2_test.go:10\n\t\tExpect:123, Actual:456\n\tet-core.go:28:\n\t\t/Users/llj/mygithub/src/github.com/tinyhubs/et/examples/example2_test.go:11\n\t\tExpect:333, Actual:7788\n\tet-core.go:28:\n\t\t/Users/llj/mygithub/src/github.com/tinyhubs/et/examples/example2_test.go:12\n\t\tExpect:sina, Actual:google\n\tet-core.go:16:\n\t\t/Users/llj/mygithub/src/github.com/tinyhubs/et/examples/example2_test.go:16\n\t\tExpect:mtn, Actual:rnd\n```\n\n## 断言意图作为代码\n\n测试代码也需要良好的可维护性,所以大家写测试代码的时候往往会在断言代码附近写上注释来表达断言的意图.如下所示:\n\n```go\n//  Check the email is xxx@timo.com\nassert.Match(t, `^[a-zA-Z0-9-_]+@timo\\.com$`, email)\n```\n\n但注释毕竟不是代码,难以得到开发人员的爱,很容易失去维护,所以如果能将断言的意图作为assert函数的一个参数,那么大家修改测试代码时就不会再忽略掉了.\n这是一个很好的实践,`et`也支持这种用法.`et`的每个基本assert接口都支持一个`i`后缀的函数,使用这些`i`后缀的函数你可以在调用assert函数的时候,附上断言的意图.\n如下所示,我们可以利用`et`的`i`系列函数将前面这个代码改成下面这样:\n\n```go\nassert.Matchi(t, \"Check the email is xxx@timo.com\", `^[a-zA-Z0-9-_]+@timo\\.com$`, email)\n```\n\n\n## 反逻辑断言(NOT-Assertor)\n\n为提高断言代码的可用性,et为每个正向断言和反向断言都提供了函数. 比如:\n如果你用`assert.Equal`来检测两个数据是否相等,那么你也应该知道其实您也可以使用`assert.NotEqual`来检测两个数不相等.\n类似的还有:\n\n`assert.Equal` vs `assert.NotEqual`\n\n`assert.True` vs `assert.False`\n\n`assert.Panic` vs `assert.NoPanic`\n\n`i`系列函数也是支持的:\n\n`assert.Equali` vs `assert.NotEquali`\n\n`assert.Truei` vs `assert.Falsei`\n\n`assert.Panici` vs `assert.NoPanici`\n\n\n## 扩展断言机制\n\n假设一个场景,我们现在需要一个判断某个整数是否在某个范围之内的断言怎么办?这个是et默认没有提供的断言.\n但是,聪明如你,你一定会想到其实可以用`assert.True`来实现这个断言.所以你可能会这样写代码:\n\n```go\nassert.True(t, (value \u003e= min) \u0026\u0026 (value \u003c= max))\n```\n\n这个代码可以工作,但是我们总有那么点遗憾:为啥et不提供一个`assert.Inrange`的断言函数呢?这样咱们就可以这样写代码了:\n\n```go\nassert.Inrange(t, min, max, value)\n```\n\n其实,et提供了一种机制可以帮你实现自己的断言.\n\n#### 方式1,创建一个判断范围的`Assertor`\n\n采用这种方式时,需要你提供一个实现了`et.Assertor`接口的类,您可以这样做:\n\n```go\ntype Inrange struct {\n\tMin   int\n\tMax   int\n\tValue int\n}\n\nfunc (i *Inrange) Assert() error {\n\tif (i.Value \u003e= i.Min) \u0026\u0026 (i.Value \u003c= i.Max) {\n\t\treturn nil\n\t}\n\n\treturn fmt.Errorf(\"Expect in range [%v, %v], Actual: %v\", i.Min, i.Max, i.Value)\n}\n\n```\n\n然后,这样使用(注意需要先`import \"github.com/tinyhubs/et/et\"`):\n\n```go\net.Assert(\u0026Inrange{min, max, value}, t)\n```\n\n如果触发了断言失败,可以获得下面的结果,注意看第二行提示,这个就是我们Assertor返回的error哦:\n\n```text\n\tet-core.go:16:\n\t\t/Users/llj/mygithub/src/github.com/tinyhubs/et/examples/example3_test.go:27\n\t\tExpect in range [1, 100], Actual: 320\n```\n\n`et.Assert`的用法好像跟`assert.Equal`其他的不一样对吧,但其实assert.Equal也是使用上面的Assertor类似的机制实现的.\n这就涉及到下面的扩展方式2.\n\n#### 方式2,自行封装assert函数\n\n你可以在方式1的基础上再额外提供下面一个断言函数:\n\n```go\nfunc AssertInrange(t *testing.T, min int, max int, value int) {\n\tet.AssertInner(t, \"\", \u0026Inrange{min, max, value}, 2)\n}\n```\n\n然后,方式1中的实例代码可以改成下面这样:\n\n```go\nAssertInrange(t, min, max, value)\n```\n\n输出结果如下,和方式1的输出结果相同:\n\n```text\n\tet-core.go:16:\n\t\t/Users/llj/mygithub/src/github.com/tinyhubs/et/examples/example3_test.go:40\n\t\tExpect in range [1, 100], Actual: 320\n```\n\n您可以在[`et/examples/example3_test.go`](https://github.com/tinyhubs/et/tree/master/examples/example3_test.go)查看这个完整的扩展的示例,您可以试着运行下diamante看看效果.\n\n如果您觉得自己开发了一个很不错的扩展,请必要忘记分享给你周边的同事,如果觉得您的扩展可以帮到更多人,那么直接发一个pull request给我吧,或许我可以合并到et中去.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftinyhubs%2Fet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftinyhubs%2Fet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftinyhubs%2Fet/lists"}