Guest User

Untitled

a guest
Apr 16th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  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.
Add Comment
Please, Sign In to add comment