We are currently working with a revel project, and the number of tests are already getting out of hand. I'm wanting to create a few packages under the tests
directory to separate out controller tests from model tests and so on (e.g. tests/controllers/
, tests/models/
, etc).
When I do this the revel test command stops seeing these files.
I found a recommendation online to trail my test command with ...
, but when I do this I get an error saying it failed to import my project.
Is this possible to do, or do all test files have to be within the tests
directory? If it's possible, how do you run the tests in the sub-packages?
You can move the tests themselves into an importable package (i.e. not in a _test.go
file), and then use them twice. The _test.go
files in the subdirectories can be something like:
package subdir_test
import "github.com/blah/project/parent/subdir/tests"
func TestPackage(t *testing.T) {
tests.Run(t)
}
Using TB.Run
you can make things play nice with go test
command line arguments:
package tests // subdir
import "testing"
func Run(t *testing.T) {
for _, test := range testCases { // or whatever
test.Run(t)
}
}
And of course parent directories can just call their child directories' Run
methods:
package parent_test
import (
"testing"
p "github.com/blah/project/parent/tests"
s "github.com/blah/project/parent/subdir/tests"
)
func TestPackage(t *testing.T) {
p.Run(t)
}
func TestSubPackages(t *testing.T) {
s.Run(t)
}
I would recommend avoiding variants of go list ./...
etc as lots of the tooling is not really prepared to handled that gracefully (for example running that with coverage does not produce a single combined report, complicating things for large projects, and it also lists vendored code which might not always make sense)