我可以从 TestMain 输出日志消息吗?
我想编写一个集成测试来测试向数据库写入数据和从数据库读取数据。为此,我需要第一个测试函数来连接并准备测试数据库;如果成功,则运行其他测试,然后对其进行查询。
因此,我实现了一个 TestMain(m *testing.M)
,它创建一个数据库并在其上运行迁移,如果没有错误,我会运行 m.Run()
。
但它没有像 testing.T
那样的任何 m.Print
或 m.Fatal
方法。如果我尝试使用 log.Print()
记录某些内容,它不会获得输出(可能是因为没有测试可以被正确标记为失败)。
我不喜欢使用常规测试功能创建数据库的原因是,常规测试当然不能阻止其他测试的运行,因此该文件中的所有其他测试都将运行并困扰输出消息,因为他们都会失败。
那么我如何从 TestMain 中注销一些内容呢?我的 TestMain 仍然可能以多种方式失败,我想在输出中看到它是什么。
显示 TestMain
的输出未显示的示例:
$ ls
go.mod main_test.go
$ cat go.mod
module App
go 1.16
$ cat main_test.go
package main
import (
"testing"
"log"
)
func TestMain(m *testing.M) {
log.Print("hello")
}
$ go test ./.
ok App 0.147s
$
I want to write an integration test which tests writing and reading data to and from a database. In order to do this, I need my first test function to connect and prepare a test database; and if that is a success, let the other tests run, which will then be doing queries on it.
So I implemented a TestMain(m *testing.M)
which creates a database and runs migrations on it, and if there's no errors, I run m.Run()
.
But it doesn't have any m.Print
or m.Fatal
methods like testing.T
does. And if I try to log something with log.Print()
, it doesn't get output (probably because no testing could have been properly marked as failing).
And the reason why I don't like the creation of the database with a regular test function, is because a regular test of course cannot stop other tests from running, so all my other tests from that file will run and plague the output messages, since they will all fail.
So how do I log something out from TestMain? My TestMain can still fail in multiple ways, and i would like to see in the output what it was.
Example to show TestMain
's output not being displayed:
$ ls
go.mod main_test.go
$ cat go.mod
module App
go 1.16
$ cat main_test.go
package main
import (
"testing"
"log"
)
func TestMain(m *testing.M) {
log.Print("hello")
}
$ go test ./.
ok App 0.147s
$
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果测试以非零代码退出,则使用
log.Print()
打印的消息似乎会显示,因此这种情况最好通过在日志行之后以非零代码退出来解决,例如所以:It seems that messages printed with
log.Print()
will display if the test exits with a non-zero code, so this situation is best solved by exiting with non-zero right after the log line, like so:您需要
-v
标志:注意:
.
相当于./.
如果您想在所有子目录上运行测试,请使用 < code>./... 就像这样:
这是为什么?由于您正在所谓的包列表模式(即
./.
)中运行测试 - 来自go help test
中的文档:这是您注意到的行为。进一步阅读:
注意:
go test
单独(即没有包列表)将允许从TestMain
输出日志记录,如log.Print
。编辑
以下是我通常为测试进行飞行前设置的方式:
如果任何准备失败,则
log.Fatal
确保记录原因并记录原因。程序终止。You need the
-v
flag:Note:
.
is equivalent to./.
If you want to run tests on all sub-directories use
./...
like so:Why is this? Since you are running tests in what's call package list mode (i.e.
./.
) - from the docs ingo help test
:This is the behavior you have noted. Reading further:
Note:
go test
alone (i.e. no package list) will allow output logging fromTestMain
likelog.Print
.EDIT
here's how I typically do pre-flight setup for my tests:
if any of the prep fails, then
log.Fatal
ensures the reason is logged & the program terminates.