7

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.

4

3 回答 3

11

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

于 2017-09-13T20:13:47.910 回答
1

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.

于 2021-06-23T22:03:32.980 回答
0

There are execution time limits on playground and I believe they are less than the minimum execution time used by bench. From reviewing the source of the go test command, it looks like the current proper entry point for tests is testing.MainStart which you may or may not have luck using. It is technically an internal-use API and not subject to compatibility guidelines, so YMMV.

于 2017-09-13T20:14:51.787 回答