线上第八期_接口测试实战 下 3-20190414

如何定位代码问题

最近很多同学开始利用老师上课的框架,练习或者应用到自己的实际项目中,遇到不少问题,但苦于不知道如何去解决,无从下手。在本次课程开始之前,希望能收集到大家平常遇到的问题,教大家怎么有个正确的思路,能独立的解决问题。

出现如下问题,清除pytest的缓存

py._path.local.LocalPath.ImportMismatchError: (‘testcasebak.conftest’, ‘/Users/xxx/Desktop/api_student/src/testcase/conftest.py’, local(’/Users/xxx/Desktop/api_student/src/testcasebak/conftest.py’))
ERROR: could not load /Users/xxx/Desktop/api_student/src/testcasebak/conftest.py

bash#rm -rf pycache

如果服务器连接不上

telnet ip port

bash# testcase telnet 127.0.0.1 5000
Trying 127.0.0.1…
Connected to localhost.
Escape character is ‘^]’.

练习1

通过配置文件,发送req请求,并通过配置文件比较返回结果(全量比较)。

{
  "testcase1":
  {"req":
{
  "userid": "liebao20190414",
    "name": "猎豹",
    "alias": "liebao",
    "mobile": "16613215421",
    "department": [41, 45],
    "order":[10,40],
    "position": "产品经理",
    "gender": "1",
    "email": "liebao@gzdev2.com",
    "is_leader_in_dept": [1, 0],
    "enable":1,
    "avatar_mediaid": "2FA6SlMIgOiW0jbxks3fztkv9dm8Pw7E7AORvW2Xq-SY",
    "telephone": "021-1223456",
    "address": "广州市海珠区新港中路"
},
  "res":
    {
      "errcode": 0,
      "errmsg": "created"
}

},
"testcase2":
{
    "userid": "liebao",
    "name": "猎豹",
    "alias": "liebao",
    "mobile": "16913215421",
    "department": [41, 45],
    "order":[10,40],
    "position": "产品经理",
    "gender": "1",
    "email": "liebao@gzdev.com",
    "is_leader_in_dept": [1, 0],
    "enable":1,
    "avatar_mediaid": "2FA6SlMIgOiW0jbxks3fztkv9dm8Pw7E7AORvW2Xq-SY",
    "telephone": "021-1223456",
    "address": "广州市海珠区新港中路"
}
}

生成邮件测试报告所需要模块

pip install pyecharts
npm install -g phantomjs-prebuilt # 或者cnpm install -g phantomjs-prebuilt
pip install pyecharts-snapshot

pip install emails

toolskit.py

#! /usr/bin/env python
#coding=utf-8

import time
import random

def update_json_value_by_key(json_obj,key,new_value):
     json_obj[key]=new_value
     return json_obj

def append_time_stamp_string(old_value):
     return old_value+"_"+time.strftime('%Y%m%d%H%M%S')


def get_random_mobile():
    for k in range(10):
        prelist=["130", "131", "132", "133", "134", "135", "136", "137", "138", "139",
                 "147", "150", "151", "152", "153", "155", "156", "157", "158", "159",
                 "186", "187", "188", "189"]
        return random.choice(prelist)+"".join(random.choice("0123456789") for i in range(8))

def get_random_string(length=5):
     low_case = [chr(i) for i in range(65, 91)]
     up_cases =[chr(i) for i in range(97, 123)]
     all_string="".join(low_case)+"".join(up_cases) +"0123456789"
     return "".join(random.choice(all_string) for i in range(length))

if __name__=="__main__":
     print(get_random_string(6))

练习2

把userid, email,mobile字段变成动态值,就是每次提交的请求,这3个字段的值都不一样

{
  "testcase1":
  {"req":
{
  "userid": "liebao",
    "name": "猎豹",
    "alias": "liebao",
    "mobile": "16613215421",
    "department": [41, 45],
    "order":[10,40],
    "position": "产品经理",
    "gender": "1",
    "email": "liebao@gzdev.com",
    "is_leader_in_dept": [1, 0],
    "enable":1,
    "avatar_mediaid": "2FA6SlMIgOiW0jbxks3fztkv9dm8Pw7E7AORvW2Xq-SY",
    "telephone": "021-1223456",
    "address": "广州市海珠区新港中路"
},
  "res":
    {
      "errcode": 0,
      "errmsg": "created"
}

},
"testcase2":
{
    "userid": "liebao",
    "name": "猎豹",
    "alias": "liebao",
    "mobile": "16913215421",
    "department": [41, 45],
    "order":[10,40],
    "position": "产品经理",
    "gender": "1",
    "email": "liebao@gzdev.com",
    "is_leader_in_dept": [1, 0],
    "enable":1,
    "avatar_mediaid": "2FA6SlMIgOiW0jbxks3fztkv9dm8Pw7E7AORvW2Xq-SY",
    "telephone": "021-1223456",
    "address": "广州市海珠区新港中路"
}
}

为什么断言数据也需要数据化

  1. 响应的字段名字或者字段值都可能发生变化
  2. 响应的字段增加或者减少
关闭