DEV Community


Go Notes: Example Tests

uris77 profile image Roberto Guerra ・2 min read

Go's Example Tests are a great tool for documenting the expectations of our code. They are conveniently integrated with godocs, making the docs have more context and actual runnable examples. These tests provide a quick overview of what the code is supposed to be doing in a manner that is easily digestible for the reader without needing to understand the inner workings.

What are Example Tests?

In Example Tests, we print out the result of a function that we want to test, and we indicate the expected output as comments:

func ExampleTest() {
    fmt.Println("Hello World!")
    // Output:
    // Hello World!

If we can't determine the order of the output of a test, we can use Unordered output instead of Output:

func ExampleUnorderedTest() {
    unordered := afunc()

    // Unordered output:
    // d
    // c
    // a
    // b


Naming Conventions

The Go community seems to have settled on two naming conventions for the example file names. Both conventions can be found in the standard library. The first one follows the pattern xxx_example_test.go. This can bee seen in the example tests for stringer:

The second pattern is example_xxx_test.go. This pattern is prevalent in the sort package of the standard library:

# Summary
Example tests are a useful tool for documenting our code. They are kept relevant because they are run as tests and are embedded in the "godocs" for easy consumption.

Discussion (0)

Editor guide