Search code examples
testinggogo-playground

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.


Solution

  • 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