• 使用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

  • 相关阅读:
    题目:返回一个整数数组中最大子数组的和。(要求程序必须能处理1000 个元素)
    四则运算三(接受用户输入答案,并判断对错。)
    二维数组
    结对开发(一位数组)
    测试四则运算
    四则运算2
    程序设计思路
    项目计划总结
    小学二年级题目的改进
    二年级题目的改进
  • 原文地址:https://www.cnblogs.com/rongfengliang/p/9801832.html
Copyright © 2020-2023  润新知