Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- What does it mean that the ExampleReverse function "passes"?
- As it executes the example, the testing framework captures data written to standard output and then compares the output against the example's "Output:" comment. The test passes if the test's output matches its output comment.
- To see a failing example we can change the output comment text to something obviously incorrect
- ```
- func ExampleReverse() {
- fmt.Println(stringutil.Reverse("hello"))
- // Output: golly
- }
- ```
- and run the tests again:
- ```
- $ go test
- --- FAIL: ExampleReverse (0.00s)
- got:
- olleh
- want:
- golly
- FAIL
- ```
- If we remove the output comment entirely
- ```
- func ExampleReverse() {
- fmt.Println(stringutil.Reverse("hello"))
- }
- ```
- then the example function is compiled but not executed:
- ```
- $ go test -v
- === RUN TestReverse
- --- PASS: TestReverse (0.00s)
- PASS
- ok github.com/golang/example/stringutil 0.009s
- ```
- 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.
Add Comment
Please, Sign In to add comment