github.com/go-swagger/go-swagger@v0.31.0/docs/faq/faq_testing.md (about)

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