如何依次运行golang测试? [英] How to run golang tests sequentially?
问题描述
当我运行去测试
,我的输出:
- - FAIL:TestGETSearchSuccess(0.00s)
位置:drivers_api_test.go:283
错误:不等于:200(预计)
!= 204(实际)
--- FAIL:TestGETCOSearchSuccess(0.00s)
位置:drivers_api_test.go:391
错误:不等于:200(预计)
!= 204(实际)
但是在我运行再次测试
后,所有的测试都通过了。
测试仅在重置我的mysql数据库时失败,然后首次运行 go test
。
对于每个 GET
请求,我都会执行 POST
请确保在数据库中创建了数据。
任何人都可以帮助我确定测试顺序运行吗?那就是 POST 请求是在
GET
请求之前运行的?
您不能/不应该依赖测试执行顺序。未定义执行测试的顺序,并且使用测试标志可以从运行中排除测试,所以你不能保证它们可以运行。
例如,以下命令将只运行其名称包含'W'
字母:
go test -run W
另请注意,如果某些测试函数使用 T.Parallel()
方法,go工具将重新排序测试首先运行非平行测试,然后在某些情况下并行运行并行测试(由 -p
>等测试标志控制)。你可以在这个答案中看到这个例子:测试是在Go还是一个一个地执行并行?
测试应该彼此独立。如果测试函数具有先决条件,那么不能在另一个测试函数中完成/实现。
在测试函数运行之前执行其他任务的选项: strong>
init()
函数,在 _test.go
文件本身中。在测试函数开始执行之前,这将运行一次。
M.Run()
触发执行测试功能。
TestMain()
你应该检查你的数据库是否被初始化(有插入测试记录)不要插入测试记录。注意,从Go 1.7开始,您可以使用子测试来定义子测试的执行顺序。有关详细信息,请参阅博客文章:使用子测试和子基准以及 测试
包。
When I run go test
, my output:
--- FAIL: TestGETSearchSuccess (0.00s)
Location: drivers_api_test.go:283
Error: Not equal: 200 (expected)
!= 204 (actual)
--- FAIL: TestGETCOSearchSuccess (0.00s)
Location: drivers_api_test.go:391
Error: Not equal: 200 (expected)
!= 204 (actual)
But after I run go test
again, all my tests pass.
Tests fail only when I reset my mysql database, and then run go test
for the first time.
For every GET
request, I do a POST
request before to ensure that there is data created in the DB.
Could anyone help me with how to make sure that tests are run sequentially? That is the POST
requests are run before the GET
requests?
You can't / shouldn't rely on test execution order. The order in which tests are executed is not defined, and with the use of testing flags it is possible to exclude tests from running, so you have no guarantee that they will run at all.
For example the following command will only run tests whose name contains a 'W'
letter:
go test -run W
Also note that if some test functions mark themselves eligible for parallel execution using the T.Parallel()
method, the go tool will reorder the tests to first run non-parallel tests, and then run parallel tests in parallel under certain circumstances (controlled by test flags like -p
). You can see examples of this in this answer: Are tests executed in parallel in Go or one by one?
Tests should be independent from each other. If a test function has prerequisites, that cannot be done/implemented in another test function.
Options to do additional tasks before a test function is run:
- You may put it in the test function itself
- You may put it in a package
init()
function, in the_test.go
file itself. This will run once before execution of test functions begins. - You may choose to implement a
TestMain()
function which will be called first and in which you may do additional setup before you callM.Run()
to trigger the execution of test functions. - You may mix the above options.
In your case in package init()
or TestMain()
you should check if your DB is initialized (there are test records inserted), and if not, insert the test records.
Note that starting with Go 1.7, you may use subtests in which you define execution order of subtests. For details see blog post: Using Subtests and Sub-benchmarks, and the package doc of the testing
package.
这篇关于如何依次运行golang测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!