Golang tests in sub-directory

BuildGo

Build Problem Overview


I want to create a package in Go with tests and examples for the package as subdirectories to keep the workspace cleaner. Is this possible and if so how?

All the documentation always puts the testing code in the same place as the other code, is this better in some way or just convention?

Build Solutions


Solution 1 - Build

Note that you can run go test "recursively": you need to list all the packages you want to test.

If you are in the root folder of your Go project, type:

go test ./...

The './...' notation is described in the section "Description of package lists" of the "command go":

> An import path is a pattern if it includes one or more "..." wildcards, each of which can match any string, including the empty string and strings containing slashes. > > Such a pattern expands to all package directories found in the GOPATH trees with names matching the patterns. > > As a special case, x/... matches x as well as x's subdirectories.
For example, net/... expands to net and packages in its subdirectories.


If you keep your _test.go files in a subfolder, the 'go test ./...' command will be able to pick them up.
But:

  • you will need to prefix your exported variables and functions (used in your tests) with the name of your package, in order for the test file to be able to access the package exported content.
  • you wouldn't access non-exported content.

That being said, I would still prefer keep the _test.go file right beside the main source file: it is easier to find.

Solution 2 - Build

EDITED

Built on VonC's answer,

This answer is valid in go1.11. No yet tested in upper go versions.

For those of you who like to keep their tests in a sub-folder, say test, then running

go test ./...

will attempt to run tests in every folder, even those that do not contain any test, thus having a ? in the subsequent report for non-test folders.

Running

go test ./.../test

instead will target only your test folders, thus having a clean report focused on your tests folders only.

CAUTION

Please be mindful that using test sub-folders will prevent coverage report computation. The phylosophy of go is to leave test files in the package folders.

Solution 3 - Build

Put your tests alongside your code in the same directory in a file called file_test.go where "file" is the name of the source code file you're testing. This is convention and I've found it to be best in my own experience.

If the go test tool isn't quite automated enough for you, you might look into GoConvey, which has a web UI that will automatically update and run traditional Go tests as well as GoConvey tests (which are based on behavior, and are more self-documenting than traditional Go tests).

Solution 4 - Build

I normally don't do test, but you can group your file into directories and use import like

import "./models" if is one level out
import "../models if is one level out and one level in

For example, for:
./models/todo.go
./test/todo_test.go

to test todo.go from todo_test.go, your import in the todo_test.go will be

import "../models"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionThe Graceful PenguinView Question on Stackoverflow
Solution 1 - BuildVonCView Answer on Stackoverflow
Solution 2 - Buildavi.elkharratView Answer on Stackoverflow
Solution 3 - BuildMattView Answer on Stackoverflow
Solution 4 - BuildFelix ZillaView Answer on Stackoverflow