dpub33855 2014-05-16 15:32
浏览 71
已采纳

去lang测试事件监听器?

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/)?

  • 写回答

3条回答 默认 最新

  • drxrgundk062317205 2014-05-16 23:46
    关注

    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.


    Edit: I wrote a really simple wrapper over *testing.T and an event dispatching framework here: https://github.com/Jragonmiris/testevents

    It's totally untested (heh), but it should be relatively close to what you want. It aims to require only one or two extra method calls per test to hook up the dispatching code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题