测试开发进阶(三十二)

测试开发进阶(三十二)

开启跨域

安装django-cors-headers

  1. 添加应用
1
2
3
4
5
6
7
8
9
10
11
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.添加中间件

1
2
3
4
5
6
7
8
9
10
11
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',

]
  1. 添加白名单
1
2
3
4
5
6
7
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 将上述框架进行封装,几乎可以零代码,高效率的进行测试

安装

1
$ pip install httprunner

创建工程

1
$ hrun --startproject httprunner_learn

创建项目

最小单元

新建api/login.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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]

测试

1
zhongxindeMacBook-Pro:httprunner_learn zhongxin$ hrun api/login.ym

查看报告

报告

测试2

使用环境变量

.env文件:${ENV(USERNAME)}

1
2
USERNAME=zhongxin
PASSWORD=123456
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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$
 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
您的支持将鼓励我继续创作!