PS C:\Users\Lenovo\Desktop\QA-System\app\services\mongodbService> go test 2024/05/0213:27:51 Connected to MongoDB PASS ok QA-System/app/services/mongodbService 0.613s
go test -v
所以我们可以试试加上-v的参数,查看测试函数名称和运行时间
1 2 3 4 5 6
PS C:\Users\Lenovo\Desktop\QA-System\app\services\mongodbService> go test -v === RUN TestSaveAnswerSheet 2024/05/02 13:29:23 Connected to MongoDB --- PASS: TestSaveAnswerSheet (0.27s) PASS ok QA-System/app/services/mongodbService 0.547s
Go提供内置功能来检查你的代码覆盖率。我们可以使用go test -cover来查看测试覆盖率。例如(哎呀,这占比真低):
1 2 3 4 5
PS C:\Users\Lenovo\Desktop\QA-System\app\services\mongodbService> go test -cover 2024/05/0213:38:24 Connected to MongoDB PASS coverage: 9.4% of statements ok QA-System/app/services/mongodbService 0.639s
基准测试并不会默认执行,需要增加-bench参数,所以我们通过执行go test -bench=SaveAnswerSheet命令执行基准测试,输出结果如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
PS C:\Users\Lenovo\Desktop\QA-System\app\services\mongodbService> go test -bench=SaveAnswerSheet 2024/05/0214:08:16 Connected to MongoDB 2024/05/0214:08:16 Connected to MongoDB 2024/05/0214:08:16 Connected to MongoDB 2024/05/0214:08:17 Connected to MongoDB goos: windows goarch: amd64 pkg: QA-System/app/services/mongodbService cpu: AMD Ryzen 74800H with Radeon Graphics BenchmarkSaveAnswerSheet-162024/05/0214:08:17 Connected to MongoDB 2024/05/0214:08:17 Connected to MongoDB 2024/05/0214:08:18 Connected to MongoDB 2024/05/0214:08:19 Connected to MongoDB 3039870327 ns/op PASS ok QA-System/app/services/mongodbService 4.328s
PS C:\Users\Lenovo\Desktop\QA-System\app\services\mongodbService> go test -bench=SaveAnswerSheet -benchmem 2024/05/0214:10:55 Connected to MongoDB 2024/05/0214:10:55 Connected to MongoDB 2024/05/0214:10:55 Connected to MongoDB 2024/05/0214:10:55 Connected to MongoDB goos: windows goarch: amd64 pkg: QA-System/app/services/mongodbService cpu: AMD Ryzen 74800H with Radeon Graphics BenchmarkSaveAnswerSheet-162024/05/0214:10:56 Connected to MongoDB 2024/05/0214:10:56 Connected to MongoDB 2024/05/0214:10:57 Connected to MongoDB 2024/05/0214:10:58 Connected to MongoDB 2543956060 ns/op 10080 B/op 105 allocs/op PASS ok QA-System/app/services/mongodbService 4.374s