概述

  • 表格测试
  • go test
  • go test -coverprofile=c.out 代码覆盖率
  • go tool cover -html=c.out
  • BenchmarkSubstr(b *testing.B) 做性能测试
  • go doc Queue 查看说明
    • godoc -http :6060

代码

  1. package main
  2. import "testing"
  3. func TestTriangle(t *testing.T) {
  4. tests := []struct{ a, b, c int }{
  5. {3, 4, 5},
  6. {5, 12, 13},
  7. {8, 15, 17},
  8. {12, 35, 37},
  9. {30000, 40000, 50000},
  10. }
  11. for _, tt := range tests {
  12. if actual := calcTriangle(tt.a, tt.b); actual != tt.c {
  13. t.Errorf("calcTriangle(%d, %d); "+
  14. "got %d; expected %d",
  15. tt.a, tt.b, actual, tt.c)
  16. }
  17. }
  18. }
  1. package main
  2. import "testing"
  3. func TestSubstr(t *testing.T) {
  4. tests := []struct {
  5. s string
  6. ans int
  7. }{
  8. // Normal cases
  9. {"abcabcbb", 3},
  10. {"pwwkew", 3},
  11. // Edge cases
  12. {"", 0},
  13. {"b", 1},
  14. {"bbbbbbbbb", 1},
  15. {"abcabcabcd", 4},
  16. // Chinese support
  17. {"一二三二一", 3},
  18. }
  19. for _, tt := range tests {
  20. actual := lengthOfNonRepeatingSubStr(tt.s)
  21. if actual != tt.ans {
  22. t.Errorf("got %d for input %s; "+
  23. "expected %d",
  24. actual, tt.s, tt.ans)
  25. }
  26. }
  27. }
  28. func BenchmarkSubstr(b *testing.B) {
  29. s := "abcabcbb"
  30. for i := 0; i < 13; i++ {
  31. s = s + s
  32. }
  33. b.Logf("len(s) = %d", len(s))
  34. ans := 3
  35. b.ResetTimer()
  36. for i := 0; i < b.N; i++ {
  37. actual := lengthOfNonRepeatingSubStr(s)
  38. if actual != ans {
  39. b.Errorf("got %d for input %s; "+
  40. "expected %d",
  41. actual, s, ans)
  42. }
  43. }
  44. }

web

  1. package main
  2. import (
  3. "errors"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. "net/http/httptest"
  8. "os"
  9. "strings"
  10. "testing"
  11. )
  12. func errPanic(_ http.ResponseWriter,
  13. _ *http.Request) error {
  14. panic(123)
  15. }
  16. type testingUserError string
  17. func (e testingUserError) Error() string {
  18. return e.Message()
  19. }
  20. func (e testingUserError) Message() string {
  21. return string(e)
  22. }
  23. func errUserError(_ http.ResponseWriter,
  24. _ *http.Request) error {
  25. return testingUserError("user error")
  26. }
  27. func errNotFound(_ http.ResponseWriter,
  28. _ *http.Request) error {
  29. return os.ErrNotExist
  30. }
  31. func errNoPermission(_ http.ResponseWriter,
  32. _ *http.Request) error {
  33. return os.ErrPermission
  34. }
  35. func errUnknown(_ http.ResponseWriter,
  36. _ *http.Request) error {
  37. return errors.New("unknown error")
  38. }
  39. func noError(writer http.ResponseWriter,
  40. _ *http.Request) error {
  41. fmt.Fprintln(writer, "no error")
  42. return nil
  43. }
  44. var tests = []struct {
  45. h appHandler
  46. code int
  47. message string
  48. }{
  49. {errPanic, 500, "Internal Server Error"},
  50. {errUserError, 400, "user error"},
  51. {errNotFound, 404, "Not Found"},
  52. {errNoPermission, 403, "Forbidden"},
  53. {errUnknown, 500, "Internal Server Error"},
  54. {noError, 200, "no error"},
  55. }
  56. func TestErrWrapper(t *testing.T) {
  57. for _, tt := range tests {
  58. f := errWrapper(tt.h)
  59. response := httptest.NewRecorder()
  60. request := httptest.NewRequest(
  61. http.MethodGet,
  62. "http://www.ctoedu.com", nil)
  63. f(response, request)
  64. verifyResponse(response.Result(),
  65. tt.code, tt.message, t)
  66. }
  67. }
  68. func TestErrWrapperInServer(t *testing.T) {
  69. for _, tt := range tests {
  70. f := errWrapper(tt.h)
  71. server := httptest.NewServer(
  72. http.HandlerFunc(f))
  73. resp, _ := http.Get(server.URL)
  74. verifyResponse(
  75. resp, tt.code, tt.message, t)
  76. }
  77. }
  78. func verifyResponse(resp *http.Response,
  79. expectedCode int, expectedMsg string,
  80. t *testing.T) {
  81. b, _ := ioutil.ReadAll(resp.Body)
  82. body := strings.Trim(string(b), "\n")
  83. if resp.StatusCode != expectedCode ||
  84. body != expectedMsg {
  85. t.Errorf("expect (%d, %s); "+
  86. "got (%d, %s)",
  87. expectedCode, expectedMsg,
  88. resp.StatusCode, body)
  89. }
  90. }

示例代码

  1. package queue
  2. import "fmt"
  3. func ExampleQueue_Pop() {
  4. q := Queue{1}
  5. q.Push(2)
  6. q.Push(3)
  7. fmt.Println(q.Pop())
  8. fmt.Println(q.Pop())
  9. fmt.Println(q.IsEmpty())
  10. fmt.Println(q.Pop())
  11. fmt.Println(q.IsEmpty())
  12. // Output:
  13. // 1
  14. // 2
  15. // false
  16. // 3
  17. // true
  18. }

qrcode_for_gh_e95b474fcf08_344.jpg