【问题标题】:Calling distinct test func in TestMain() in Golang在 Golang 的 TestMain() 中调用不同的测试函数
【发布时间】:2018-07-09 21:00:03
【问题描述】:

我正在尝试测试我的 http 控制器并使用 TestMain 函数来准备我的测试,但在我运行所有测试请求之前,我需要先运行 TestAuthUserController 测试,它会创建并授权用户。为此,我使用了 wrapper func,它可以帮助我调用 TestAuthUserController:

func TestMain(m *testing.M)  {
    //some prepearing steps
    AuthUserController()//create and authorize user before all other tests
    m.Run()
    fmt.Println("after all in main")
    dbMdm.End()
}

    //AuthUserController is a wrapper func to run TestAuthUserController before all other tests in TestMain func
func AuthUserController() func(t *testing.T){
    fmt.Println("in wrapper")
    return TestAuthUserController
}

这是我的TestAuthUserController

//TestAuthUserController tests in series creation of new user and his authorization
func TestAuthUserController(t *testing.T) {
    t.Run("testCreateUserSuccessBeforeAuthorize", testCreateUserSuccessBeforeAuthorize)
    t.Run("testAuthorizeUserSuccess", testAuthorizeUserSuccess)
}

当我运行命令 go test - 没关系! TestMain 调用它成功, 但是当我尝试单独运行一些测试时,例如go test -run TestSomeController 它失败了,因为TestAuthUserController 在这种情况下不会运行。

【问题讨论】:

  • 您要求 go 工具仅运行 TestSomeController,现在想知道为什么 TestAuthUserController 不运行?您得到的正是您所订购的。
  • 不要使用TestMain。只需让每个测试直接调用 setup 函数即可。
  • @Peter,我要求运行 TestSomeController,但是我的 TestAuthUserController 必须在 TestMain(m *testing.M) 中调用,并且在所有情况下都运行 TestMain...
  • 如果 TestAuthUserController 不是一个测试,而是一些设置代码,不要让它看起来像一个测试。
  • 我遇到了同样的问题。还没有解决方案。

标签: testing go


【解决方案1】:
func setupTestUserAndAuthorise(){
   // create user, authorise

   // return User
}

func deleteTestUser(user User){
   // delete user and other clean up actions
}

func TestCase1(t *testing.T){
    user := setupUserAndAuthorise()
    defer deleteTestUser(user)

    // use this user in test
}

您可以创建一个函数来创建一个新用户、授权并返回它。 测试运行后,可以调用清理函数删除用户等。

单元测试的执行顺序不应影响测试的结果。

【讨论】:

    【解决方案2】:

    我已经为测试文件创建了 main.go 文件和 main_test.go。在 main_test.go 文件中,它包含以下代码:

    func TestMain(m *testing.T)  {
        fmt.Println("Testing is running...")
        AuthUserController()
    }
    
    func AuthUserController() func(t *testing.T){
        fmt.Println("in wrapper")
        return TestAuthUserController
    }
    
    
    func testCreateUserSuccessBeforeAuthorize(m *testing.T)  {
        fmt.Println("create user... testCreateUserSuccessBeforeAuthorize")
    }
    
    func testAuthorizeUserSuccess(m *testing.T)  {
        fmt.Println("authorize user... testAuthorizeUserSuccess")
    }
    
    func TestAuthUserController(t *testing.T) {
        t.Run("testCreateUserSuccessBeforeAuthorize", testCreateUserSuccessBeforeAuthorize)
        t.Run("testAuthorizeUserSuccess", testAuthorizeUserSuccess)
    }
    

    当你运行这个命令时:go test -run TestAuthUserController

    //Here is the output:
    Testing is running... testCreateUserSuccessBeforeAuthorize
    Testing is running... testAuthorizeUserSuccess
    PASS
    

    希望这是您的预期。 :)

    【讨论】:

    • 在我的测试中这个案例也有效。但是,如果您将添加任何其他单元测试并以相同的顺序运行它,那么TestAuthUserController 将不会被调用...
    • 我在 main_test.go 文件中添加了 TestSomeController(t *testing.T) 并运行 go test -run TestSomeController 它也可以工作..
    • 我还创建了 name_test.go 并将 TestSomeController(m *testing.T)TestAnotherMain(m *testing.T) 放在一起,它可以工作还。请注意,如果您添加另一个单元测试(测试文件),您必须添加后缀“_test”。
    • 你的 func TestMain(m *testing.T) 接受 testing.T 指针代替 testing.M。我试过你的例子。但是当我运行go test -run TestAuthUserController 我有这个输出:create user... testCreateUserSuccessBeforeAuthorize authorize user... testAuthorizeUserSuccess 似乎程序没有调用TestMain
    • 如果你想调用TestMain,你可以像这样t.Run("testMain()", TestMain)把TestMain放在你的** TestAuthUserController**中并运行它再次去测试 - 运行 TestAuthUserController
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-02
    • 1970-01-01
    • 1970-01-01
    • 2020-02-16
    • 2021-12-28
    • 2018-04-19
    • 2022-11-22
    相关资源
    最近更新 更多