Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use the testing package in go playground?

The testing package is available in the go playground.

How can I use the go playground to demonstrate testing concepts, without access to go test?

My assumption is it's possible using the testing.RunTests function. My attempts to do so always generate only "testing: warning: no tests to run".

Example: https://play.golang.org/p/PvRCMdeXhX

For context, my use case is for sharing quick examples of tests, examples and benchmarks with colleagues. I rely on go playground for sharing code snippets like this often.

like image 988
Ben Walther Avatar asked Sep 13 '17 20:09

Ben Walther


People also ask

Where do you put tests in go?

The convention for naming test files in Go is to end the file name with the _test.go suffix and place the file in the same directory as the code it tests. In the example above, the Multiply function is in integers.go , so its tests are placed in integers_test.go .

Which command is used to run the tests in go?

Running subtests in Go The t. Run() command takes two arguments — the first matches against parameters passed to go test , and the second is the name of a test function.

Do go tests run concurrently?

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').


2 Answers

You need to use testing.Main:

func main() {
    testSuite := []testing.InternalTest{
        {
            Name: "TestCaseA",
            F:    TestCaseA,
        },
    }
    testing.Main(matchString, testSuite, nil, nil)
}

https://play.golang.org/p/DQsIGKNWwd

If you want to use the "non deprecated", but unstable way:

https://play.golang.org/p/4zH7DiDcAP

like image 76
dave Avatar answered Oct 29 '22 16:10

dave


Dave's answer still works as a manual way of invoking tests, but if you read the "About" tests are now automatically supported:

If the program contains tests or examples and no main function, the service runs the tests. Benchmarks will likely not be supported since the program runs in a sandboxed environment with limited resources.

like image 33
Nelz Avatar answered Oct 29 '22 16:10

Nelz