使用just-api 进行接口测试
just-api 是基于配置的测试,同时支持基于jsonpath jsonschema 的数据校验,
对于数据的请求只集成hook,支持测试失败重试、测试报告、graphql api 测试。。。。
使用docker-compose 运行
项目初始化
- yarn
yarn init -y
- 添加依赖
yarn add just-api
- 配置npm scripts
{
"name": "just-api-project",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"just-api": "^1.2.0"
},
"scripts": {
"test":"just-api",
"report":"just-api --reporter html,json"
}
}
创建基本项目
just-api 有默认的测试运行目录,默认是当前目录的specs 可以在运行的时候修改
- 测试定义
参考项目 specs/starwars_service.yml,比较简单就是指定测试接口地址,测试request response 以及数据
校验规则
meta:
name: "星球大战测试"
configuration:
scheme: "https"
host: "swapi.co"
base_path: "/api"
specs:
- name: "get Luke Skywalker info"
request:
path: "/people/1/"
method: "get"
response:
status_code: 200
headers:
- name: "content-type"
value: !!js/regexp application/json
json_data:
- path: "$.name"
value: "Luke Skywalker"
- 运行
yarn test
- 生成报告
yarn report
- 效果
docker构建支持
比较简单使用nginx 做为报告的显示,同时使用泛域名服务支持虚拟主机
- Dockerfile (多阶段构建)
FROM node:10.7-alpine as build-env
WORKDIR /app
COPY . /app
RUN npm install -g yarn
RUN yarn
RUN yarn report && cp report.html /tmp/index.html
FROM openresty/openresty:alpine
COPY nginx.conf usr/local/openresty/nginx/conf/
COPY --from=build-env /tmp/index.html /usr/local/openresty/nginx/html/index.html
COPY --from=build-env /app/app.html /usr/local/openresty/nginx/html/app.html
EXPOSE 80
EXPOSE 443
- docker-compose
version: "3"
services:
web:
build: ./
ports:
- "8080:80"
- nginx 配置
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
gzip on;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
server {
listen 80;
server_name localhost;
charset utf-8;
root html;
location / {
index index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name 127.0.0.1.easylify.com;
charset utf-8;
root html;
location / {
index index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name app.127.0.0.1.easylify.com;
charset utf-8;
root html;
location / {
index app.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
说明
测试比较简单,实际有好多特性比如hook,cookies header 处理。。。。都是比较方便的http 测试要用到的,同时支持graphql 的测试。。。
参考资料
https://github.com/kiranz/just-api
https://github.com/rongfengliang/just-api-basic-test
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
2016-10-17 HTTP Proxy Servlet 代理服务使用