4-1测试.mp4

传统测试vs表格驱动测试

传统测试

  • 测试数据和测试逻辑混在一起
  • 出错信息不明确
  • —旦一个数据出错测试全部结束

    表格驱动测试

    1635243684088

  • 分离的测试数据和测试逻辑

  • 明确的出错信息
  • 可以部分失败
  • go语言的语法使得我们更易实践表格驱动测试 ``` package main

import “testing”

func TestTriangle(t *testing.T){ tests := []struct{a,b,c int }{ {3,4,5}, {5,12,13}, {8,15,17}, {12,35,37}, {30000,40000,52000}, }

  1. for _,tt:=range tests{
  2. if actual:= calcTriangle(tt.a,tt.b); actual !=tt.c{
  3. t.Errorf("calcTriang %d %d got %d ; exceped",tt.a,tt.b,tt.c)
  4. }
  5. }

}

  1. ```
  2. === RUN TestTriangle
  3. add_test.go:16: calcTriang 30000 40000 got 52000 ; exceped
  4. --- FAIL: TestTriangle (0.00s)
  5. FAIL
  6. Process finished with the exit code 1

4-2代码覆盖率和性能测试.mp4

  1. go test -coverprofile=c.out
  2. go test -coverprofile=c.out .
  3. go tool pprof cpu.out
  4. go tool pprof -cpuprofile cpu.out

4-3使用pprof进行性能调优.mp4

1635249436838

4-4测试http服务器(上).mp4

1635253212575

4-5测试http服务器(下).mp4

  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(writer http.ResponseWriter, request *http.Request) error {
  13. panic(123)
  14. }
  15. type testingUserError string
  16. func (e testingUserError) Error() string {
  17. return e.Message()
  18. }
  19. func (e testingUserError) Message() string {
  20. return string(e)
  21. }
  22. func errUserError(writer http.ResponseWriter, request *http.Request) error {
  23. return testingUserError("user error")
  24. }
  25. func errNotFound(writer http.ResponseWriter, request *http.Request) error {
  26. return os.ErrNotExist
  27. }
  28. func errNoPermission(writer http.ResponseWriter, request *http.Request) error {
  29. return os.ErrPermission
  30. }
  31. func errUnknown(writer http.ResponseWriter, request *http.Request) error {
  32. return errors.New("unknown error")
  33. }
  34. func noError(writer http.ResponseWriter, request *http.Request) error {
  35. fmt.Fprintln(writer, "no error")
  36. return nil
  37. }
  38. func TestErrWrapper(t *testing.T) {
  39. tests := []struct {
  40. h appHandler
  41. code int
  42. message string
  43. }{
  44. {errPanic, 500, "Internal Server Error"},
  45. {errUserError, 400, "user error"},
  46. {errNotFound, 404, "Not Found"},
  47. {errNoPermission, 403, "Forbidden"},
  48. {errUnknown, 500, "Internal Server Error"},
  49. {noError, 200, "no error"},
  50. }
  51. for _, tt := range tests {
  52. f := errWrapper(tt.h)
  53. response := httptest.NewRecorder()
  54. request := httptest.NewRequest(
  55. http.MethodGet,
  56. "http://localhost:8888/list/fib2.txt",
  57. nil,
  58. )
  59. f(response, request)
  60. b, _ := ioutil.ReadAll(response.Body)
  61. body := strings.Trim(string(b), "\n")
  62. if response.Code != tt.code ||
  63. body != tt.message {
  64. t.Errorf("expect (%d, %s)"+
  65. "expect (%d, %s);\"",
  66. tt.code,
  67. tt.message,
  68. response.Code,
  69. body,
  70. )
  71. }
  72. }
  73. }
  74. /**
  75. === RUN TestErrWrapper
  76. time="2021-10-26T20:55:25+08:00" level=info msg="Painic: 123"
  77. --- PASS: TestErrWrapper (0.02s)
  78. PASS
  79. Process finished with the exit code 0
  80. API server listening at: 127.0.0.1:50219
  81. === RUN TestErrWrapper
  82. --- PASS: TestErrWrapper (0.00s)
  83. PASS
  84. Debugger finished with the exit code 0
  85. API server listening at: 127.0.0.1:50243
  86. === RUN TestErrWrapper
  87. --- PASS: TestErrWrapper (0.00s)
  88. PASS
  89. Debugger finished with the exit code 0
  90. */

函数是一等公民

  1. func TestErrWrapperInServer(t *testing.T) {
  2. for _, tt := range tests {
  3. f := errWrapper(tt.h)
  4. server := httptest.NewServer(
  5. http.HandlerFunc(f))
  6. resp, _ := http.Get(server.URL)
  7. b, _ := ioutil.ReadAll(resp.Body)
  8. body := strings.Trim(string(b), "\n")
  9. if resp.StatusCode != tt.code ||
  10. body != tt.message {
  11. t.Errorf("expect (%d, %s)"+
  12. "expect (%d, %s);\"",
  13. tt.code,
  14. tt.message,
  15. resp.StatusCode,
  16. body,
  17. )
  18. }
  19. }
  20. }
  21. /**
  22. === RUN TestErrWrapperInServer
  23. --- PASS: TestErrWrapperInServer (0.00s)
  24. PASS
  25. Process finished with the exit code 0
  26. */
  • 通过testing库里面的 http假的request
  • 这种更像单元测试
  • 通过启动http服务来进行测试
  • 这种更加的全面

4-6生成文档和示例代码.mp4

  1. godoc -http :6060
  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. }