英文:
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 ./...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论