DEV Community

John Doe
John Doe

Posted on

Run tests with -race flag in GoLand ๐ŸŽ

To understand -race flag better, we can refer to the Go documentation:

Data races are among the most common and hardest to debug types of bugs in concurrent systems. A data race occurs when two goroutines access the same variable concurrently and at least one of the accesses is a write.

It is easy to use via the command line interface. So, how to use -race flag in GoLand with tests? Is it possible to use -race always in tests? Here you go.

Add -race flag to a specific test

  • Navigate to a test file, click on the gutter icon near the test name and select Modify Run Configuration. Gutter menu with the Modify Run Configuration entry
  • Add -race entry to the Go tool arguments field. Example of adding race flag
  • Save changes and run the configuration again.

Add -race flag to all tests

  • Navigate to Help | Find Action in the main menu (Shift+Shift by shortcuts) and type Edit Configurations, press Enter.
  • Select the Edit configuration templates option, find Go Test. Option is left-bottom position
  • Add -race entry to the Go tool arguments field. Pay attention that changing a template does not affect the existing configuration and you should add the flag manually to the same field.

Congratulations! ๐ŸŽ‰ Now your tests use -race flag and you can see warnings from the compiler. Example of warning from the compiler

Top comments (2)

Collapse
 
biros profile image
Boris Jamot โœŠ /

Please note that the race detector only finds races that happen at runtime, so it can't find races in code paths that are not executed. If your tests have incomplete coverage, you may find more races by running a binary built with -race under a realistic workload.

golang.org/doc/articles/race_detec...

Collapse
 
s0xzwasd profile image
John Doe

Good point, thanks for sharing :)