测试开发进阶(三十二)

来呀~

开启跨域

安装 django-cors-headers

  1. 添加应用


     
     
     
     
  1. INSTALLED_APPS = [

  2. 'django.contrib.admin',

  3. 'django.contrib.auth',

  4. 'django.contrib.contenttypes',

  5. 'django.contrib.sessions',

  6. 'django.contrib.messages',

  7. 'django.contrib.staticfiles',

  8. 'rest_framework',

  9. 'corsheaders',

  10. 'user.apps.UserConfig',

  11. ]

2.添加中间件


     
     
     
     
  1. MIDDLEWARE = [

  2. 'django.middleware.security.SecurityMiddleware',

  3. 'django.contrib.sessions.middleware.SessionMiddleware',

  4. 'corsheaders.middleware.CorsMiddleware',

  5. 'django.middleware.common.CommonMiddleware',

  6. # 'django.middleware.csrf.CsrfViewMiddleware',

  7. 'django.contrib.auth.middleware.AuthenticationMiddleware',

  8. 'django.contrib.messages.middleware.MessageMiddleware',

  9. 'django.middleware.clickjacking.XFrameOptionsMiddleware',

  10. ]

  1. 添加白名单


     
     
     
     
  1. CORS_ORIGIN_ALLOW_ALL = True # 所有域名都允许,默认False

  2. # CORS_ORIGIN_WHITELIST = [

  3. # # 指定可以访问后端的域名

  4. # "http://127.0.0.1:8080",

  5. # "http://localhost:8080",

  6. # ]

  7. CORS_ALLOW_CREDENTIALS = True # 语序跨域时携带Cookie,默认False

测试开发进阶(三十二)_第1张图片

HttpRunner

接口测试有哪些方法

  • 工具:Postman

  • 使用代码创建接口测试框架

用例数据存放在哪?excel

unittest + ddt 数据驱动,测数据与测试代码分离

日志器

配置文件

数据库校验:pymysql

发起请求:request

参数化:正则匹配

接口依赖:动态创建类属性的方式来处理

Jenkins实现持续集成

HttpRunner 将上述框架进行封装,几乎可以零代码,高效率的进行测试

安装


     
     
     
     
  1. $ pip install httprunner

创建工程


     
     
     
     
  1. $ hrun --startproject httprunner_learn

测试开发进阶(三十二)_第2张图片

最小单元

新建 api/login.yml


     
     
     
     
  1. name: 登录接口

  2. variables:

  3. var1: value1

  4. var2: value2

  5. request:

  6. url: http://127.0.0.1:8000/user/login/

  7. method: POST

  8. headers:

  9. Content-Type: "application/json"

  10. json:

  11. username: zhongxin

  12. password: 123456

  13. validate:

  14. - eq: ["status_code", 200]

测试


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

查看报告

测试开发进阶(三十二)_第3张图片

测试开发进阶(三十二)_第4张图片

使用环境变量

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


     
     
     
     
  1. USERNAME=zhongxin

  2. PASSWORD=123456


     
     
     
     
  1. name: 登录接口

  2. variables:

  3. var1: value1

  4. var2: value2

  5. request:

  6. url: http://127.0.0.1:8000/user/login/

  7. method: POST

  8. headers:

  9. Content-Type: "application/json"

  10. json:

  11. username: ${ENV(USERNAME)}

  12. password: ${ENV(PASSWORD)}

  13. validate:

  14. - eq: ["status_code", 200]

查看完整日志:hrun api/login.yml--log-level debug


     
     
     
     
  1. zhongxindeMacBook-Pro:~ zhongxin$ hrun /Users/zhongxin/Desktop/httprunner_learn/api/login.yml --log-level debug

  2. INFO Loading environment variables from /Users/zhongxin/Desktop/httprunner_learn/.env

  3. DEBUG Set OS environment variable: USERNAME

  4. DEBUG Set OS environment variable: PASSWORD

  5. INFO Start to run testcase:

  6. 登录接口

  7. INFO POST http://127.0.0.1:8000/user/login/

  8. DEBUG request kwargs(raw): {'headers': {'Content-Type': 'application/json'}, 'json': {'username': 'zhongxin', 'password': '123456'}, 'verify': True}

  9. DEBUG processed request:

  10. > POST http://127.0.0.1:8000/user/login/

  11. > kwargs: {'headers': {'Content-Type': 'application/json'}, 'json': {'username': 'zhongxin', 'password': '123456'}, 'verify': True, 'timeout': 120}

  12. DEBUG

  13. ================== request details ==================

  14. url : 'http://127.0.0.1:8000/user/login/'

  15. method : 'POST'

  16. headers : {'User-Agent': 'python-requests/2.22.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive', 'Content-Type': 'application/json', 'Content-Length': '46'}

  17. body : b'{"username": "zhongxin", "password": "123456"}'

  18. DEBUG

  19. ================== response details ==================

  20. ok : True

  21. url : 'http://127.0.0.1:8000/user/login/'

  22. status_code : 200

  23. reason : 'OK'

  24. cookies : {}

  25. encoding : None

  26. 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'}

  27. content_type : 'application/json'

  28. json : {'token': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoxLCJ1c2VybmFtZSI6Inpob25neGluIiwiZXhwIjoxNTcyNjE5NTUzLCJlbWFpbCI6IjQ5MDMzNjUzNEBxcS5jb20ifQ.yzGNYIZhpeoDqHzZKg_VOY9kuBpmhHAxqqfEyn91BHA'}

  29. INFO status_code: 200, response_time(ms): 205.25 ms, response_length: 199 bytes

  30. DEBUG start to validate.

  31. DEBUG extract: status_code => 200

  32. DEBUG validate: status_code equals 200(int) ==> pass

  33. .

  34. ----------------------------------------------------------------------

  35. Ran 1 test in 0.215s

  36. OK

  37. DEBUG No html report template specified, use default.

  38. INFO Start to render Html report ...

  39. INFO Generated Html report: /Users/zhongxin/reports/1572533153.html

  40. zhongxindeMacBook-Pro:~ zhongxin$

你可能感兴趣的:(测试开发进阶(三十二))