如何通过“测试"来测试Golang中的主程序包.包裹? [英] How to test the main package in Golang from a "test" package?

查看:52
本文介绍了如何通过“测试"来测试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 包中创建一个 main.go ,作为编译器的入口点.

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#Organization :

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 (程序包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天全站免登陆