登录
首页 >  Golang >  Go问答

合并多个 Go 包中的数据库设置,使其只运行一次的方法是什么?

来源:stackoverflow

时间:2024-02-10 11:45:22 464浏览 收藏

从现在开始,努力学习吧!本文《合并多个 Go 包中的数据库设置,使其只运行一次的方法是什么?》主要讲解了等等相关知识点,我会在golang学习网中持续更新相关的系列文章,欢迎大家关注并积极留言建议。下面就先一起来看一下本篇正文内容吧,希望能帮到你!

问题内容

我正在尝试在数据库中创建一些对象,以便我的测试可以使用一些数据。我已将设置逻辑放入包 testsetup 中。但是,我发现 go test 将每个包作为完全独立的实例运行,因此即使我在 testsetup 包中使用 sync.oncesetup 仍然运行多次,因为每个包的测试作为单独的 go 实例运行。我真的很想继续并行运行我的测试,因为它要快得多,所以我目前不考虑关闭并行化。有没有一种干净的方法可以做到这一点?

此时我什至开始考虑肮脏的黑客攻击,例如使用 shell 脚本来实现操作系统级同步。

这是我的包结构:

testsetup
    testsetup.go
package1
    package1.go
    package1_test.go
package2
    package2.go
    package2_test.go

这是我的 testsetup 函数的简化版本:

var onceSetup sync.Once
var data model.MockData

func Setup() model.MockData {
    onceSetup.Do(createData)
    return data
}

func createData() {
    // Do some SQL calls to create the objects. We only want to do this once.

    data = model.Data{
        Object1: ...,
        Object2: ...,
    }
}

正确答案


这是可以做到的,但可能不值得,你必须自己决定。

您将需要一个实现“测试注册表”和“测试运行程序”的包,以及另一个作为“入口点”的包,将它们连接在一起并启动运行程序。

生成的结构可能如下所示:

../module
├── app
│   ├── pkg1
│   │   ├── foo.go
│   │   ├── ...
│   │   └── tests
│   │       ├── test_foo.go
│   │       ├── ...
│   │       └── pkg1_test.go
│   └── pkg2
│       ├── ...
│       ├── bar.go
│       └── tests
│           ├── ...
│           ├── test_bar.go
│           └── pkg2_test.go
├── go.mod
├── internal
│   └── testutil
│       ├── registry.go # the test registry
│       └── runner.go # the test runner
└── tests
    └── start_test.go # the test entrypoint

首先,让我们考虑一下完成后入口点会是什么样子。您可能不喜欢所看到的内容,在这种情况下,您可能应该忽略答案的其余部分。

文件module/tests/start_test.go

package tests

import (
    "testing"

    // use the blank identifier for "side-effect-only" imports
    _ "module/app/pkg1/tests"
    _ "module/app/pkg2/tests"
    // ...

    "module/internal/testutil"
)

func test(t *testing.t) {
    testutil.testall(t)
}

接下来,module/internal/testutil/registry.go中的注册表:

package testutil

import (
    "path/filepath"
    "runtime"
    "testing"
)

//                  v: the directory of a package
//                          v: the files in a directory
//                            v: the tests in a file
var tests = make(map[string][][]func(*testing.t))

func register(ft ...func(*testing.t)) int {
    // use the directory of the caller's file
    // to map the tests. why this can be useful
    // will be shown later.
    _, f, _, _ := runtime.caller(1)
    dir := filepath.dir(f)

    tests[dir] = append(tests[dir], ft)

    // this is not necessary, but a function with a return
    // can be used in a top-level variable declaration which
    // can be used to avoid unnecessary init() functions.
    return 0
}

module/internal/testutil/runner.go 中的运行程序:

package testutil

import (
    "testing"
)

func testall(t *testing.t) {
    // todo setup ...

    defer func() {
        // todo teardown ...
    }()

    // run
    for _, dir := range tests {
        for _, file := range dir {
            for _, test := range file {
                test(t)
            }
        }
    }
}

现在是单独的包,例如module/app/pkg1/tests/test_foo.go

package tests

import (
    "testing"

    "module/internal/testutil"
)

var _ = testutil.register(
    testfoo1,
    testfoo2,
)

func testfoo1(t *testing.t) {
    // ...
}

func testfoo2(t *testing.t) {
    // ...
}

就是这样,您现在可以转到 module/tests“入口点”并运行:

go test

附录 #1

如果您想保留单独测试各个包的能力 那么也可以集成。

首先,在 module/internal/testutil/runner.go 中向运行器添加一个新函数:

package testutil

import (
    // ...
    "path/filepath"
    "runtime"
)

// ...

func testpkg(t *testing.t) {
    // now the directory of the caller's file
    // comes in handy. we can use it to make
    // sure no other tests but the caller's
    // will get executed.
    _, f, _, _ := runtime.caller(1)
    dir := filepath.dir(f)

    // todo setup ...

    defer func() {
        // todo teardown ...
    }()

    // run
    for _, file := range tests[dir] {
        for _, test := range file {
            test(t)
        }
    }
}

并在单独的测试包中添加一个测试文件,例如module/app/pkg1/tests/pkg1_test.go

package tests

import (
    "testing"

    "module/internal/testutil"
)

func test(t *testing.t) {
    testutil.testpkg(t)
}

就是这样,现在您可以将 cd 放入 module/app/pkg1/tests 并运行:

go test

附录#2

现在,各个软件包都有自己的 _test.go 文件,如果您想使用 go 测试模块/... 执行所有中的测试,您就回到了原点。模块,因为这不仅会运行入口点,还会导致单独执行各个测试包。

但是,您可以使用简单的环境变量来解决该问题。只需对 testutil.testpkg 函数进行一个小调整:

package testutil

import (
    // ...
    "os"
)

// ...

func testpkg(t *testing.t) {
    if os.getenv("skippkg") == "yes" {
        return
    }

    // ...
}

现在...

# ... the following will work as you'd expect
skippkg=yes go test module/...
go test module/tests
go test module/app/pkg1/tests

今天带大家了解了的相关知识,希望对你有所帮助;关于Golang的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>