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.

1

3 Answers 3

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

6
  • 1
    Per the documentation, testing.Main is deprecated in favor of testing.MainStart.
    – Adrian
    Sep 13, 2017 at 20:14
  • golang.org/pkg/testing/#Main - "Systems simulating "go test" should be updated to use MainStart."
    – Adrian
    Sep 13, 2017 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, 2017 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, 2017 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, 2017 at 21:00
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.

1
  • 1
    Use the dropdown menu to the right of the Share button to load an example test. Jun 23, 2021 at 23:45
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.

2
  • 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, 2017 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, 2017 at 20:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.