How to test the main package in Golang from a “test” package?

南楼画角 提交于 2021-02-07 10:26:36

问题


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.

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.

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.

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

Thanks.

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.


回答1:


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.

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

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

This is how your code structured:

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

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.

This is how you can organize your code:

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

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.

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.



来源:https://stackoverflow.com/questions/53997114/how-to-test-the-main-package-in-golang-from-a-test-package

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!