Life In The Stack Trace...

TODO: <Insert clever marketing phrase here>

What Order Do Golang Tests Run?

So, I've been moving forward with Golang lately and really like it a lot. Today, I was wondering what order tests are run and decided to find out a little. Basically, I created a directory containing a few test files.

- a_test.go
- b_test.go
- c_test.go

Download the support file and run the tests with the following command:

go test -v

You should see output similar to the following:

=== RUN TestA01
--- PASS: TestA01 (0.00s)
=== RUN TestA02
--- PASS: TestA02 (0.00s)
=== RUN TestA03
--- PASS: TestA03 (0.00s)
=== RUN TestA04
--- PASS: TestA04 (0.00s)
=== RUN TestB02
--- PASS: TestB02 (0.00s)
=== RUN TestB01
--- PASS: TestB01 (0.00s)
=== RUN TestB04
--- PASS: TestB04 (0.00s)
=== RUN TestB03
--- PASS: TestB03 (0.00s)
=== RUN TestC04
--- PASS: TestC04 (0.00s)
=== RUN TestC03
--- PASS: TestC03 (0.00s)
=== RUN TestC02
--- PASS: TestC02 (0.00s)
=== RUN TestC01
--- PASS: TestC01 (0.00s)

I have not taken the time to actual read the source code of the testing package, however, this quick test shows something half way interesting. It looks to me that the tests are accumulated via a simple directory listing and run in that order. Therefore, they are run in alphabetical order. Also, notice the order of the TestXXX method names. The output of the test matches the order of the methods within each file.

So, it seems to me that the tests are run in the order in which they appear within a directory coupled with the order in which the methods appear within each file.

In a pinch, if you absolutely needed to have a test run before or after any of the other tests, then, you could create a file containing those tests that shows up in the directory listing before or after any of the other test files and it would probably work. However, the testing package is subject to change and certainly will change over time, so, I wouldn't recommend this approach for any long term solution.