前言
自動化用例怎么寫?
(1).功能覆蓋率=至少執行一次的測試功能點數/測試功能點總數(功能點)
(2)需求覆蓋率=被驗證到的需求總量/總的需求總量(需求)
(3)覆蓋率=至少被執行一次的測試用例數/應執行的測試用例總數
接口自動化用例覆蓋率=已經實現的自動化用例格式/總接口用例個數
環境准備
Python 3.6.8
pytest5.3.5
前面我們已經安裝好了Python3以及pip3,下面直接安裝pytest
安裝pytest
pip3 install pytest ==5.3.5
#查看pytest版本
[root@VM_0_11_centos ~]# pip3 show pytest Name: pytest Version: 5.3.5 Summary: pytest: simple powerful testing with Python Home-page: https://docs.pytest.org/en/latest/ Author: Holger Krekel, Bruno Oliveira, Ronny Pfannschmidt, Floris Bruynooghe, Brianna Laugher, Florian Bruhin and others Author-email: None License: MIT license Location: /root/python36/lib/python3.6/site-packages Requires: importlib-metadata, wcwidth, py, more-itertools, packaging, pluggy, attrs Required-by:
pytest用例設計原則
文件名test_*.py文件和*_test.py
以test_開頭的函數
以Test開頭的類
以test開頭的方法
所有的package必須有__init__.py文件
pytest用例
寫自動化用例之前,先寫功能用例,要不然就是無用功。先用fiddler跑一下接口能跑通,在寫接口代碼。
fiddler接口發送請求
pytest寫用例
斷言:實際結果和期望結果對比
我們先要搞清楚兩件事情: 1.執行用例完成后,肯定有實際的結果 2.每個用例都有預期結果 拿着兩個結果對比
加斷言后用代碼
import requests
import pytest
def test_qq_1():
'''用例描述: QQ號碼-必填項 key,輸入正確的key值,請求成功'''
url = "http://japi.juhe.cn/qqevaluate/qq"
parm = { "key": "8dbee1fcd8627fb6699bce7b986adc45", "qq": "12345678" }
r = equests.get(url, params=parm)
print("返回的結果:%s" % r.text)
# 實際結果
result_reason = r.json().get("reason")
result_error_code = r.json().get("error_code")
# 期望結果 "error_code":0,"reason":"success"
assert result_reason == "success"
assert result_error_code == 0