Testing 如何按顺序运行golang测试?

Testing 如何按顺序运行golang测试?,testing,go,Testing,Go,当我运行go test时,我的输出: --- FAIL: TestGETSearchSuccess (0.00s) Location: drivers_api_test.go:283 Error: Not equal: 200 (expected) != 204 (actual) --- FAIL: TestGETCOSearchSuccess (0.00s)

当我运行
go test
时,我的输出:

--- 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)
但在我再次运行
go test
之后,我的所有测试都通过了

只有当我重置mysql数据库,然后第一次运行
go test
时,测试才会失败

对于每个
GET
请求,我都会先执行
POST
请求,以确保数据库中创建了数据


有人能帮我确定测试是按顺序运行的吗?也就是说,
POST
请求在
GET
请求之前运行?

实现这一点的最佳方法是创建一个
TestMain
,如图所示


您不能/不应该依赖于测试执行顺序。测试的执行顺序没有定义,使用它可以从运行中排除测试,因此您无法保证它们会运行

例如,以下命令将仅运行名称包含
'W'
字母的测试:

go test -run W
还请注意,如果某些测试函数使用该方法标记自己有资格并行执行,go工具将重新排序测试,以首先运行非并行测试,然后在某些情况下并行运行并行测试(由测试标志控制,如
-p
)。你可以在这个答案中看到这样的例子:

测试应该相互独立。如果一个测试函数具有先决条件,则不能在另一个测试函数中完成/实现该先决条件

在运行测试功能之前执行其他任务的选项:

  • 您可以将其放在测试函数本身中
  • 您可以将它放在
    \u test.go
    文件本身的包
    init()
    函数中。这将在开始执行测试函数之前运行一次
  • 您可以选择实现一个将首先被调用的函数,在该函数中,您可以在调用之前进行额外的设置,以触发测试函数的执行
  • 您可以混合使用上述选项
在包
init()
TestMain()
中,您应该检查数据库是否已初始化(插入了测试记录),如果未初始化,则插入测试记录


请注意,从Go 1.7开始,您可以使用定义子测试执行顺序的子测试。有关详细信息,请参阅blog post:,以及包的包文档。

对于第三方库,如和,使用plain Golang 1.7,您可以按顺序运行测试。你可以读更多

假设您从一个RESTAPI获得了一个
user
包,您想要测试它。为了能够测试登录处理程序,您需要测试create处理程序。通常我会在
user\u测试中使用它。去

type UserTests struct { Test *testing.T}
func TestRunner(t *testing.T) {

    t.Run("A=create", func(t *testing.T) {
        test:= UserTests{Test: t}
        test.TestCreateRegularUser()
        test.TestCreateConfirmedUser()
        test.TestCreateMasterUser()
        test.TestCreateUserTwice()
    })
    t.Run("A=login", func(t *testing.T) {
        test:= UserTests{Test: t}
        test.TestLoginRegularUser()
        test.TestLoginConfirmedUser()
        test.TestLoginMasterUser()
    })

}
然后,我可以将方法附加到UserTest类型,该类型不会由任何
\u test.go
文件中的
go test
命令执行

func (t *UserTests) TestCreateRegularUser() {
    registerRegularUser := util.TableTest{
        Method:      "POST",
        Path:        "/iot/users",
        Status:      http.StatusOK,
        Name:        "registerRegularUser",
        Description: "register Regular User has to return 200",
        Body: SerializeUser(RegularUser),
    }
    response := util.SpinSingleTableTests(t.Test, registerRegularUser)
    util.LogIfVerbose(color.BgCyan, "IOT/USERS/TEST", response)
}

对于那些像我这样因为同时运行多个并发测试而遇到问题的人来说。我找到了一种方法来限制并行运行的最大测试次数:

go测试-p1
这样,您的测试将一个接一个地按顺序运行


您能否通过这种方式对测试进行分组来确认顺序是否得到了实际控制?文档说您可以控制并行性,但不是说它们必须按照调用
t.run
的顺序运行。比赛条件很难核实。。。如果我发现任何确凿的证据,我都会回复。@Three他们肯定是按顺序运行的。您基本上是以这种方式运行单个测试。这对于单元测试是有意义的,在单元测试中,您正在测试模拟数据源,但是当您有集成测试时,让集成测试按顺序运行可能是一个合理的要求。
go test -run ''      # Run all tests.
go test -run Foo     # Run top-level tests matching "Foo", such as "TestFooBar".
go test -run Foo/A=  # For top-level tests matching "Foo", run subtests matching "A=".
go test -run /A=1    # For all top-level tests, run subtests matching "A=1".
type UserTests struct { Test *testing.T}
func TestRunner(t *testing.T) {

    t.Run("A=create", func(t *testing.T) {
        test:= UserTests{Test: t}
        test.TestCreateRegularUser()
        test.TestCreateConfirmedUser()
        test.TestCreateMasterUser()
        test.TestCreateUserTwice()
    })
    t.Run("A=login", func(t *testing.T) {
        test:= UserTests{Test: t}
        test.TestLoginRegularUser()
        test.TestLoginConfirmedUser()
        test.TestLoginMasterUser()
    })

}
func (t *UserTests) TestCreateRegularUser() {
    registerRegularUser := util.TableTest{
        Method:      "POST",
        Path:        "/iot/users",
        Status:      http.StatusOK,
        Name:        "registerRegularUser",
        Description: "register Regular User has to return 200",
        Body: SerializeUser(RegularUser),
    }
    response := util.SpinSingleTableTests(t.Test, registerRegularUser)
    util.LogIfVerbose(color.BgCyan, "IOT/USERS/TEST", response)
}