不运行某些测试

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

Go not running some tests

问题

我是你的中文翻译助手,以下是翻译好的内容:

我是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消息的日志?

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

https://stackoverflow.com/questions/22772907/why-the-tests-are-not-running-golang-goapp-test-bug

编辑:我编辑了这个问题以使其更清晰。

英文:

I'm new to Golang and I'm trying to run some tests from a golang app.
Tests are running at a docker container with a Golang 1.12.

My problem is that some tests appear to be running correctly, but others do not.

Example:

I have a function that I wanted to fail on purpose.

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

When I execute the docker container with a "docker run ... go test -v ./..." it should run all tests and about this function on specific it should fail, but what happens that it doesn't fail. Golang just log a "ok" beside the test.

Then I tried to run only the folder with test file that should fail.

Log:

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 

My question is why golang dosn't output any log with a FAIL message for this file in specific?

I think it's somewhat related to this question, but as it didn't received any answear I'm reposting it.

https://stackoverflow.com/questions/22772907/why-the-tests-are-not-running-golang-goapp-test-bug

EDIT: I'm editting to this question be more clear.

答案1

得分: 0

你可以尝试添加"-timeout"参数。如果你的测试文件中包含了同名的FuncTest,请进行重命名。你可以尝试将输出改为"go test -v -json ./..."。

	-timeout d
	    如果一个测试二进制文件运行时间超过了d,就会发生panic。
	    如果d为0,则禁用超时。
	    默认超时时间为10分钟(10m)。
英文:

You can try add -timeout
If your test files contain FuncTest with samenames rename.
You can try change output to 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).

答案2

得分: 0

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
}

Change if condition to 0 == 0 to see Fail with somelogs.

将if条件更改为0 == 0以查看带有一些日志的失败。

英文:

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
}

Change if condition to 0 == 0 to see Fail with somelogs.

答案3

得分: 0

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

每个测试文件导入porthos-go或porthos-go/mock时,测试文件都无法运行。删除这些导入语句解决了我的问题。

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

我仍然不知道原因,但是一旦我知道了,我会更新这个答案。

英文:

I found out that a import was causing this issue.

Every test file importing porthos-go or porthos-go/mock isn't running the test files. Removing those imports fixed my problem.

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

I still don't know why, but when I do I'll update this answear.

huangapple
  • 本文由 发表于 2021年9月25日 02:34:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/69319709.html
匿名

发表评论

匿名网友

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

确定