dongzhang1839
2018-02-10 00:44
浏览 19
已采纳

Golang中的单元测试模式识别

I am building unit testing of the application that uses os.Exit(1), once os.Exit(1) executes remaining test file are skipped with go test, I am thinking about suppressing os.Exit during the unit test execution only. I wonder how I can determine that app has been bootstrap from go test runner?

图片转代码服务由CSDN问答提供 功能建议

我正在为使用os.Exit(1)(一次是os.Exit(1))的应用程序构建单元测试 执行剩余的测试文件会被go test跳过, 我正在考虑仅在单元测试执行期间抑制os.Exit。 我想知道如何确定该应用已从go测试运行程序中启动?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanpai1033 2018-02-10 00:52
    已采纳

    You will find various ways to unit test method with os.Exit() in "Testing os.Exit scenarios in Go with coverage information (coveralls.io/Goveralls)".

    It uses a function which is:

    • os.Exit() when you are not testing

      var osExit = os.Exit
      
    • and "yourOsExit" when you are testing.

      func TestCrasher(t *testing.T) {
          // Save current function and restore at the end:
          oldOsExit := osExit
          defer func() { osExit = oldOsExit }()
          osExit = myExit
      
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题