測試開發進階(三十二)

來呀~

開啓跨域

安裝 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

HttpRunner

接口測試有哪些方法

  • 工具:Postman

  • 使用代碼創建接口測試框架

用例數據存放在哪?excel

unittest + ddt 數據驅動,測數據與測試代碼分離

日誌器

配置文件

數據庫校驗:pymysql

發起請求:request

參數化:正則匹配

接口依賴:動態創建類屬性的方式來處理

Jenkins實現持續集成

HttpRunner 將上述框架進行封裝,幾乎可以零代碼,高效率的進行測試

安裝

  1. $ pip install httprunner

創建工程

  1. $ hrun --startproject httprunner_learn

最小單元

新建 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

查看報告

使用環境變量

.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$

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章