接口自动化:六.pytest写用例

前言

自动化用例怎么写?

(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
原文地址:https://www.cnblogs.com/liushui0306/p/13188242.html