普通网友 2013-04-09 09:07
浏览 28
已采纳

我如何在通用的测试套件中包含多个软件包?

When I'm writing an interface, its often convenient to define my tests in the same package as the interface, and then define multiple packages that implement the interface set, eg.

package/
package/impl/x <-- Implementation X
package/impl/y <-- Implementation Y

Is there an easy way to run the same test suite (in this case, located in package/*_test.go) in the sub packages?

The best solution I've come up with so far is to add a test package:

package/tests/

Which implements the test suite, and a test in each of the implementations to run the tests, but this has two downsides:

1) The tests in package/tests are not in _test.go files, and end up being part of the actual library, documented by godoc, etc.

2) The tests in package/tests are run by a custom test runner, which has to basically duplicate all the functionality of 'go test' to scan for go tests and run them.

Seems like a pretty tacky solution.

Is there is a better way of doing this?

  • 写回答

3条回答 默认 最新

  • douhan9191 2013-04-09 15:14
    关注

    I don't really dislike the idea to use a separate testing library. If you have an interface and you have generic tests for each interface, other people that implement that interface might like to use these tests as well.

    You could create a package "package/test" that contains a function

    // functions needed for each implementation to test it
    type Tester struct {
        func New() package.Interface
        func (*package.Interface) Done()
        // whatever you need. Leave nil if function does not apply
    }
    
    func TestInterface(t *testing.T, tester Tester)
    

    Notice that the signature of TestInterface does not match to what go test expects. Now, for each package package/impl/x you add one file generic_test.go:

    package x
    
    import "testing"
    import "package/test"
    
    // run generic tests on this particular implementation
    func TestInterface(t *testing.T) {
        test.TestInterface(t,test.Tester{New:New})
    }
    

    Where New() is the constructor function of your implementation. The advantage with this scheme is that

    1. Your tests are reusable for whoever implements your interface, even from other packages
    2. It is immediately obvious that you run the generic test suite
    3. The test cases are where the implementation is and not at another, obscure place
    4. The code can be adapted easily if one implementation needs special initialization or similar stuff
    5. It's go test compatible (big plus!)

    Of course, in some cases you need a more complicated TestInterface function, but this is the basic idea.

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

报告相同问题?

悬赏问题

  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳