testkit

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2026 License: MIT Imports: 20 Imported by: 0

README

testkit

testkit 是 Genesis 的测试辅助包,目标不是提供一套庞大的测试框架,而是把项目里反复出现的测试基础设施和通用 helper 沉淀成统一入口。

接口文档见 go doc -all ./testkit。这个包没有单独的设计 blog,因为它是测试辅助包,不是面向生产接入的组件。

它当前主要解决三类问题:

  • 为测试提供统一的 loggermetercontext 和随机 ID helper。
  • 通过 testcontainers 自动拉起 Redis、MySQL、PostgreSQL、Etcd、NATS、Kafka。
  • 为 SQLite 这类无需容器的依赖提供快速 helper。

定位

testkit 只服务于测试代码,不应进入生产路径。它的设计目标是:

  • 集成测试尽量贴近真实依赖。
  • 运行测试前不需要手动执行 make up
  • 所有资源都挂在 *testing.T 生命周期上,由 t.Cleanup 自动回收。
  • 需要文件持久化的 helper 会使用 t.TempDir(),不会把临时文件留在仓库里。
  • 它不会替你自动建表、造业务数据或模拟完整调用链。

通用 helper

func TestSomething(t *testing.T) {
    kit := testkit.NewKit(t)

    ctx, cancel := testkit.NewContext(t, 5*time.Second)
    defer cancel()

    kit.Logger.InfoContext(ctx, "Starting test")
    _ = testkit.NewID()
}

可直接使用的通用 helper 有:

  • NewKit(t):返回带默认 CtxLoggerMeter 的测试工具包。
  • NewLogger():返回适合本地调试的开发态 logger。
  • NewMeter():返回 discard meter。
  • NewContext(t, timeout):返回带超时的 context,并自动注册 cleanup。
  • NewID():返回 8 位随机 ID,适合拼接 Redis key、topic、表名后缀。

容器化依赖

所有容器化 helper 都遵循同一模式:

  • NewXxxContainerConfig(t):启动容器并返回 connector config。
  • NewXxxContainerConnector(t):启动容器并返回已连接的 Genesis connector。
  • NewXxxContainerClient/Conn/DB(t):返回原生 client 或 DB。
依赖 Config Connector Client / DB
Redis NewRedisContainerConfig NewRedisContainerConnector NewRedisContainerClient
MySQL NewMySQLContainerConfig NewMySQLConnector NewMySQLDB
PostgreSQL NewPostgreSQLContainerConfig NewPostgreSQLConnector NewPostgreSQLDB
Etcd NewEtcdContainerConfig NewEtcdContainerConnector NewEtcdContainerClient
NATS NewNATSContainerConfig NewNATSContainerConnector NewNATSContainerConn
Kafka NewKafkaContainerConfig NewKafkaContainerConnector NewKafkaContainerClient

Redis 示例:

func TestRedis(t *testing.T) {
    rdb := testkit.NewRedisContainerClient(t)

    err := rdb.Set(context.Background(), "key", "value", 0).Err()
    require.NoError(t, err)
}

MySQL 示例:

func TestMySQL(t *testing.T) {
    db := testkit.NewMySQLDB(t)

    type User struct {
        ID   uint
        Name string
    }

    require.NoError(t, db.AutoMigrate(&User{}))
}

Etcd 示例:

func TestEtcd(t *testing.T) {
    client := testkit.NewEtcdContainerClient(t)

    _, err := client.Put(context.Background(), "test:key", "value")
    require.NoError(t, err)
}

SQLite helper

SQLite 不依赖容器,适合快速测试:

func TestSQLite(t *testing.T) {
    db := testkit.NewSQLiteDB(t)
    require.NotNil(t, db)
}

如果需要文件持久化:

func TestPersistentSQLite(t *testing.T) {
    conn := testkit.NewPersistentSQLiteConnector(t)
    require.NotNil(t, conn.GetClient())
}

使用约束

  • 集成测试优先复用 testkit,不要在各组件里重复实现容器拉起逻辑。
  • 运行测试前不要手动执行 make uptestkit 会通过 testcontainers 自动处理依赖。
  • 测试断言统一使用 require,不要新增 assert
  • 需要数据隔离时,使用 testkit.NewID() 生成唯一 key、topic、consumer group 或表后缀。

当前边界

testkit 目前提供的是基础设施级 helper,而不是完整测试 DSL。它不会替你自动建表、造业务数据或模拟完整调用链,这些仍应由各组件测试自行控制。

相关入口:

Documentation

Overview

Package testkit 提供 Genesis 组件测试所需的通用 helper。

这个包只服务于测试代码,不进入生产路径,也不承担完整测试 DSL 的职责。 它的核心目标有三点:

  1. 统一测试中的 logger、meter、context 和随机 ID 生成方式。
  2. 通过 testcontainers 为 Redis、MySQL、PostgreSQL、Etcd、NATS、Kafka 等外部依赖提供一次性容器、已连接 client/connector 和测试隔离。
  3. 让组件集成测试尽量贴近真实依赖,同时避免开发者在运行测试前手动执行 make up 或维持一整套长期驻留的本地环境。

典型用法是直接在测试中调用:

redisClient := testkit.NewRedisContainerClient(t)
db := testkit.NewMySQLDB(t)
ctx, cancel := testkit.NewContext(t, 5*time.Second)
defer cancel()

所有公开 helper 都以 *testing.T 为生命周期锚点,通过 t.Cleanup 自动回收 容器、连接器和临时资源。需要文件持久化的 SQLite helper 会使用 t.TempDir() 创建临时目录。

这个包不会替测试自动建表、造业务数据或模拟完整调用链,这些控制权仍应由 各组件测试自行保留。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewContext

func NewContext(t *testing.T, timeout time.Duration) (context.Context, context.CancelFunc)

NewContext 返回一个带有超时的测试上下文。

func NewEtcdContainerClient

func NewEtcdContainerClient(t *testing.T) *clientv3.Client

NewEtcdContainerClient 使用 testcontainers 创建并返回原生 Etcd 客户端。 生命周期由 t.Cleanup 管理。

func NewEtcdContainerConfig

func NewEtcdContainerConfig(t *testing.T) *connector.EtcdConfig

NewEtcdContainerConfig 使用 testcontainers 创建 Etcd 容器并返回配置。 生命周期由 t.Cleanup 管理。

func NewEtcdContainerConnector

func NewEtcdContainerConnector(t *testing.T) connector.EtcdConnector

NewEtcdContainerConnector 使用 testcontainers 创建并连接 Etcd 连接器。 生命周期由 t.Cleanup 管理。

func NewID

func NewID() string

NewID 返回一个唯一的测试 ID(UUID v4 前 8 位)。 用于生成唯一的 Key、Topic 或表名后缀,避免测试间数据冲突。

func NewKafkaContainerClient

func NewKafkaContainerClient(t *testing.T) *kgo.Client

NewKafkaContainerClient 使用 testcontainers 创建并返回原生 Kafka 客户端。 生命周期由 t.Cleanup 管理。

func NewKafkaContainerConfig

func NewKafkaContainerConfig(t *testing.T) *connector.KafkaConfig

NewKafkaContainerConfig 使用 testcontainers 创建 Kafka 容器并返回配置。 生命周期由 t.Cleanup 管理。

func NewKafkaContainerConnector

func NewKafkaContainerConnector(t *testing.T) connector.KafkaConnector

NewKafkaContainerConnector 使用 testcontainers 创建并连接 Kafka 连接器。 生命周期由 t.Cleanup 管理。

func NewLogger

func NewLogger() clog.Logger

NewLogger 返回一个用于测试的 logger。 输出到开发环境格式,适合本地调试。

func NewMeter

func NewMeter() metrics.Meter

NewMeter 返回一个用于测试的 meter。 使用 Discard 模式,不实际输出指标。

func NewMySQLConnector

func NewMySQLConnector(t *testing.T) connector.MySQLConnector

NewMySQLConnector 获取 MySQL 连接器(基于 testcontainers)。 生命周期由 t.Cleanup 管理。

func NewMySQLContainerConfig

func NewMySQLContainerConfig(t *testing.T) *connector.MySQLConfig

NewMySQLContainerConfig 使用 testcontainers 创建 MySQL 容器并返回配置。 生命周期由 t.Cleanup 管理。

func NewMySQLDB

func NewMySQLDB(t *testing.T) *gorm.DB

NewMySQLDB 获取 GORM DB 实例(基于 testcontainers)。

func NewNATSContainerConfig

func NewNATSContainerConfig(t *testing.T) *connector.NATSConfig

NewNATSContainerConfig 使用 testcontainers 创建 NATS 容器并返回配置。 生命周期由 t.Cleanup 管理。

func NewNATSContainerConn

func NewNATSContainerConn(t *testing.T) *nats.Conn

NewNATSContainerConn 使用 testcontainers 创建并返回原生 NATS 连接。 生命周期由 t.Cleanup 管理。

func NewNATSContainerConnector

func NewNATSContainerConnector(t *testing.T) connector.NATSConnector

NewNATSContainerConnector 使用 testcontainers 创建并连接 NATS 连接器。 生命周期由 t.Cleanup 管理。

func NewPersistentSQLiteConfig

func NewPersistentSQLiteConfig(t *testing.T) *connector.SQLiteConfig

NewPersistentSQLiteConfig 返回持久化 SQLite 测试配置。 用于需要文件持久化的测试场景,数据库文件存储在 t.TempDir() 中。

func NewPersistentSQLiteConnector

func NewPersistentSQLiteConnector(t *testing.T) connector.SQLiteConnector

NewPersistentSQLiteConnector 获取持久化 SQLite 连接器。 数据库文件存储在临时目录中,测试结束后自动清理。 生命周期由 t.Cleanup 管理。

func NewPostgreSQLConnector

func NewPostgreSQLConnector(t *testing.T) connector.PostgreSQLConnector

NewPostgreSQLConnector 获取 PostgreSQL 连接器(基于 testcontainers)。 生命周期由 t.Cleanup 管理。

func NewPostgreSQLContainerConfig

func NewPostgreSQLContainerConfig(t *testing.T) *connector.PostgreSQLConfig

NewPostgreSQLContainerConfig 使用 testcontainers 创建 PostgreSQL 容器并返回配置。 生命周期由 t.Cleanup 管理。

func NewPostgreSQLDB

func NewPostgreSQLDB(t *testing.T) *gorm.DB

NewPostgreSQLDB 获取 GORM DB 实例(基于 testcontainers)。

func NewRedisContainerClient

func NewRedisContainerClient(t *testing.T) *redis.Client

NewRedisContainerClient 使用 testcontainers 创建并返回原生 Redis 客户端。 生命周期由 t.Cleanup 管理。

func NewRedisContainerConfig

func NewRedisContainerConfig(t *testing.T) *connector.RedisConfig

NewRedisContainerConfig 使用 testcontainers 创建 Redis 容器并返回配置。 生命周期由 t.Cleanup 管理。

func NewRedisContainerConnector

func NewRedisContainerConnector(t *testing.T) connector.RedisConnector

NewRedisContainerConnector 使用 testcontainers 创建并连接 Redis 连接器。 生命周期由 t.Cleanup 管理。

func NewSQLiteConfig

func NewSQLiteConfig() *connector.SQLiteConfig

NewSQLiteConfig 返回 SQLite 内存数据库配置。 默认使用内存数据库,测试结束后自动清理。

func NewSQLiteConnector

func NewSQLiteConnector(t *testing.T) connector.SQLiteConnector

NewSQLiteConnector 获取 SQLite 连接器(内存数据库)。 生命周期由 t.Cleanup 管理。

func NewSQLiteDB

func NewSQLiteDB(t *testing.T) *gorm.DB

NewSQLiteDB 获取 GORM DB 实例(内存数据库)。

Types

type Kit

type Kit struct {
	Ctx    context.Context
	Logger clog.Logger
	Meter  metrics.Meter
}

Kit 包含通用的测试依赖。

func NewKit

func NewKit(t *testing.T) *Kit

NewKit 返回一个包含默认依赖的测试工具包。

Jump to

Keyboard shortcuts

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