测试开发进阶(三十二)
程序员文章站
2024-03-22 08:10:16
...
开启跨域
安装 django-cors-headers
添加应用
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'corsheaders',
'user.apps.UserConfig',
]
2.添加中间件
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
# 'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
添加白名单
CORS_ORIGIN_ALLOW_ALL = True # 所有域名都允许,默认False
# CORS_ORIGIN_WHITELIST = [
# # 指定可以访问后端的域名
# "http://127.0.0.1:8080",
# "http://localhost:8080",
# ]
CORS_ALLOW_CREDENTIALS = True # 语序跨域时携带Cookie,默认False
HttpRunner
接口测试有哪些方法
工具:Postman
使用代码创建接口测试框架
用例数据存放在哪?excel
unittest + ddt 数据驱动,测数据与测试代码分离
日志器
配置文件
数据库校验:pymysql
发起请求:request
参数化:正则匹配
接口依赖:动态创建类属性的方式来处理
Jenkins实现持续集成
HttpRunner 将上述框架进行封装,几乎可以零代码,高效率的进行测试
安装
$ pip install httprunner
创建工程
$ hrun --startproject httprunner_learn
最小单元
新建 api/login.yml
name: 登录接口
variables:
var1: value1
var2: value2
request:
url: http://127.0.0.1:8000/user/login/
method: POST
headers:
Content-Type: "application/json"
json:
username: zhongxin
password: 123456
validate:
- eq: ["status_code", 200]
测试
zhongxindeMacBook-Pro:httprunner_learn zhongxin$ hrun api/login.ym
查看报告
使用环境变量
.env
文件: ${ENV(USERNAME)}
USERNAME=zhongxin
PASSWORD=123456
name: 登录接口
variables:
var1: value1
var2: value2
request:
url: http://127.0.0.1:8000/user/login/
method: POST
headers:
Content-Type: "application/json"
json:
username: ${ENV(USERNAME)}
password: ${ENV(PASSWORD)}
validate:
- eq: ["status_code", 200]
查看完整日志:hrun api/login.yml--log-level debug
zhongxindeMacBook-Pro:~ zhongxin$ hrun /Users/zhongxin/Desktop/httprunner_learn/api/login.yml --log-level debug
INFO Loading environment variables from /Users/zhongxin/Desktop/httprunner_learn/.env
DEBUG Set OS environment variable: USERNAME
DEBUG Set OS environment variable: PASSWORD
INFO Start to run testcase:
登录接口
INFO POST http://127.0.0.1:8000/user/login/
DEBUG request kwargs(raw): {'headers': {'Content-Type': 'application/json'}, 'json': {'username': 'zhongxin', 'password': '123456'}, 'verify': True}
DEBUG processed request:
> POST http://127.0.0.1:8000/user/login/
> kwargs: {'headers': {'Content-Type': 'application/json'}, 'json': {'username': 'zhongxin', 'password': '123456'}, 'verify': True, 'timeout': 120}
DEBUG
================== request details ==================
url : 'http://127.0.0.1:8000/user/login/'
method : 'POST'
headers : {'User-Agent': 'python-requests/2.22.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive', 'Content-Type': 'application/json', 'Content-Length': '46'}
body : b'{"username": "zhongxin", "password": "123456"}'
DEBUG
================== response details ==================
ok : True
url : 'http://127.0.0.1:8000/user/login/'
status_code : 200
reason : 'OK'
cookies : {}
encoding : None
headers : {'Date': 'Thu, 31 Oct 2019 14:45:53 GMT', 'Server': 'WSGIServer/0.2 CPython/3.7.1', 'Content-Type': 'application/json', 'Vary': 'Accept, Origin', 'Allow': 'POST, OPTIONS', 'X-Frame-Options': 'SAMEORIGIN', 'Content-Length': '199'}
content_type : 'application/json'
json : {'token': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoxLCJ1c2VybmFtZSI6Inpob25neGluIiwiZXhwIjoxNTcyNjE5NTUzLCJlbWFpbCI6IjQ5MDMzNjUzNEBxcS5jb20ifQ.yzGNYIZhpeoDqHzZKg_VOY9kuBpmhHAxqqfEyn91BHA'}
INFO status_code: 200, response_time(ms): 205.25 ms, response_length: 199 bytes
DEBUG start to validate.
DEBUG extract: status_code => 200
DEBUG validate: status_code equals 200(int) ==> pass
.
----------------------------------------------------------------------
Ran 1 test in 0.215s
OK
DEBUG No html report template specified, use default.
INFO Start to render Html report ...
INFO Generated Html report: /Users/zhongxin/reports/1572533153.html
zhongxindeMacBook-Pro:~ zhongxin$
上一篇: Kaggle-房价预测
下一篇: 三级联动案列