Go语言测试事件监听器?

huangapple go评论84阅读模式
英文:

Go lang test events listener?

问题

这个问题是关于Go语言测试的。你可能知道,大多数主流语言都有自己的xUnit框架。这些框架中的大多数都有监听测试运行事件的能力(例如,测试用例开始、测试用例结束、测试失败等)。这通常被称为测试事件监听器,主要用于编写框架的第三方扩展。

我的问题是:是否有类似的方法可以连接到标准的Go语言测试框架事件(http://golang.org/pkg/testing/)?

英文:

This question is about Go language testing. As you probably know most of mainstream languages have their own xUnit frameworks. Most of these frameworks have an ability to listen to test run events (e.g. test case started, test case finished, test failed and so on). This is often called test event listener and is mainly used when writing third-party extensions for frameworks.

My question: is there any similar way to attach to standard Go language testing framework events (http://golang.org/pkg/testing/)?

答案1

得分: 1

我不知道。了解你想要实现什么目标会有所帮助,你需要这种功能吗?

英文:

Not that I know of. It would help to know what you are trying to accomplish such that you need this capability?

答案2

得分: 1

不是开箱即用的,但你自己搭建起来应该不难。任何名为init的函数都保证在其他任何代码之前运行,测试也是如此。

在你的测试文件中:

var listener pkg.EventListener
func init() {
    pkg.SetupMyFramework()
    listener = pkg.Listener()
}

然后在任何测试中:

func TestXxx(t *testing.T) {
    listener.Dispatch(pkg.MakeEvent("TestXxx", pkg.TestStarted))

    err := DoSomething()
    if err != nil {
        listener.Dispatch(pkg.MakeEvent("TestXxx", pkg.TestFailed))
        t.Fatal("Test failed")
    }

    listener.Dispatch(pkg.MakeEvent("TestXxx", pkg.TestPassed))
}

当然,你可以根据需要进行扩展(使用通道,在Fatal周围创建包装函数以减少冗余等),但这就是你可以集成它的要点。

英文:

Not out of the box, but it shouldn't be hard to rig up yourself. Any function named init is guaranteed to be run before anything else, and that's true for tests as well.

In your test file:

var listener pkg.EventListener
func init() {
    pkg.SetupMyFramework()
    listener = pkg.Listener()
}

Then in any test

func TestXxx(t *testing.T) {
    listener.Dispatch(pkg.MakeEvent("TestXxx", pkg.TestStarted))

    err := DoSomething()
    if err != nil {
        listener.Dispatch(pkg.MakeEvent("TestXxx", pkg.TestFailed))
        t.Fatal("Test failed")
    }

    listener.Dispatch(pkg.MakeEvent("TestXxx", pkg.TestPassed))
}

You can, of course, extend this however you want (using channels, making wrapper functions around Fatal to make this less verbose, etc), but that's the gist of how you can integrate it.

答案3

得分: 0

go1.10即将发布,其中一个新功能是go test -json(发布说明https://tip.golang.org/doc/go1.10#test)。

使用go test -json可以解析测试输出并将其发送到第三方框架。

英文:

go1.10 is going to be released soon, and one new feature is go test -json (release notes https://tip.golang.org/doc/go1.10#test).

Using go test -json you can parse the test output and send it to a third party framework.

huangapple
  • 本文由 发表于 2014年5月16日 23:32:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/23699237.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定