Is there a way to get the command line arguments in go "tests",
When you call go test
obviously your main
is not run, so is there a way to process command line arguments,
One way would be to use the flags
packages and check for the command line arguments in each test or function being tested, but that is not ideal for that you need to do this in lots and lots of places, unlike the way you to it just in main
when you run the application.
One may think it is a wrong thing to do, and that it is against purity of unit-tests:
- not all tests are unit tests
- it is very functional not to rely on "ENV" variables and actually pass the stuff as arguments in command line,
For the record I ended up putting an init()
function in one of my _test
files, and set the variable that is set through flags when the main is called this way.