使用just-api 进行接口测试

just-api 是基于配置的测试,同时支持基于jsonpath jsonschema 的数据校验,
对于数据的请求只集成hook,支持测试失败重试、测试报告、graphql api 测试。。。。

使用docker-compose 运行

项目初始化

参考项目 https://github.com/rongfengliang/just-api-basic-test

  • 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

posted on 2018-10-17 08:26  荣锋亮  阅读(545)  评论(0编辑  收藏  举报

导航