I am using this method here to collect the code coverage for my system tests. The overall idea is that I need a dummy test so that I can generate an executable that calls the main()
in the package. As a result, I have a file like the following in each and every packages:
TestRunner.go:
//go:build testrunner
package main
import "testing"
import (
"os"
)
func TestRunMain(t *testing.T) {
main()
}
func TestMain(m *testing.M) {
// This is where I parse flags
m.Run()
}
As shown, it is a really simple file but I would need a duplicate of this for each package to generate the executables since they all have their own main()
. From the maintainability point of view, this does not seem to be the best practice as if I were to change the flag parsing in the test runner, I would have to make the same change across all packages.
One way I can think of is to keep a template. Then write a script in the Makefile so that every time I build the project, it copy-pastes the template to each package and after it generates the executables, the copies will get deleted. But this feels rather cumbersome.
Is there any other way I can keep only one copy of the file and somehow have it reference the main()
s from different packages?