🧪 WxBot Enhanced 插件测试和调试指南¶
 **确保插件质量和稳定性的完整测试方案**
📋 目录导航¶
🧪 测试框架概述¶
测试环境准备¶
package myplugin
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/ruk1ng001/wxbot/engine/robot"
)
// 创建测试上下文
func createTestContext(message string) *robot.Ctx {
return &robot.Ctx{
Event: &robot.Event{
FromWxId: "test_group",
FinalFromWxId: "test_user",
Message: &robot.Message{
Content: message,
Type: robot.MsgTypeText,
},
},
}
}
✅ 单元测试¶
基础单元测试¶
func TestBasicHandler(t *testing.T) {
ctx := createTestContext("test message")
result := processMessage(ctx.MessageString())
assert.NotEmpty(t, result)
assert.Contains(t, result, "test")
}
func TestInputValidation(t *testing.T) {
testCases := []struct {
input string
expected bool
}{
{"normal input", true},
{"", false},
{"very long input" + strings.Repeat("x", 2000), false},
}
for _, tc := range testCases {
result := isValidInput(tc.input)
assert.Equal(t, tc.expected, result, "Input: %s", tc.input)
}
}
错误处理测试¶
func TestErrorHandling(t *testing.T) {
ctx := createTestContext("invalid input")
// 测试错误处理
assert.NotPanics(t, func() {
errorProneHandler(ctx)
})
}
🔗 集成测试¶
完整流程测试¶
func TestCompleteWorkflow(t *testing.T) {
// 1. 初始化插件
engine := control.Register("testplugin", &control.Options{
Alias: "测试插件",
})
// 2. 注册处理器
engine.OnPrefix("test").Handle(testHandler)
// 3. 模拟消息处理
ctx := createTestContext("test hello")
testHandler(ctx)
// 4. 验证结果
// (这里需要模拟回复机制)
}
📊 性能测试¶
并发测试¶
func TestConcurrentExecution(t *testing.T) {
var wg sync.WaitGroup
numGoroutines := 100
for i := 0; i < numGoroutines; i++ {
wg.Add(1)
go func(id int) {
defer wg.Done()
ctx := createTestContext(fmt.Sprintf("test %d", id))
testHandler(ctx)
}(i)
}
wg.Wait()
}
🐛 调试技巧¶
调试日志¶
import "github.com/ruk1ng001/wxbot/engine/pkg/log"
func debugHandler(ctx *robot.Ctx) {
log.Debugf("收到消息: %s", ctx.MessageString())
// 业务逻辑
result := processMessage(ctx)
log.Infof("处理结果: %s", result)
}
📚 相关文档¶
**🧪 测试驱动开发,质量第一!** **用完整的测试保证插件的稳定性!**