如何从“测试"中测试Golang中的主包包裹? [英] How to test the main package in Golang from a "test" package?

查看:19
本文介绍了如何从“测试"中测试Golang中的主包包裹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用 Golang 编写的简单程序.这是一个 API.因此,在项目文件夹中,有一个名为 cmd 的文件夹,其中包含我的 main 包(用于初始化应用程序并定义 API 的端点).还有一个以我的程序命名的文件夹,其中包含以我的程序命名的包中的多个文件.这个包作为模型来执行所有必要的查询,并包含我定义的所有类型.

I have a simple program written in Golang. It's an API. So inside the project folder, there's a folder named cmd containing my main package (used to initialise the app and defines the endpoints for the API). There's also a folder named after my program, containing multiple files from a package also named after my program. This package serves as the model to do all the necessary queries and contains all the types I have defined.

我还创建了一个名为 test 的文件夹.它包含我在名为 test 的包下的所有测试文件.问题是要运行测试,我必须访问我的主包!有没有办法在 Golang 中做到这一点?我尝试简单地使用 import "cmd/main" 但当然它不起作用.

I also created a folder called test. It contains all my test files under the package named test. The problem is that to run the tests, I have to access my main package ! Is there a way to do that in Golang ? I tried simply using import "cmd/main" but of course it doesn't work.

我也有一个想法.也许我可以将所有初始化函数(在 cmd 文件夹中)移动到以我的程序命名的包中.这样我就可以在 test 中做一个常规的 import.我在 cmd 内部创建了一个 main.gomain 包中,作为编译器的入口点.

I also had an idea. Perhaps I could move all my initialising functions (in the cmd folder) to the package named after my program. This way I could do a regular import in test. And I create, inside of cmd, a main.go in the main package that serves as the entry point for the compiler.

我是 Go 的新手,所以我不是很自信.你认为这是正确的方法吗?

I'm new to Go so I'm not really confident. Do you think it's the right way ?

谢谢.

显然有些人认为这个问题是重复的,但事实并非如此.这是我在评论中给出的解释:

EDIT : Apparently some people think this question is a duplicate, but it's not. Here's the explanation I gave in on of the comments :

我在发帖前阅读了这篇文章,但它没有回答我的问题因为在那篇文章中,该人在主包中进行了测试.这我问我的问题的原因是因为我不想让我的在主包中进行测试.我宁愿把它们都放在一个测试文件夹中在同一个包内.

I read this post before posting, but it didn't answer my question because in that post the person has his tests in the main package. The reason why I asked my question is because I don't want to have my tests in the main package. I'd rather have them all in a test folder inside the same package.

推荐答案

你想做的在 GO 中是不可能的(假设你想测试私有函数).

What you want to do is not not possible in GO (assuming you want to test private functions).

因为我不想在主包中包含我的测试.我宁愿将它们全部放在同一个包内的测试文件夹中.

because I don't want to have my tests in the main package. I'd rather have them all in a test folder inside the same package.

如果你把它移动到不同的文件夹,你的代码属于不同的包.

Your code belongs to different package if you move it into different folder.

这就是 GO 定义包的方式 https://golang.org/doc/code.html#组织:

This is how GO defines packages https://golang.org/doc/code.html#Organization:

每个包由一个或多个 Go 源文件组成,单个目录.

Each package consists of one or more Go source files in a single directory.

您的代码结构如下:

main
| -- main.go (package main)
+ -- test
     | -- main_test.go (package test)

将测试与代码保存在同一文件夹中是惯用的做法.如果语言或框架设置了一些开发人员必须遵循的规则,这是正常的.GO 对此非常严格.

It is idiomatic to keep tests in the same folder with code. It is normal if language or framework set some rules that developer has to follow. GO is pretty strict about that.

这是组织代码的方式:

main
| -- main.go (package main)
| -- main_test.go (package main_test)
| -- main_private_test.go (package main)

通常根据其公共接口测试代码是有意义的.最好的方法是将测试放入不同的包中.GO 约定是将测试保存在同一文件夹中,这会导致使用相同的包名.该问题有一个解决方法.您可以为测试的包名称添加 _test (package main_test) 前缀.

Often it makes sense to test code against its pubic interfaces. The best way to do that that, is to put tests into different package. GO convention is to keep tests in the same folder what leads to using the same package name. There is a workaround for that issue. You can add _test (package main_test) prefix to package name for your tests.

如果无法使用公共接口测试您的代码,您可以添加另一个带有测试的文件并在该文件中使用 package main.

If, that is not possible to test your code using public interfaces, you can add another file with tests and use package main in that file.

这篇关于如何从“测试"中测试Golang中的主包包裹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆