接口自动化pytest框架学习

1、pytest用例管理框架的作用

发现测试用例: 从多个py文件中通过默认的规则去找测试用例
执行测试用例:顺序和条件
判断测试结果:断言
生产测试报告:html、allure

2、pytest的插件

pytest-html                       生成简易报告
pytest-xdist                      多线程
pytest-ordering                   控制测试用例的执行顺序
pytest-rerunfailures               失败用例重跑
pytest-base-url                   基础路径的配置
allure-pytest                     生成alure报告

将上面的插件名放到requirements.txt 中,不要中文,并且把requirements.txt放到项目根目录
安装

pip install -r requirements.txt

3、pytest 发现测试用例的规则

 1、 模块名必须以test_开头或是_test结尾
 2、 测试类必须以Test开头,并且不能有init 方法
 3、 测试方法以test_开头

4、 pytest的运行方法
方式一:命令行的运行方式运行命令:

pytest

可以添加参数

-v             输出更加详细的信息
-s             输出调试信息
-n             多线程运行
--reruns       失败用例重跑
--html         生成简易报告
-x             出现一个用例失败就停止测试
--maxfail      出现几个用例失败就停止测试
-k             执行的测试用例的名称中包含某个字符串的测试用例

运行测试子集

运行单个目录:               pytest tests/func
运行单个测试文件/模块         pytest tests/func/test_add.py
运行单个测试函数             pytest tests/func/test_add.py::test_one
运行单个测试类               pytest tests/func/test_add.py::TestAdd
运行测试类中的单个测试方法     pytest tests/func/test_add.py::TestAdd::test_one

方式二: 主函数的运行方式
将run.py放在项目根目录下

if __name__ == '__main__':
    pytest.main()

方式三:通过pytest.ini文件运行
注意:
一般放在项目的根目录下,名称必须是pytest.ini
编码格式为gb2312,有中文的时候
pytest.ini 可以改变默认的测试用例规则
不管是命令行运行还是主函数运行都会加载这个配置文件

[pytest]
addopts = -vs -m "smoke"
testpaths = ./testcases
python_files = test_*.py
python_classes = Test*
python_functions = test_*
markers =
    smoke:冒烟用例
    systemmanage:系统管理

-m “smoke” 表示只执行冒烟测试用例
5、pytest的用例执行顺序
默认是从上到下
可以通过如下标记改变用例执行顺序
@pytest.mark.run(order=1)
全为正数或负数时,值越小,优先级越高
既有正数又有负数时,正数优先级高(-1,2,-3的执行顺序是2,-3,-1)
6、接口关联封装
原因:如果说使用类变量的话,那么这个类变量只能在当前的py文件中使用,不能在其他py文件中使用
策略:把整个接口自动化的所有中间变量都保存到一个extract.yaml 文件中
7、pytest跳过测试用例
1)、无条件跳过
@pytest.mark.skip(reason=“无条件跳过”)

import pytest
class TestApi:
    @pytest.mark.smoke
    def test_jingtian(self):
        print("jingtian")

    def test_xiyao(self):
        print("夕瑶")
    @pytest.mark.skip(reason="无条件跳过")
    def test_longkui(self):
        print("龙葵")

2)、有条件跳过
@pytest.mark.skipif(workage<10,reason=“工作经验不足10年”)

import pytest
class TestSchedule:
    workage=8
    @pytest.mark.smoke
    def test_xuejian(self):
        print("xuejian")

    def test_xiyao(self):
        print("夕瑶")
    @pytest.mark.skip(reason="无条件跳过")
    def test_longkui(self):
        print("龙葵")
    @pytest.mark.skipif(workage<10,reason="工作经验不足10年")
    def test_jingtian(self):
        print("景天")

7、pytest测试用例的前后置固件
在common目录下创建CommonUtil类

class CommonUtil:
    def setup_class(self):
        print("每个类之前执行一次")

    def teardown_class(self):
        print("每个类之后执行一次")

    def setup(self):
        print("每个测试用例执行前执行一遍")

    def teardown(self):
        print("每个测试用例执行后执行一遍")

在测试用例的类中继承CommonUtil类,实现调用

import pytest
class TestApi(CommonUtil):

    def test_jingtian(self):
        print("jingtian")

    def test_xiyao(self):
        print("夕瑶")
   
    def test_longkui(self):
        print("龙葵")

8、使用fixture实现部分前后置
setup、teardown可以实现在执行用例前或结束后加入一些操作,但这种都是针对整个脚本全局生效的
如果有以下场景:用例 1 需要先登录,用例 2 不需要登录,用例 3 需要先登录。很显然无法用 setup 和 teardown 来实现了

@pytest.fixture(scope="function",autouse=True)

1、scope: 作用域
function: 在函数之前或之后执行
1、手动调用的方式是在测试用例里参数里面加入fixture的名字
2、如果说fixture有通过return 或是yield返回值的话,可以通过测试用例的参数传到测试用例当中,传fixture的名字
class 在类之前或之后执行
1、手动调用是在类的上面加@pytest.mark.usefixtures(“exe_database_sql”)装饰器
package/session 在整个项目会话之前或之后执行

2、autouse: 自动执行,默认False, True就不需要调用,每个用例前都自动执行

@pytest.fixture(scope="function",autouse=True)
def exe_database_sql():
    print("执行sql语句")
    yield
    print("关闭数据库连接")

3、params: 实现参数化
如何把值传到fixture,是通过fixture的函数里面加入参数resquest来接收参数,然后通过request.param来取值。

def read_yaml():
    return ['001','002','003']

@pytest.fixture(scope="function",autouse=False,params=read_yaml())
def exe_database_sql(request):
    print("执行sql语句")
    yield request.param
    print("关闭数据库连接")

4、name :给fixture取别名,一但取了别名就不能在用之前的名称了,要使用别名

9、fixture结合conftest.py文件使用

1、conftest.py 是专门存放fixture的配置文件,他的名称固定,不能变。
2、在conftest.py 文件里面的所有方法都不需要导包。
3、conftest.py 文件可以有多个,并且多个conftest.py里的多个fixture可以被一个用例调用

10、 setup、teardown、setup_class、teardown_class、fixture、 conftest 的优先级

会话:    fixture 的session级别的优先级最高
类:     fixture的class级别的优先次之
类:     teardown_class 的级别次之
函数:   fixture的function的级别的优先级次之
函数:   setup   的优先级最后

11、allure-pytest 生成报告
1、安装allure-pytest的插件
2、下载allure, 下载解压后要设置环境变量 下载地址 https://github.com/allure-framework/allure2/releases
3、验证是否安装成功
a) 在doc下运行 allure --version
b) 重启pycharm,在pycharm 中同样运行allure --version
4、生成报告
a) 生成临时的json报告,在pytest.ini中加入

addopts = -vs --alluredir=./temp --clean-alluredir

–alluredir=./temp 临时报告存放目录
–clean-alluredir 清空临时目录

b) 生成正式的报告
在run.py中加入

if __name__ == '__main__':
    pytest.main()
    time.sleep(3)
    os.system("allure generate ./temp -o ./reports --clean")

执行的时候要 选择该main文件运行才能生成报告
12、基于pytest的parametrize的数据驱动
@pytest.mark.parametrize(args_name,args_value)
args_name:参数名
args_value:参数值 (list, tuple, 字典列表,字典元祖),有多少个值用例就会执行多少次
[‘a’,‘b’,‘c’] (‘a’,‘b’,‘c’), [{‘a’:1},{b:1}], ({a:1},{b:1})
yaml 有两种数据类型
1、 键值对 value:key 读取之后是字典
2、 列表,通过-表示一组数值,读取之后是字典列表
-name1: 喂喂
-name2:欢欢
操作yaml文件需要安装:pip install pyyaml
导包的快捷操作 Alt+Enter
一个接口一个yaml文件
第一种用法:

class TestApi:
    @pytest.mark.parametrize("caseinfo",['黛玉','宝玉','宝钗'])
    def test_01_gettoken(self,caseinfo):
        print("获取统一鉴权码: "+caseinfo)

第二种用法:

 @pytest.mark.parametrize("arg1,arg2",[['黛玉',16],['宝钗',17]])
    def test_01_gettoken(self,arg1,arg2):
        print("获取统一鉴权码: "+str(arg1)+" "+str(arg2))

你可能感兴趣的:(测试,自动化,pytest,学习)