【问题标题】:Go not running some tests去不运行一些测试
【发布时间】:2021-11-18 00:22:04
【问题描述】:

我是 Golang 新手,我正在尝试从 golang 应用程序运行一些测试。 测试在使用 Golang 1.12 的 docker 容器中运行。

我的问题是某些测试似乎运行正常,但其他测试却没有。

例子:

我有一个函数我想故意失败。

func TestLol(t *testing.T) {
    assert.EqualValues(t, 1, 2)
    t.Fail()
}

当我使用“docker run ... go test -v ./...”执行 docker 容器时,它应该运行所有测试,并且关于这个函数的特定它应该失败,但是它没有发生什么失败。 Golang 只是在测试旁边记录一个“ok”。

然后我尝试只运行包含应该失败的测试文件的文件夹。

日志:

ok      github.com/eventials/csw-notifications/services 0.016s
2021/09/25 21:08:44 Command finished successfully.
Tests exited with status code: 0
Stopping csw-notifications_db_1     ... done
Stopping csw-notifications_broker_1 ... done
Going to remove csw-notifications_app_run_ed70597b5c20, csw-notifications_db_1, csw-notifications_broker_1
Removing csw-notifications_app_run_ed70597b5c20 ... done
Removing csw-notifications_db_1                 ... done
Removing csw-notifications_broker_1             ... done 

我的问题是,为什么 golang 不针对该文件输出任何带有 FAIL 消息的日志?

我认为这与这个问题有些相关,但由于它没有收到任何答复,我正在重新发布它。

Why the tests are not running ? ( Golang ) - goapp test - bug?

编辑:我正在对这个问题进行更清楚的编辑。

【问题讨论】:

  • 你在哪个文件中添加了TestLol?你说“在这个例子中 /services/ 没有运行”。但是在输出的底部,它说ok github.com/.../app/services 0.013s 表示服务已运行并且所有测试都通过了。
  • 我已经添加了两个文件。我说它没有运行,因为它应该失败,但它没有。所以我猜这个测试并没有通过所有的测试功能。

标签: unit-testing go testing


【解决方案1】:

你可以试试加 -timeout 如果您的测试文件包含具有相同名称的 FuncTest 重命名。 您可以尝试将输出更改为go test -v -json ./...

    -timeout d
        If a test binary runs longer than duration d, panic.
        If d is 0, the timeout is disabled.
        The default is 10 minutes (10m).

【讨论】:

  • 我认为我得到了正确的结果。 {"Time":"2021-09-25T20:51:09.137402228Z","Action":"output","Package":"github.com/.../.../services","Output":"ok \tgithub.com/..../.../services\t0.015s\n"} {"Time":"2021-09-25T20:51:09.137427418Z","Action":"fail","Package":"github.com/.../.../services","Elapsed":0.015} 2021/09/25 20:51:09 Command finished successfully.你能解释一下为什么有些测试在测试失败时有一个“FAIL”日志,而有些则没有?
  • @BrenoAlves 您看到 PASS 是因为您的测试没有收到失败的信号。也许您创建了一个测试并忘记了发送 t.Fail() 或 IF 内的条件不返回 t.Fail() 或类似。试试这个:if assert.EqualValues(t, 1, 2) { // ok it is equal } else { // error not equal t.Fail() }assert.Equal 将返回 true 或 false,你需要 if 来决定接下来会发生什么。
  • @BrenoAlves create func TestName(t *testing.T) { t.Fail()} Createa another one func ExampleName() { fmt.Println("hi") } //Output: hi 要查看测试中的一些日志,您需要停止使用 fmt.Println 并更改为 log 或 logf。在 t.Fail() t.Logf("Expected %d, got %d", expected, actual) 之前尝试这个
【解决方案2】:

main.go

func Start(s ...string) {
  fmt.Println(s)
}

main_test.go

func TestStart(t *testing.T) {
    Start("rocket")

    if 0 == 1 {
        t.Log("Houston, we have a problem")
        t.Fail()
    }
}

func ExampleStart() {
    fmt.Println("Ground Control to Major Tom")
    // Output:
    // Ground Control to Major Tom
}

将 if 条件更改为 0 == 0 以查看 Fail with somelogs。

【讨论】:

  • 在我的测试函数中,我调用了 t.Fail(),但没有锻炼。我发现导入导致无法运行测试文件,我不知道为什么
【解决方案3】:

我发现导入导致了这个问题。

每个导入 porthos-go 或 porthos-go/mock 的测试文件都没有运行测试文件。删除这些导入解决了我的问题。

库:https://github.com/porthos-rpc/porthos-go

我仍然不知道为什么,但是当我这样做时,我会更新这个答案。

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center。
猜你喜欢
  • 2021-10-10
  • 1970-01-01
  • 1970-01-01
  • 2011-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-06
相关资源
最近更新 更多