Documentation
¶
Overview ¶
Package testkit 提供 Genesis 组件测试所需的通用 helper。
这个包只服务于测试代码,不进入生产路径,也不承担完整测试 DSL 的职责。 它的核心目标有三点:
- 统一测试中的 logger、meter、context 和随机 ID 生成方式。
- 通过 testcontainers 为 Redis、MySQL、PostgreSQL、Etcd、NATS、Kafka 等外部依赖提供一次性容器、已连接 client/connector 和测试隔离。
- 让组件集成测试尽量贴近真实依赖,同时避免开发者在运行测试前手动执行 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 ¶
- func NewContext(t *testing.T, timeout time.Duration) (context.Context, context.CancelFunc)
- func NewEtcdContainerClient(t *testing.T) *clientv3.Client
- func NewEtcdContainerConfig(t *testing.T) *connector.EtcdConfig
- func NewEtcdContainerConnector(t *testing.T) connector.EtcdConnector
- func NewID() string
- func NewKafkaContainerClient(t *testing.T) *kgo.Client
- func NewKafkaContainerConfig(t *testing.T) *connector.KafkaConfig
- func NewKafkaContainerConnector(t *testing.T) connector.KafkaConnector
- func NewLogger() clog.Logger
- func NewMeter() metrics.Meter
- func NewMySQLConnector(t *testing.T) connector.MySQLConnector
- func NewMySQLContainerConfig(t *testing.T) *connector.MySQLConfig
- func NewMySQLDB(t *testing.T) *gorm.DB
- func NewNATSContainerConfig(t *testing.T) *connector.NATSConfig
- func NewNATSContainerConn(t *testing.T) *nats.Conn
- func NewNATSContainerConnector(t *testing.T) connector.NATSConnector
- func NewPersistentSQLiteConfig(t *testing.T) *connector.SQLiteConfig
- func NewPersistentSQLiteConnector(t *testing.T) connector.SQLiteConnector
- func NewPostgreSQLConnector(t *testing.T) connector.PostgreSQLConnector
- func NewPostgreSQLContainerConfig(t *testing.T) *connector.PostgreSQLConfig
- func NewPostgreSQLDB(t *testing.T) *gorm.DB
- func NewRedisContainerClient(t *testing.T) *redis.Client
- func NewRedisContainerConfig(t *testing.T) *connector.RedisConfig
- func NewRedisContainerConnector(t *testing.T) connector.RedisConnector
- func NewSQLiteConfig() *connector.SQLiteConfig
- func NewSQLiteConnector(t *testing.T) connector.SQLiteConnector
- func NewSQLiteDB(t *testing.T) *gorm.DB
- type Kit
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewContext ¶
NewContext 返回一个带有超时的测试上下文。
func NewEtcdContainerClient ¶
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 ¶
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 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 ¶
NewMySQLDB 获取 GORM DB 实例(基于 testcontainers)。
func NewNATSContainerConfig ¶
func NewNATSContainerConfig(t *testing.T) *connector.NATSConfig
NewNATSContainerConfig 使用 testcontainers 创建 NATS 容器并返回配置。 生命周期由 t.Cleanup 管理。
func NewNATSContainerConn ¶
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 ¶
NewPostgreSQLDB 获取 GORM DB 实例(基于 testcontainers)。
func NewRedisContainerClient ¶
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 管理。