自动化测试学习:pytest + request基础知识 - 把【控制手机App】和pytest结合起来——App自动化

📅 2026-11-30 👤 拼多多助力营销技巧 📂 首码项目 👁️ 428 次阅读

1、pytest 快速掌握

pytest是测试框架。用于:用例发现、用例筛选、用例执行、用例报告、第三方插件。

(1)安装pytest

pip install pytest

(2)使用pytest(创建用例、执行用例)

基本要求:

1、创建test开头的py文件;

2、创建test_开头的函数;

3、在函数中使用断言

#举例:创建一个含test的py文件,如test_api.py,并在这文件中创建一个函数,函数名含test

def test_abc():

assert 1 == 2 //断言

(3) 启动框架、执行用例命令:pytest

(4)查看参数、配置、使用说明:pytest -h

(5)pytest 比较常用的参数:【 pytest -s :关闭io捕获】

mark(标记用例,被标记的用例不会再执行):@pytest.mark.skip

fixture(用例之间传递内容)

hook(改变pytest)

2、requests快速掌握

requests主要用于向接口发送请求。

把【向接口发送请求】和pytest结合起来——接口自动化

把【控制浏览器】和pytest结合起来——Web自动化

把【控制手机App】和pytest结合起来——App自动化

1、安装request

pip install requests

2、使用request

(1)导入request:import requests

(2)创建一个函数,如:

import requests

def test_baidu():

resp = requests.request(

method='get', #http请求方法,必填

url='https://www.baidu.com', #接口地址,必填

data={"a":1, "b":2} , #接口参数,选填,类型有多种

json={}, #接口参数,选填,类型有多种

files={}, #接口参数,选填,类型有多种

headers={} #接口参数,选填,类型有多种

)

print(resp.status_code) #打印出接口返回的状态码

print(resp.headers) #打印出接口返回的请求头

print(resp.text)

assert resp.status_code == 200 #使用断言;断言就是对结果的判断

assert 'baidu' in resp.text

3、常出现的场景

(1)表单参数

主要用在web项目:

1、参数只能是字符串;

2、请求头中包含from;

//data参数封装成字典

user_info ={

"username":"sanmu",

"password":"123456"

}

def test_api_form():

resp = requests.request(

method="post",

url="http://api.fbi.com:9225/rest-v1/login/with_form",

data=user_info, #封装成字典 :如果data参数是一个字典,则自动将其识别为表单,并自动添加请求头

)

assert resp.status_code == 200

request技巧:

1)如果data参数是一个字典,则自动将其识别为表单,并自动添加请求头。

(2)json参数

主要用在各类项目中:

1、参数类型很多:字符串、数字、布尔值、空值、数组;

2、请求头中包含json;

user_info ={

"username":"sanmu",

"password":"123456"

}

def test_api_json():

resp = requests.request(

method="post",

url="http://api.fbi.com:9225/rest-v1/login/with_form",

json=user_info, #json的方式传参

)

assert resp.status_code == 200

request技巧:

1)如果传递“json参数”,自动识别为json参数类型,自动添加请求。

(3)文件上传

主要用在各类项目中:

1、上传方式:body直传、表单。

2、请求头说明使用哪种上传方式。

def test_file_upload():

path = r"E:\testPycharm\python_learn\常用知识.txt" #字符串

f = open(path,'r') #文件对象

resp = requests.request(

method="post",

url="http://api.fbi.com:9225/rest-v1/upload/one_file",

files={"file": f}, # 表单上传的方式传参

)

assert resp.status_code == 200 拼多多帮助力入口

request技巧:

1)如果传递“files参数”,自动识别为表单文件上传,自动添加请求。

(4)接口关联

通过变量进行关联:创建变量、传递变量、读取变量、打印变量。

流程:

(1)请求第一个接口,得到响应;

(2)从响应中提取数据,创建变量;

(3)在第二个接口中,使用变量;

1、如何从上一个响应中提取变量:

(1)正则:re

(2)jsonpath:json接口 用jsonpath进行提取

(3)xpath

def test_token():

resp = requests.request(

method='post',

url='http://api.fbi.com:9225/rest-v1/login/with_form',

data=user_info

)

assert resp.status_code == 200

print(resp.text) #接口返回的东西打印出来:会返回token,身份凭据

#变量提取

token = jsonpatch.JsonPatch(resp.json(), '$.token')[0]

resp = requests.request(

method='get',

url='http://api.fbi.com:9225/rest-v1/auth/token_with_header',

headers={"token": token} #使用变量

)

assert resp.status_code == 200

(5)参数化测试

数据驱动测试 = 参数化测试+数据文件(csv,json,yaml,execl,mysql)

3、日志、报告、插件等框架封装

pytest是一个专注用例执行的框架。如果需要增加新的功能,需要借助第三方插件。

1、I-P-O模型:

(1)输入简单:用力不是代码,只是数据

(2)输出简单:不是文本,是精美的网页

(3)处理灵活:记录日志、发送通知……,自动化测试学习

标签: 拼多多助力网站平台 赚钱项目 拼多多助力网 首码项目 手赚APP
拼多多助力营销技巧