assert

package
v1.7.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 18, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package assert 提供最基础的测试断言函数,替代 testify/assert 的常用功能。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Contains

func Contains(t testing.TB, s any, substr string, msgAndArgs ...any) bool

Contains 断言 s 包含 substr。支持 string 和 []byte 类型,返回是否断言通过。

func Empty

func Empty(t testing.TB, value any, msgAndArgs ...any) bool

Empty 断言值为空:nil、零长字符串/切片/map/通道,或类型零值,返回是否断言通过。

func Equal

func Equal(t testing.TB, expected, actual any, msgAndArgs ...any) bool

Equal 断言两个值相等,不等时报告错误并标记测试失败。 msgAndArgs 支持可选的格式化消息,第一个参数是 format 字符串,后面是参数。 返回是否断言通过,便于用 if !assert.Equal(...) { return } 实现立即终止。

func Error

func Error(t testing.TB, err error, msgAndArgs ...any) bool

Error 断言 err 不为 nil,返回是否断言通过。

func ErrorIs

func ErrorIs(t testing.TB, err, target error, msgAndArgs ...any) bool

ErrorIs 断言 err 链上包含 target 错误,返回是否断言通过。

func Eventually

func Eventually(t testing.TB, condition func() bool, waitFor, tick time.Duration, msgAndArgs ...any) bool

Eventually 断言 condition 在 waitFor 时间内最终返回 true,每隔 tick 轮询一次。 用于替代 sleep 等待异步结果,避免测试因固定等待时间而 flaky,返回是否断言通过。

func False

func False(t testing.TB, value bool, msgAndArgs ...any) bool

False 断言值为 false,返回是否断言通过。

func Len

func Len(t testing.TB, value any, length int, msgAndArgs ...any) bool

Len 断言值的长度等于 length,仅支持字符串/数组/切片/map/通道,返回是否断言通过。

func Never

func Never(t testing.TB, condition func() bool, waitFor, tick time.Duration, msgAndArgs ...any) bool

Never 断言 condition 在 waitFor 时间内始终返回 false,每隔 tick 检查一次。 一旦条件成立立即报错,用于验证某事在观察窗口内绝不发生,返回是否断言通过。

func Nil

func Nil(t testing.TB, value any, msgAndArgs ...any) bool

Nil 断言值为 nil,兼容携带类型信息的 nil(如 (*int)(nil)),返回是否断言通过。

func NoError

func NoError(t testing.TB, err error, msgAndArgs ...any) bool

NoError 断言 err 为 nil,返回是否断言通过。

func NotEmpty

func NotEmpty(t testing.TB, value any, msgAndArgs ...any) bool

NotEmpty 断言值非空:非 nil、长度大于零,且不是类型零值,返回是否断言通过。

func NotNil

func NotNil(t testing.TB, value any, msgAndArgs ...any) bool

NotNil 断言值不为 nil,兼容携带类型信息的 nil,返回是否断言通过。

func True

func True(t testing.TB, value bool, msgAndArgs ...any) bool

True 断言值为 true,返回是否断言通过。

func Zero

func Zero(t testing.TB, value any, msgAndArgs ...any) bool

Zero 断言值是其类型的零值,返回是否断言通过。

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL