Go provides the go test command for running the unit tests in a project.
Running this command will ordinarily run the whole test suite, but you can limit
it to only a specific file or test.
Let’s assume you have the following main.go file:
And tests for the main package defined in a main_test.go file as follows:
If you run go test, it will run both the TestFactorial and TestSquare
functions.
$ go test -v
=== RUN TestSquare
--- PASS: TestSquare (0.00s)=== RUN TestFactorial
--- PASS: TestFactorial (0.00s)PASS
ok github.com/ayoisaiah/example 0.002s
To run a specific one, pass the name of the test to the -run flag as shown
below:
$ go test -v -run TestSquare=== RUN TestSquare
--- PASS: TestSquare (0.00s)PASS
ok github.com/ayoisaiah/example 0.002s
Be sure to check the
documentation for more
information.