-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.test.js
More file actions
187 lines (160 loc) · 6.16 KB
/
example.test.js
File metadata and controls
187 lines (160 loc) · 6.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
const Fastman = require("../dist/index").default;
const fastman = new Fastman();
const fs = require("fs");
const path = require("path");
const yaml = require("js-yaml");
const assert = require("assert");
const specPath = path.join(__dirname, "example.yaml");
const testedSpecPath = path.join(__dirname, "example_tested.yaml");
const postmanPath = path.join(__dirname, "postman.json");
let user = null;
describe("Fastman example", function () {
before(function () {
// 读取 yaml 文件到object中, 也可以直接读取json文件
const spec = yaml.safeLoad(fs.readFileSync(specPath).toString("utf8"));
fastman
.openApiVersion(3) //指定openapi版本,目前只支持3.0 必须
.load(spec) // 读取spec, 文档中应包含Server的描述, 或者通过 server方法添加
// .server({ // 添加server
// url: "http://localhost:3000",
// description: "测试文档",
// })
.globalHeaders({ // 配置全局测试使用的header, 可按照方法配置,也可设置 all: {} 为所有方法公用
all: {
"Authorization": "Bearer 345453434gfdfgdfdf",
},
post: {
"Content-Type": "application/json",
},
put: {
"Content-Type": "application/json",
},
})
.resource("Users"); // 绑定资源,设置之后未来的测试都在Users资源下
});
after(function () {
// 输出测试结果
// 写入openapi文档到文件
fs.writeFileSync(testedSpecPath, fastman.writeSpec());
// 写入测试用例到
fastman.exportPostman(postmanPath);
});
it("Test List User", async function () {
await fastman
.path("/users") // 选择当前测试的路径
.operation("get") // 选择当前测试的方法
.response("200") // 选择当前测试的status code,fastman会自动assert结果中的 stauts code
.testCase({ // 只有在resource, path, operation ,response 都正确配置之后,才可以配置测试用例
name: "ListUserOK", // 测试用例的名称,fastman会将这一名称写入openapi文档和postman文件中
save: true, // 结果是否保存,测试结果是否保存在openapi文档的example中
query: { // 配置request query
offset: 0,
limit: 10,
},
})
.except((resp) => { // 添加一些测试代码
let body = resp.json(); // resp 是newman 自动返回的Response类型,没有找到相关文档
// resp.json() json方式获取body 注意!如果Response没有body, 这里会抛出一个异常
assert(body.length === 10);
const xTotal = resp.headers.get("X-Total"); // resp.headers.get(key) // 获取headers
assert(xTotal !== 0);
})
.run() // run 返回的是一个 Promise<Response>
});
it('Test Create User ', async function () {
let resp = await fastman
.path("/users")
.operation("post")
.response("200")
.testCase({
name: "CreateUerOK",
save: true,
body: {
name: "MagicCaptain",
},
}).run();
// 测试代码也可以放在后面写
const body = resp.json();
assert(body.id);
user = body.id;
assert(body["name"] === "MagicCaptain");
});
it("Test get User", async function () {
let resp = await fastman
.path("/users/{userId}")
.operation("get")
.response("200")
.testCase({
name: "GetUserOK",
save: true,
params: [user],
}).run();
let body = resp.json();
assert(body.name === "MagicCaptain");
assert(body.id === user);
// 连续测试同一个 path,operation,response时不需要重新配置
resp = await fastman
.response("404")
.testCase({
name: "GetUserNotFound",
save: true,
params: ["fdfdfder3"],
})
.run();
body = resp.json();
assert(body.name === "NotFound");
});
it("Test update user", async function () {
const okResp = await fastman
.path("/users/{userId}")
.operation("put")
.response(200)
.testCase({
name: "UpdateUserOK",
save: true,
params: [user],
body: {
name: "CaptainMagic",
},
})
.run();
const body = okResp.json();
assert(body.name === "CaptainMagic");
// 测试一下not found
const resp404 = await fastman
.response(404)
.testCase({
name: "UpdateUserNotFound",
save: true,
params: ["dfdfdrt43"],
body: {
name: "CaptainMagic",
},
})
.run();
const body404 = resp404.json();
assert(body404.name === "NotFound");
});
it("Test delete user", async function () {
const okResp = await fastman
.path("/users/{userId}")
.operation("delete")
.response(204)
.testCase({
name: "DeleteUserOK",
save: true,
params: [user],
})
.run();
const resp404 = await fastman
.response(404)
.testCase({
name: "DeleteUserNotFound",
save: true,
params: ["dfdfdrt43"],
})
.run();
const body404 = resp404.json();
assert(body404.name === "NotFound");
});
});