无法在除了 main 之外的包中调用测试。

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

Cannot call tests inside packages other than main

问题

你好!你的问题是关于在Go代码中编写测试的问题,以及如何在其他包中调用测试。以下是翻译好的内容:

我正在为我的Go代码编写测试,在sender文件夹的sender包中,我添加了exposed_api_test.go(也尝试了exposed_api_test.go,因为我在exposed_api.go中有代码)。

package sender

import (
	"log"
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestTimeConsuming(t *testing.T) {
 
   assert.Equal(t, "test", "test1")

}

当我运行构建和运行命令go test my_project时,我得到了? my_project [no test files]的错误提示。当我将测试放在这个包之外(在main包中,相同的测试但是package main),并执行相同的命令时,我可以执行main包中的测试。

问题是什么,如何调用除main包之外的其他包中的测试?

英文:

I am writing tests for my go code and inside sender folder, sender package I have added exposed_api_test.go ( also tried exposed_api_test.go because I have code in exposed_api.go)

package sender

import (
	"log"
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestTimeConsuming(t *testing.T) {
 
   assert.Equal(t, "test", "test1")

}

and when I run build and run command go test my_project I get ? my_project [no test files]
When I put test out of this package ( in main package same test but package main ) and execute same command I get that test from main executed.
What is a problem and how to call tests inside other packages other than main ?

答案1

得分: 1

试试这个:

go test my_project/...

或者如果你在项目内部:

go test ./...
英文:

Try this:

go test my_project/...

Or if you are within your project:

go test ./...

huangapple
  • 本文由 发表于 2015年6月17日 17:27:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/30887528.html
匿名

发表评论

匿名网友

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

确定