百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
A

assert

> 测试质量
开源

使用 Go 通用类型的简单断言库

194 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

使用 Go 通用类型的简单断言库

A simple assertion library using Go generics

This library is inspired by testify/require, but with a significantly reduced API surface based on empirical use of that package.

It also provides much nicer diff output, eg.

=== RUN   TestFail
    assert_test.go:14: Expected values to be equal:
         assert.Data{
        -  Str: "foo",
        +  Str: "far",
           Num: 10,
         }
--- FAIL: TestFail (0.00s)

API

Import then use as assert:

go
import "github.com/alecthomas/assert/v2"

This library has the following API. For all functions, msgAndArgs is used to format error messages using the fmt package.

…

Evaluation process

Our empirical data of testify usage comes from a monorepo with around 50K lines of tests.

These are the usage counts for all testify functions, normalised to the base (not Printf()) non-negative(not No(t)?) case for each core function.

2240 Error
1314 Equal
 219 True
 210 Nil
 167 Empty
 107 Contains
  79 Len
  61 False
  24 EqualValues
  20 EqualError
  17 Zero
  15 Fail
  15 ElementsMatch
   9 Panics
   7 IsType
   6 FileExists
   4 JSONEq
   3 PanicsWithValue
   3 Eventually

The decision for each function was:

Keep

  • Error(t, err) -> frequently used, keep
  • Equal(t, expected, actual) -> frequently used, keep but make type safe
  • True(t, expr) -> frequently used, keep
  • False(t, expr) -> frequently used, keep
  • Empty(t, thing) -> require.Equal(t, len(thing), 0)
  • Contains(t, haystack string, needle string) - the only variant used in our codebase, keep as concrete type
  • Zero(t, value) -> make type safe, keep
  • Panics(t, f) -> useful, keep
  • EqualError(t, a, b) -> useful, keep
  • Nil(t, value) -> frequently used, keep

Not keeping, replace with ...

  • ElementsMatch(t, a, b) - use peterrk/slices or stdlib sort support once it lands.
  • IsType(t, a, b) -> require.Equal(t, reflect.TypeOf(a).String(), reflect.TypeOf(b).String())
  • FileExists() -> very little use, drop
  • JSONEq() -> very little use, drop
  • PanicsWithValue() -> very little use, drop
  • Eventually() -> very little use, drop
  • Contains(t, haystack []T, needle T) - very little use, replace with
  • Contains(t, haystack map[K]V, needle K) - very little use, drop
  • Len(t, v, n) -> cannot be implemented as a single function with genericsEqual(t, len(v), n)
  • EqualValues() - Equal(t, TYPE(a), TYPE(b))
  • Fail() -> t.Fatal()

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Goassertgenericsgogolang

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类测试质量
定价开源

> 相关工具

J
Jest
JavaScript 测试框架
P
Playwright
现代端到端测试框架
E
eslint-plugin-test-selectors
Enforces that data-test-id attributes are added to interactive DOM elements (JSX) to help with UI testing. JSX only.