lundi 26 octobre 2015

How to run Go examples, which don't have output comments?

Testable Go examples look awesome.

func ExampleReverse() {
    fmt.Println(stringutil.Reverse("hello"))
    // Output: olleh
}

The above, for example, is equivalent to a unit test that asserts:

stringutil.Reverse("hello") == "olleh"

According to the golang blog, we can write examples that don't have an output comment:

If we remove the output comment entirely then the example function is compiled but not executed. Examples without output comments are useful for demonstrating code that cannot run as unit tests, such as that which accesses the network, while guaranteeing the example at least compiles.

The output of such examples, although not testable, could still be useful for the user to produce and read. And the examples themselves - useful to run on their computer.

So is there a way or a tool that can run example functions in *_test.go files from the terminal?

Aucun commentaire:

Enregistrer un commentaire