Java自动化测试(mock 21)

mock测试就是在测试过程中,对于某些不容易构造或者不容易获取的对象,用一个虚拟的对象来创建以便测试的测试方法。

mock测试环境

https://www.easy-mock.com/login

https://github.com/easy-mock/easy-mock

Easy Mock 是一个可视化,并且能快速生成模拟数据的持久化服务。

特性

  • 支持接口代理

  • 支持快捷键操作

  • 支持协同编辑

  • 支持团队项目

  • 支持 RESTful

  • 支持 Swagger | OpenAPI Specification (1.2 & 2.0 & 3.0)

  • 基于 Swagger 快速创建项目

  • 支持显示接口入参与返回值

  • 支持显示实体类

  • 支持灵活性与扩展性更高的响应式数据开发

  • 支持自定义响应配置(例:status/headers/cookies)

  • 支持 Mock.js 语法

  • 支持 restc 方式的接口预览

Java自动化测试(mock 21)_第1张图片 登陆

单机版

https://github.com/dreamhead/moco

Maven


   4.0.0
   com.lemon
   moco-runner
   0.0.1-SNAPSHOT
   
      1.8
      1.8
      
      UTF-8
      UTF-8
      
      UTF-8
   
   
      
         com.github.dreamhead
         moco-runner
         1.0.0
      
   


runner.java

package com.lemon;

import com.github.dreamhead.moco.bootstrap.Main;

public class Runner {
   public static void main(String[] args) {
      test9();
   }

   public static void test1() {
      // get http://localhost:12306/
      String[] args = "http -p 12306 -c src/test/resources/foo.json".split(" ");
      Main.main(args);
   }

   public static void test2() {
      // get http://localhost:12306/ body foo
      String[] args = "http -p 12307 -c src/test/resources/foo2.json".split(" ");
      Main.main(args);
   }

   public static void test3() {
      // get http://localhost:12306/getBoy?name=onecoder
      String[] args = "http -p 12306 -c src/test/resources/foo3.json".split(" ");
      Main.main(args);
   }

   public static void test4() {
      // post http://localhost:12306/getBoy body name=onecoder
      String[] args = "http -p 12306 -c src/test/resources/foo4.json".split(" ");
      Main.main(args);
   }

   public static void test5() {
      // get http://localhost:12306/template?name=123 返回 123
      String[] args = "http -p 12306 -c src/test/resources/foo5.json".split(" ");
      Main.main(args);
   }

   public static void test6() {
      // get http://localhost:12306/template?name=123 返回 123
      String[] args = "http -p 12306 -c src/test/resources/foo6.json".split(" ");
      Main.main(args);
   }

   public static void test7() {
      // get http://localhost:12306/template?name=123 返回 123
      String[] args = "http -p 12306 -c src/test/resources/foo7.json".split(" ");
      Main.main(args);
   }

   public static void test8() {
      // get http://localhost:12306/template?name=123 返回 123
      String[] args = "http -p 12306 -c src/test/resources/foo8.json".split(" ");
      Main.main(args);
   }

   public static void test9() {
      // get http://localhost:12306/template?name=123 返回 123
      String[] args = "http -p 12306 -c src/test/resources/foo9.json".split(" ");
      Main.main(args);
   }
}

测试数据

  • foo1.json

[
  {
    "response" :
      {
        "text" : "Hello, Moco"
      }
  }
] 
  • foo2.json

[{
  "request" : { "text" : "foo" },
  "response" : {  "xml" : "bar" }
}]
  • foo3.json

[{
   "request": {
      "uri": "/getBoy",
      "queries": {
         "name": "onecoder"
      }
   },
   "response": {
      "text": "get Hey."
   }
}]
  • foo4.json

[
   {
      "request": {
         "method": "post",
         "forms": {
            "name": "onecoder"
         }
      },
      "response": {
         "text": "post Hi."
      }
   }
]
  • foo5.json

[
   {
      "request": {
         "uri": "/template"
      },
      "response": {
         "text": {
            "template": "aaaaaaaa${req.queries['name']}bbbbbbb"
         }
      }
   }
]
  • foo6.json

[
   {
      "description": "带header请求",
      "request": {
         "uri": "/member/register",
         "method": "post",
         "headers": {
            "content-type": "application/json"
         },
         "json": {
            "mobile_phone": "13212312312",
            "pwd": "12345678"
         }
      },
      "response": {
         "json": {
            "code": 0,
            "msg": "OK",
            "data": {
               "id": 189,
               "reg_name": "小柠檬",
               "mobile_phone": "13212312312"
            }
         }
      }
   }
]
  • foo7.json

[
   {
      "description": "这是一个带cookies的Post请求",
      "request": {
         "uri": "/postDemoWithCookies",
         "method": "post",
         "cookies": {
            "login": "true"
         }
      },
      "response": {
         "status": "200",
         "json": {
            "name": "success",
            "status": "1"
         },
         "cookies": {
            "JSESSIONID": "123"
         }
      }
   }
]
  • foo8.json

[
   {
      "description": "充值",
      "request": {
         "uri": "/member/recharge",
         "method": "post",
         "headers": {
            "X-Lemonban-Media-Type": "lemonban.v2",
            "Authorization" : "Bearer Token",
            "content-type": "application/json"
         },
         "json": {
            "member_id": "123",
            "amount": "4000"
         }
      },
      "response": {
         "json": {
            "code": 0,
            "msg": "OK",
            "data": {
               "id": 123,
               "leave_amount": 1800000.01 ,
               "mobile_phone": "13888888879",
               "reg_name": "小柠檬",
               "reg_time": "2019-08-25 21:18:30.0",
               "type": 1
            }
         }
      }
   }
]
  • foo9.json

[
   {
      "description": "登录",
      "request": {
         "uri": "/member/login",
         "method": "post",
         "headers": {
            "X-Lemonban-Media-Type": "lemonban.v2",
            "content-type": "application/json;charset=utf-8"
         },
         "json": {
            "mobile_phone": "13212312312",
            "pwd": "12345678"
         }
      },
      "response": {
         "json": {
            "code": 0,
            "msg": "OK",
            "data": {
               "id": 70362,
               "leave_amount": 0.0,
               "mobile_phone": "13212312312",
               "reg_name": "小柠檬",
               "reg_time": "2019-12-19 21:12:21.0",
               "type": 1,
               "token_info": {
                  "token_type": "Bearer",
                  "expires_in": "2019-12-19 21:17:23",
                  "token": "eyJhbGciOiJIUzUxMiJ9.eyJtZW1iZXJfaWQiOjg5ODU4LCJleHAiOjE1NzY3NjE0NDN9.ZJKz-aOxWtCQakvhOfRjTSTA_K7amiKe0e9wpaF9lpzKQaTZiVjsoUDmpA2DCKtIdMyaQ4QYWWybP1ZD5QDVWQ"
               }
            },
            "copyright": "Copyright 柠檬班 © 2017-2019 湖南省零檬信息技术有限公司 All Rights Reserved"
         }
      }
   }
]

你可能感兴趣的:(Java自动化测试(mock 21))