Go:如何运行多个软件包的测试? [英] Go: how to run tests for multiple packages?

查看:151
本文介绍了Go:如何运行多个软件包的测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在src /下的子目录下有多个包,
运行每个包含 go test 的测试工作正常。



当试图使用运行所有测试时,请测试./... 测试正在运行,但失败..



这些测试是针对本地数据库服务器运行的,每个测试文件都有带db指针的全局变量。



我试图运行测试与 -parallel 1 来防止数据库中的争用,但测试仍然失败。



可能是什么问题在这里?



编辑:一些测试失败的数据库条目,我完全清除数据库之前和之后的每个测试。我可以想到为什么会发生这种情况的唯一原因是因为测试之间存在争用。



编辑2:



我的每个测试文件都有2个全局变量(使用mgo):

  var session * mgo.Session 
var db * mgo.Database

还具有以下设置和拆卸功能:



$ p $ func setUp(){
s,err:= cfg.GetDBSession()
if err!= nil {
panic(err)
}

session = s

db = cfg.GetDB(session)

db.DropDatabase( )
}

func tearDown(){
db.DropDatabase()

session.Close()
}

每个用 setUp() defer tearDown()



也是cfg:

  package cfg 

import(
labix.org/v2/mgo


func GetDBSession()(* mgo.Session,错误){
session,err:= mgo.Dial(localhost)

return session,err
}

func GetDB(session * mgo.Session)* mgo。数据库{
return session.DB(test_db)
}

编辑3:

我更改cfg以使用随机数据库,测试通过。
似乎来自多个包的测试并行运行。



可以强制 go test 在包中依次运行所有东西?

解决方案

显然运行 go test -p 1 按顺序运行一切(包括构建),我在 go help test go help testflag


I have multiple packages under a subdirectory under src/, running the tests for each package with go test is working fine.

When trying to run all tests with go test ./... the tests are running but it fails..

the tests are running against local database servers, each test file has global variables with db pointers.

I tried to run the tests with -parallel 1 to prevent contention in the db, but the tests still fail.

what can be the issue here?

EDIT: some tests are failing on missing DB entries, I completely clear the DB before and after each test. the only reason I can think of why this is happening is because of some contention between tests.

EDIT 2:

each one of my test files has 2 global variables (using mgo):

var session *mgo.Session
var db *mgo.Database

also it has the following setup and teardown functions:

func setUp() {
   s, err := cfg.GetDBSession()
   if err != nil {
       panic(err)
   }

   session = s

   db = cfg.GetDB(session)

   db.DropDatabase()
}

func tearDown() {
   db.DropDatabase()

   session.Close()
}

each tests startup with setUp() and defer tearDown()

also cfg is:

package cfg

import (
    "labix.org/v2/mgo"
)

func GetDBSession() (*mgo.Session, error) {
    session, err := mgo.Dial("localhost")

    return session, err
}

func GetDB(session *mgo.Session) *mgo.Database {
    return session.DB("test_db")
}

EDIT 3:

I changed cfg to use a random database, the tests passed. it seems that the tests from multiple packages are running somewhat in parallel.

is it possible to force go test to run everything sequentially across packages ?

解决方案

apparently running go test -p 1 runs everything sequentially (including build), I haven't see this argument in go help test or go help testflag

这篇关于Go:如何运行多个软件包的测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆