Forem

Cover image for Go Test supports shuffling tests
Georgios Kampitakis
Georgios Kampitakis

Posted on • Edited on

3

Go Test supports shuffling tests

TLDR: Randomize the execution order of tests and benchmarks with -shuffle off,on,N flag.


As a general guideline, it's bad for unit tests to depend on each other and the order of them messing with the results.

A "trick" I have seen some codebases adopt to mitigate and catch issues with unit test results relying on order is instead of using arrays in table-driven tests using maps.


TestMyFunc(t *testing.T) {
    for name, _ := range map[string]struct{}{} {
        t.Run(name, func(t *testing.T) {
                ...
        })
    }
}

Enter fullscreen mode Exit fullscreen mode

The reason behind this is, the order of map iteration is not guaranteed, so on different runs the tests might run in different order making it more difficult to rely on the order.

But as of Go 1.17, you can "Randomize the execution order of tests and benchmarks."

As go help testflag states

-shuffle off,on,N
        Randomize the execution order of tests and benchmarks.
        It is off by default. If -shuffle is set to on, then it will seed
        the randomizer using the system clock. If -shuffle is set to an
        integer N, then N will be used as the seed value. In both cases,
        the seed will be reported for reproducibility.
Enter fullscreen mode Exit fullscreen mode

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay