github.com/rzurga/go-swagger@v0.28.1-0.20211109195225-5d1f453ffa3a/docs/faq/faq_testing.md (about) 1 <!-- Questions about testing --> 2 ## API testing 3 4 ### Any suggestions how to leverage the generated client for benchmarking the API? 5 _Use-Case_: I want to benchmark the throughput and latency of different API calls. 6 7 >I can think of using the go testing/benchmarking framework and hand role the API calls, or I can leverage the generated client and augment it with a benchmarking framework. 8 9 *What have others done?* 10 11 At the moment, the toolkit does not generate testing tools. You may be interested in this contribution: <https://github.com/go-openapi/stubs> 12 (generates random JSON to fill in testcases). 13 14 We acknowledge that API testing is an important use-case. However, it is not yet supported. Pull requests to move on forward in that direction are welcome. 15 16 Originally from issue [#787](https://github.com/go-swagger/go-swagger/issues/787). 17 18 ### Using httptest 19 _Use-Case_: I would like to use httptest for testing my handlers. 20 Go-swagger provides a Server, but not a configured handler. 21 22 **Hint**: I use this hack : in a file `test.go` in the restapi folder, I steal the private `configureAPI` function. It works. 23 24 ```golang 25 package restapi 26 27 import ( 28 loads "github.com/go-openapi/loads" 29 "github.com/pim/pam/poum/restapi/operations" 30 "net/http" 31 ) 32 33 func getAPI() (*operations.ThefactoryAPI, error) { 34 swaggerSpec, err := loads.Analyzed(SwaggerJSON, "") 35 if err != nil { 36 return nil, err 37 } 38 api := operations.NewThefactoryAPI(swaggerSpec) 39 return api, nil 40 } 41 42 func GetAPIHandler() (http.Handler, error) { 43 api, err := getAPI() 44 if err != nil { 45 return nil, err 46 } 47 h := configureAPI(api) 48 err = api.Validate() 49 if err != nil { 50 return nil, err 51 } 52 return h, nil 53 } 54 ``` 55 56 And I can use this in tests like this: 57 ```golang 58 handler, err := restapi.GetAPIHandler() 59 if err != nil { 60 t.Fatal("get api handler", err) 61 } 62 ts := httptest.NewServer(handler) 63 defer ts.Close() 64 res, err := http.Get(ts.URL + "/api/v1/boxes") 65 ``` 66 67 But, hacking restapi, which use my handlers is cyclic, I can't drop my test near my handlers, and this is still a hack. 68 69 *What is the official way to manage handler testing?* 70 71 **Hint**: you don't actually need httptest to test the handlers. 72 A handler is essentially a function of parameters to result. 73 The result knows how to write itself to a http.ResponseWriter, and you already know that that part works. 74 So to test a handler what you require is to test just your code. 75 76 So to test the AddOne operation from the todo list this, there are 2 functions involved in the implementation. 77 78 The first function uses the data from the request to actually write the todo item to a store, this can be tested separately. 79 ```golang 80 func addItem(item *models.Item) error { 81 if item == nil { 82 return errors.New(500, "item must be present") 83 } 84 85 itemsLock.Lock() 86 defer itemsLock.Unlock() 87 88 newID := newItemID() 89 item.ID = newID 90 items[newID] = item 91 92 return nil 93 } 94 ``` 95 96 Then there is the actual handler: 97 98 ```golang 99 todos.AddOneHandlerFunc(func(params todos.AddOneParams) middleware.Responder { 100 if err := addItem(params.Body); err != nil { 101 return todos.NewAddOneDefault(500).WithPayload(&models.Error{Code: 500, Message: swag.String(err.Error())}) 102 } 103 return todos.NewAddOneCreated().WithPayload(params.Body) 104 }) 105 ``` 106 To test this second function we don't need to use the httptest package, you can assume that that part of the code works. So all you have to test is whether or not you get the right return types for a given set of parameters. 107 108 Originally from issue [#719](https://github.com/go-swagger/go-swagger/issues/719). 109 110 ------------------- 111 112 Back to [all contributions](README.md#all-contributed-questions)