You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4.5 KiB

集成测试运行指南

运行测试

1. 运行所有测试

cd D:\Projects\nnet
go test ./test/integration -v

2. 运行特定测试

# 运行基础测试
go test ./test/integration -v -run TestBasicServerClient

# 运行路由测试
go test ./test/integration -v -run TestRouter

# 运行协议测试
go test ./test/integration -v -run TestProtocol

# 运行拦截器测试
go test ./test/integration -v -run TestInterceptor

# 运行编解码器测试
go test ./test/integration -v -run TestCodec

3. 运行测试并显示覆盖率

go test ./test/integration -v -cover

4. 运行测试并生成覆盖率报告

go test ./test/integration -v -coverprofile=coverage.out
go tool cover -html=coverage.out -o coverage.html

5. 运行测试并设置超时

go test ./test/integration -v -timeout 60s

获取测试结果

1. 控制台输出

测试结果会直接输出到控制台,包括:

  • 测试名称
  • 测试状态PASS/FAIL
  • 测试日志t.Logf输出
  • 错误信息(如果有)

2. 保存测试结果到文件

# 保存到文件
go test ./test/integration -v > test_results.txt 2>&1

# 或者使用PowerShell
go test ./test/integration -v 2>&1 | Tee-Object -FilePath test_results.txt

3. 使用JSON格式输出

go test ./test/integration -v -json > test_results.json

如何将测试结果告知我

方式1: 复制测试输出

  1. 运行测试命令
  2. 复制控制台输出
  3. 将输出粘贴给我

方式2: 保存到文件并分享

  1. 运行测试并保存到文件:
    go test ./test/integration -v > test_results.txt 2>&1
    
  2. test_results.txt 文件内容分享给我

方式3: 只分享失败信息

如果测试失败,可以只分享失败的部分:

go test ./test/integration -v 2>&1 | Select-String -Pattern "FAIL|Error|timeout" -Context 5

测试结果格式说明

成功的测试

=== RUN   TestBasicServerClient
--- PASS: TestBasicServerClient (1.23s)

失败的测试

=== RUN   TestBasicServerClient
    integration_test.go:75: Response for 'hello': ""
    integration_test.go:77: 
        	Error Trace:	D:/Projects/nnet/test/integration/basic_test.go:77
        	Error:      	Received unexpected error:
        	            	read tcp 127.0.0.1:61899->127.0.0.1:61895: i/o timeout
        	Test:       	TestBasicServerClient
        	Messages:   	Request should succeed
--- FAIL: TestBasicServerClient (15.45s)

注意事项

  1. Windows防火墙: 第一次运行时Windows防火墙可能会弹出确认对话框需要点击"允许访问"

  2. 端口占用: 测试使用随机端口,通常不会出现端口冲突。如果出现端口占用错误,可以:

    • 等待几秒钟后重试
    • 检查是否有其他进程占用端口
  3. 超时设置: 测试默认超时时间为3秒。如果测试经常超时可以

    • 增加超时时间:-timeout 60s
    • 检查服务器启动时间
    • 查看KNOWN_ISSUES.md了解已知问题
  4. 日志级别: 测试使用info级别日志。如果需要更详细的日志可以

    • 修改测试代码中的日志级别为debug
    • 查看服务器日志输出

常见问题

1. 测试超时

现象: 测试超时,客户端无法收到响应

可能原因:

  • 服务器启动慢
  • 网络延迟
  • gnet AsyncWrite机制问题参见KNOWN_ISSUES.md

解决方法:

  • 增加超时时间
  • 检查服务器日志
  • 查看KNOWN_ISSUES.md

2. 端口冲突

现象: 错误信息显示端口已被占用

解决方法:

  • 等待几秒钟后重试
  • 检查是否有其他进程占用端口
  • 测试使用随机端口,通常不会冲突

3. 防火墙阻止

现象: Windows防火墙弹出确认对话框

解决方法:

  • 点击"允许访问"
  • 在防火墙设置中添加例外参见KNOWN_ISSUES.md

测试文件说明

  • basic_test.go - 基础测试(服务器-客户端通信、Echo路由、多客户端
  • router_test.go - 路由测试(帧数据路由、路由中间件)
  • protocol_test.go - 协议测试(协议版本管理)
  • interceptor_test.go - 拦截器测试(拦截器验证)
  • codec_test.go - 编解码器测试JSON、二进制编解码器
  • simple_test.go - 简单测试(用于调试)
  • integration_test.go - 测试工具函数
  • helper.go - 测试辅助函数

下一步

运行测试后,请将测试结果(特别是失败信息)分享给我,我会根据结果进行相应的修复和改进。