L
L
last72016-06-16 21:52:26
go
last7, 2016-06-16 21:52:26

How to write unit tests for a large go application?

Let's say there is some application similar in structure to this . That is, there are several nested packages, including controllers, and it needs to be tested.
We create *_test.go in the package. But the problem is that when the application is initialized: a map of templates is created, all certificates are connected from files, and so on. The paths in them are relative, so if the test is in the root - everything is OK, but if the test is in a package, due to the specifics of running tests in Go, the root of the test run = the path of the package, they are not loaded. Found that I'm not the only one . But it still seems to me not canonical and not beautiful.
For a long time I climbed the gihab, but did not find a large web application at least 50% covered with tests.
From here the question arose: how does one test nested packages, if they have a file-opening dependency? And generally large applications.
Thanks

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
sim3x, 2016-06-16
@last7

By definition, unit tests test a piece of themselves and don't look at the application as a whole, or even at the module. They should mock all external requests and slip predefined answers
. And it’s quite possible to put a functional test in the root of the project

E
Evgeniy Ivakha, 2016-06-16
@ivahaev

There are several approaches.
One of them is to test only the public methods of the packages, and keep the test files in the root of the project.
The second, and, it seems to me, more correct is to reduce the cohesion of packages. Make methods simpler and more abstract so that they are easier to test.

X
xabesaviv, 2016-06-20
@xabesaviv

https://speakerdeck.com/mitchellh/advanced-testing...
Discussion in Russian - here
devzen.ru/episode-0095
Including on the question you are interested in.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question