10

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.

Ben Walther
  • 1,605
  • 10
  • 18
  • See also [Go issue #6511](https://github.com/golang/go/issues/6511) and especially [this snippet](http://play.golang.org/p/K0gFil6jFC) it refers to. – kostix Sep 14 '17 at 08:24

3 Answers3

13

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

dave
  • 62,300
  • 5
  • 72
  • 93
  • 1
    Per the documentation, `testing.Main` is deprecated in favor of `testing.MainStart`. – Adrian Sep 13 '17 at 20:14
  • https://golang.org/pkg/testing/#Main - "Systems simulating "go test" should be updated to use MainStart." – Adrian Sep 13 '17 at 20:16
  • They're both equally unstable (they're both intended for internal use only). So calling one "non-deprecated but unstable" is misleading. – Adrian Sep 13 '17 at 20:51
  • Except `Main` is "preserved, as much as possible", per the documentation, whereas `MainStart` is "not subject to the Go 1 compatibility document". So, yeah, it certainly seems like one is more stable than the other. – dave Sep 13 '17 at 20:56
  • They're both internal functions, so the only way it could be construed as "more stable" would be that it's no longer being updated *because it's deprecated*. It's still an internal API not subject to the Go 1 compatibility promise. – Adrian Sep 13 '17 at 21:00
  • Updated following changes to testDeps https://play.golang.org/p/y5gMkY5xV_D – Stuart Eagles Jun 11 '21 at 10:47
4

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.

Nelz
  • 103
  • 4
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.

Adrian
  • 42,911
  • 6
  • 107
  • 99
  • 1
    Not from me, but this doesn't have much more than commentary. Plus dave does have 2 working examples, with both `testing.Main` and `testing.MainStart` (not that I'm recommending trying to simulate `go test` at all ;) ) – JimB Sep 13 '17 at 20:48
  • 1
    The question doesn't have any code either and it's +2/-0, and Dave had the wrong (clearly indicated in the documentation) answer until I pointed it out. Oh well, the StackOverflow gods are fickle and inscrutable. – Adrian Sep 13 '17 at 20:52