daily pastebin goal
61%
SHARE
TWEET

Untitled

a guest Apr 16th, 2018 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. What does it mean that the ExampleReverse function "passes"?
  2.  
  3. 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.
  4.  
  5. To see a failing example we can change the output comment text to something obviously incorrect
  6.  
  7. ```
  8. func ExampleReverse() {
  9.     fmt.Println(stringutil.Reverse("hello"))
  10.     // Output: golly
  11. }
  12. ```
  13.  
  14. and run the tests again:
  15.  
  16. ```
  17. $ go test
  18. --- FAIL: ExampleReverse (0.00s)
  19. got:
  20. olleh
  21. want:
  22. golly
  23. FAIL
  24. ```
  25.  
  26. If we remove the output comment entirely
  27.  
  28. ```
  29. func ExampleReverse() {
  30.     fmt.Println(stringutil.Reverse("hello"))
  31. }
  32. ```
  33.  
  34. then the example function is compiled but not executed:
  35.  
  36. ```
  37. $ go test -v
  38. === RUN TestReverse
  39. --- PASS: TestReverse (0.00s)
  40. PASS
  41. ok      github.com/golang/example/stringutil    0.009s
  42. ```
  43.  
  44. 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.
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top