# swagger-test-templates模块

## swagger-test-templates模块

[npm地址](https://www.npmjs.com/package/swagger-test-templates)

[github地址](https://github.com/apigee-127/swagger-test-templates)

> 从 [Swagger](http://swagger.io/) 规范生成测试代码（version 2.0）

### 用法

通过npm安装

```
npm install --save swagger-test-templates
```

使用您的[Swagger](http://swagger.io/) API规范生成API的测试文件。

```javascript
var stt = require('swagger-test-templates');
var swagger = require('/path/to/swagger.json');
var config = {
  assertionFormat: 'should',
  testModule: 'supertest',
  pathName: ['/user', '/user/{id}'],
  loadTest: [{pathName:'/user', operation:'get', load:{requests: 1000, concurrent: 100}}, { /* ... */ }],
  maxLen: 80,
  pathParams: {
    "id": "0123"
  }
};

// Generates an array of objects containing the test file content, following specified configuration
// the array contains objects with the scheme { name: <test-file-name>, test: <test-file-content> }
// tests = [ {name: base-path-test.js, test: ... }, {name: users-test.js, test: ... }]
var tests = stt.testGen(swagger, config);
```

## API

`swagger-test-templates`模块导出具有下列参数和返回值的函数：

**参数：**

* `assertionFormat 必选`: `should`,`expect`或`assert`其中之一。 选择应该在输出测试代码中使用哪种断言方法。
* `testModule 必选 ：supertest`或者`request`其中之一。通过`request`模块还是`supertest`模块进行api接口的调用。
* `pathName 必选 ：`在您的Swagger API规范中列出可用的路径名称，用于生成测试文件。空数组的话会生成所有路径的测试文件。
* `statusCodes 可选：`需要测试的状态数组。排除此参数将生成所有响应的测试。只有当状态代码列在 Swagger API 规范中时，才为状态代码生成测试代码。
* `loadTest：可选：` 针对Swagger API 规范中的对象信息列表来生成压力测试。如果指定，则pathName 和operation 是

  **必须的**。，请求次数是可选字段，默认值为1000，并发默认值为100。
* `maxLen 可选：`最大行长度。如果设置为-1，说明将不会被截断。默认值为80。
* `pathParams 可选：`对象，该对象包含特定路径参数的值。
* `templatesPath 可选：`生成测试文件的自定义模板路径字符串。注意：将所有模板复制到自定义目录中，这是一个“all-or-nothing”路径。
* `requestData 可选：`包含请求的数据的对象，请参见requestData章节获取更多细节。

**返回值：**

每个字符串都包含测试文件和文件名的数组。使用此信息将这些文件写入磁盘。

## 发送请求数据（requestData）

基于您的模式，有几个模块允许您生成模拟请求payloads。可以通过填充模块的`requestData`属性，将此模拟数据连同该模块生成的测试一起发送。模拟数据需要具有以下结构：

#### 模拟HTTP请求体

```javascript
{
   '/endpoint': {
       operation: {
           'responseCode': [{ body: {}, description:'some description of the data'}]
       }
   }
 }
```

#### 模拟路径参数

```javascript
{
   '/pet/{name}': {
       get: {
           '200': [{ name: 'spot', description:'some description of the data'}]
       }
   }
 }
```

#### 模拟查询参数

```javascript
{
   '/pet': {
       get: {
           '200': [{ name: 'spot', description:'some description of the data'}]
       }
   }
 }
```

#### 模拟HTTP报头

这将添加一个HTTP头`X-Token`设置为`WaErthyDtufJ`，假设您的 swagger API 具有该头的定义。

```javascript
{
   '/pet': {
       get: {
           '200': [{ 'X-Token': 'waestrydtufj', description:'some description of the data'}]
       }
   }
 }
```

支持状态代码请求数据数组中的多个对象。例如，这可能是：

```javascript
{
     '/pet': {
         post: {
             '200': [
               {
                 body: {
                  id: 1,
                  otherProperty: 'some property that is a string'
                 },
                 description: 'the description for this data'
               },
               {
                 body: {
                  id: 2,
                  otherProperty: 'another value of that property'
                 },
                 description: 'the description for another data'
               }
             ]
         },
         get: {
            '200': [
              {
                guid: 'some_string_to_place_in_path',
                anotherPathParam: 100,
                description: 'valid path or query parameters'
              },
              {
                guid: 'some_other_string_to_place_in_path',
                anotherPathParam: 200,
                description: 'another valid path or query parameters'
              }
          ]
         }
     }
 }
```

注意：对于GET请求，匹配的数据将被传递到 `pathParams`。因此，直接设置`config.pathParams`将具有相同的效果（见上文）。

`responseCode`中的每个mockData项将用于生成测试。描述将被添加到“it”函数以供参考。

在你的标记中Swagger API参数显式标记为`required: false`，只有在请求数据对象中有匹配值时才会设置。在Swagger API规范中没有显式设置`required`标志的必要参数和参数将被设置为requestData对象中的匹配值或“DATA GOES HERE”字符串。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://microservice-nodejs.shujuwajue.com/jia-gou-mo-kuai/swagger-test-templates.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
