无法从非根目录运行测试。

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

Can not run tests from non-root folder

问题

我有一个看起来像这样的测试代码:

package tst

import (
	"testing"
	"github.com/demas/cowl-go/pkg/postgres"
	"log"
	"os"
	"fmt"
	"github.com/jmoiron/sqlx"
	"github.com/demas/cowl-go/pkg/quzx-crawler"
	"github.com/SlyMarbo/rss"
	"time"
    _ "github.com/lib/pq"
)

func TestMain(m *testing.M) {

	prepare()
	retCode := m.Run()
	os.Exit(retCode)
}

func prepare() {
	connectionString := fmt.Sprintf("user=%s password=%s host=%s port=%s dbname=%s sslmode=disable",
		os.Getenv("DBUSER"),
		os.Getenv("DBPASS"),
		os.Getenv("DBHOST"),
		os.Getenv("DBPORT"),
		os.Getenv("DBNAME"))

	db, err := sqlx.Open("postgres", connectionString)
	if err != nil {
		log.Fatal(err)
	}

	db.Exec(`DELETE FROM Settings`)
	db.Exec(`DELETE FROM HackerNews`)
    // ....
}

如果我将这些测试代码保留在项目的根目录中,测试是正常工作的。但是,如果我将它们移动到tst文件夹中,我会收到以下错误信息:

D:\development\gopath\src\github.com\demas\cowl-go\tst>go test -v
2017/03/31 16:30:06 sql: unknown driver "postgres" (forgotten import?)
exit status 1
FAIL    github.com/demas/cowl-go/tst    0.085s

为什么会出现这个错误?

英文:

I have a tests which looks like:

package tst

import (
	"testing"
	"github.com/demas/cowl-go/pkg/postgres"
	"log"
	"os"
	"fmt"
	"github.com/jmoiron/sqlx"
	"github.com/demas/cowl-go/pkg/quzx-crawler"
	"github.com/SlyMarbo/rss"
	"time"
    _ "github.com/lib/pq"
)

func TestMain(m *testing.M) {

	prepare()
	retCode := m.Run()
	os.Exit(retCode)
}

func prepare() {
	connectionString := fmt.Sprintf("user=%s password=%s host=%s port=%s dbname=%s sslmode=disable",
		os.Getenv("DBUSER"),
		os.Getenv("DBPASS"),
		os.Getenv("DBHOST"),
		os.Getenv("DBPORT"),
		os.Getenv("DBNAME"))

	db, err := sqlx.Open("postgres", connectionString)
	if err != nil {
		log.Fatal(err)
	}

	db.Exec(`DELETE FROM Settings`)
	db.Exec(`DELETE FROM HackerNews`)
    // ....
}

Tests works fine if I keep in the root project folder, but if I move them to tst folder I get error message:

D:\development\gopath\src\github.com\demas\cowl-go\tst>go test -v
2017/03/31 16:30:06 sql: unknown driver "postgres" (forgotten import?)
exit status 1
FAIL    github.com/demas/cowl-go/tst    0.085s

Why ?

答案1

得分: 4

如@JimB在评论中已经提到的,这个错误意味着你在使用sqlx.Open打开数据库连接之前没有导入数据库驱动程序。在你的情况下,可以通过添加_ "github.com/lib/pq"导入规范来解决这个问题。

如果即使添加了这个导入规范,你仍然看到相同的错误,那么这意味着你的某个依赖项也在尝试在没有先导入必要的驱动程序的情况下打开数据库连接。

请注意,虽然log.Fatal是一种很好和干净的停止程序的方式,但有时可能不够完善,正如你已经知道的那样。你可能想考虑改用panic,它的输出更加混乱,但另一方面,你将得到引发 panic 的行号和文件名,并最终学会快速解析它。

英文:

As already mentioned by @JimB in the comments, the error means that you're trying to open a db connection, using sqlx.Open, without first importing a db driver. This can be fixed by, in your case, adding this _ "github.com/lib/pq" import spec.

If, even after adding that import, you're still seeing the same error, then that means that one of your dependencies is also trying to open a db connection without first importing the necessary driver.

Please note that while log.Fatal is a nice and clean way to stop your program it can sometimes be lacking, as you already know. You might want to consider using panic instead, its output is much more chaotic but, on the other hand, you'll get the line number and file name that caused the panic and eventually you'll learn to parse it quickly.

huangapple
  • 本文由 发表于 2017年3月31日 21:34:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/43142080.html
匿名

发表评论

匿名网友

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

确定