Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run golang tests sequentially?

Tags:

When I run go test, my output:

--- FAIL: TestGETSearchSuccess (0.00s)         Location:       drivers_api_test.go:283         Error:          Not equal: 200 (expected)                                 != 204 (actual)  --- FAIL: TestGETCOSearchSuccess (0.00s)         Location:       drivers_api_test.go:391         Error:          Not equal: 200 (expected)                                 != 204 (actual) 

But after I run go test again, all my tests pass.

Tests fail only when I reset my mysql database, and then run go test for the first time.

For every GET request, I do a POST request before to ensure that there is data created in the DB.

Could anyone help me with how to make sure that tests are run sequentially? That is the POST requests are run before the GET requests?

like image 992
Varun Patro Avatar asked Jul 03 '15 08:07

Varun Patro


People also ask

Do go tests run concurrently?

Yes, tests are executed as goroutines and, thus, executed concurrently.

Does go run unit tests in parallel?

The 'go test' command may run tests for different packages in parallel as well, according to the setting of the -p flag (see 'go help build').

How are go tests run?

At the command line in the greetings directory, run the go test command to execute the test. The go test command executes test functions (whose names begin with Test ) in test files (whose names end with _test.go). You can add the -v flag to get verbose output that lists all of the tests and their results.

Which command is used to run the tests in go?

After the package test finishes, go test prints a summary line showing the test status ('ok' or 'FAIL'), the package name, and elapsed time. To run your tests in this mode, run go test in your project's root directory. In the package list mode, go test compiles and tests each package listed as arguments to the command.


1 Answers

You can't / shouldn't rely on test execution order. The order in which tests are executed is not defined, and with the use of testing flags it is possible to exclude tests from running, so you have no guarantee that they will run at all.

For example the following command will only run tests whose name contains a 'W' letter:

go test -run W 

Also note that if some test functions mark themselves eligible for parallel execution using the T.Parallel() method, the go tool will reorder the tests to first run non-parallel tests, and then run parallel tests in parallel under certain circumstances (controlled by test flags like -p). You can see examples of this in this answer: Are tests executed in parallel in Go or one by one?

Tests should be independent from each other. If a test function has prerequisites, that cannot be done/implemented in another test function.

Options to do additional tasks before a test function is run:

  • You may put it in the test function itself
  • You may put it in a package init() function, in the _test.go file itself. This will run once before execution of test functions begins.
  • You may choose to implement a TestMain() function which will be called first and in which you may do additional setup before you call M.Run() to trigger the execution of test functions.
  • You may mix the above options.

In your case in package init() or TestMain() you should check if your DB is initialized (there are test records inserted), and if not, insert the test records.

Note that starting with Go 1.7, you may use subtests in which you define execution order of subtests. For details see blog post: Using Subtests and Sub-benchmarks, and the package doc of the testing package.

like image 90
icza Avatar answered Sep 19 '22 15:09

icza