学习15天:pytest
1、.pytest强大的插件
pytest-html(生成html格式的自动化测试报告)
pytest-xdist测试用例分布式执行。多CPU分发。
pytest-ordering 用于改变测试用例的执行顺序
pytest-rerunfailures用例失败后重跑
allure-pytest 用于生成美观的测试报告。
2、规则:
模块名必须以test_开头或者_test结尾
测试类必须Test开头
测试方法必须以test开头
3、运行
文件目录:
(1)主函数运行
- 运行所有的用例
if __name__ == '__main__':pytest.main()
- 指定模块运行
import pytest if __name__ == '__main__':pytest.main(['-vs','test_login.py']) #列表方式,注意大括号
-
指定文件夹执行
import pytest if __name__ == '__main__':pytest.main(['-vs','./interface_testcases'])
-
通过nodeid指定用例运行,nodeid由模块名、类名、函数名组成
import pytest
if __name__ == '__main__':#文件里面的函数pytest.main(['-vs','./interface_testcases/test_interface.py::test_04_func'])#类下面的方法#pytest.main(['-vs', './interface_testcases/test_interface.py::TestInterface::test_Interface_01'])
(2)命令行模式
- 运行所有:pytest
- 指定模块
- 指定目录
pytest -vs ./interface_testcases
- 指定到方法
pytes-vs ./interface_testcases/test_interface.py::TestInterface::test_Interface_01
参数详情:
-s:表示输出调试的信息,包括print打印信息
-v:打印详细信息,模块名.类名.类方法
pytest.main(['-s'])
两个参数可以合并使用
pytest.main(['-vs']) #test_login.py::TestLogin::test_login_01 这是我打印的print内容
-n :支持多线程或分布式运行测试用例,使用分布式需要先导入
pytest -vs ./testcases/test_login.py -n 2 #在命令行输入,2是代表线程数
pytest.main(['-vs','./testcases','-n=2']) #在pycharm输入格式,要安装pytest-xdist
-k: 根据测试用例的部分字符指定测试用例
pytest.main(['-v','-k test_1 or test_2','test_case3.py']) #在pycharm输入格式
pytest -vs .testcase -k "ao" #在命令行输入
4、改变测试用例的运行方式
安装pytest-ordering,在方法上面添加上装饰器
#按照order顺序执行case
@pytest.mark.run(order =3)
def test_login_01(self):print("我说这是第二个文件")
5、通过读取pytest.ini配置文件yunx
pytest.ini这个文件是pytest测试框架的核心配置文件,必须以这个命名
位置:一般放在根目录下
编码:必须是ANSI,可以使用notpad++修改编码格式,书写格式如下,举例说明
[pytest] #命令行执行参数,用空格分开 addopts = -vs#测试文件夹,可自己配置 testpaths = ./testcases
运行规则:不管是命令模式还是主函数模式运行,都会先去执行这个文件
5、初始化清除
模块、类、方法
def setup_module():print('\n *** 初始化-模块 ***')def teardown_module():print('\n *** 清除-模块 ***')class Test_错误密码:@classmethoddef setup_class(cls):print('\n === 初始化-类 ===')@classmethoddef teardown_class(cls):print('\n === 清除 - 类 ===')def setup_method(self):print('\n --- 初始化-方法 ---')def teardown_method(self):print('\n --- 清除 -方法 ---')def test_C001001(self):print('\n用例C001001')assert 1 == 1def test_C001002(self):print('\n用例C001002')assert 2 == 2def test_C001003(self):print('\n用例C001003')assert 3 == 2class Test_错误密码2:def test_C001021(self):print('\n用例C001021')assert 1 == 1def test_C001022(self):print('\n用例C001022')assert 2 == 2
目录级别
目标级别的 初始化清除,就是针对整个目录执行的初始化、清除。
我们在需要初始化的目录下面创建 一个名为 conftest.py
的文件,里面内容如下所示
import pytest @pytest.fixture(scope='package',autouse=True)
def st_emptyEnv():print(f'\n#### 初始化-目录甲')yieldprint(f'\n#### 清除-目录甲')
6、使用fixture装饰器实现部分用例的前后置
一个方法使用
方法范围使用,等同等同setup_method,teardown_method,设置autouse是所有自动使用
修改这个参数,就是对类范围生效,等同setup_class,teardown_class
固定写法,注意:params和param,函数中使用这个方法,会调用执行3次,并且可以使用这个参数
7、通过conftest.py和@python.fixture()结合使用实现“全局”的前置
1.conftest.py文件是单独存放的一个夹具配置文件,名称是不能更改
2.用处可以在不同的py文件中使用同一个fixture函数。
3.原则上contest.py需要和运行的用例放到统一层。并且不需要做任何的imprt导入的操作。
总结:
setup/teardown,setup_class/teardown class 它是作用于所有用例或者所有的类
@pytest.fixtrue()它的作用是既可以部分也可以全部前后置。
conftest.py和@pytest.fixtrue()结合使用,作用于全局的前后置,
8、数据驱动@pytest.mark.parametrize
功能类似于unittest的ddt数据驱动
class Test_错误登录:@pytest.mark.parametrize('username, password, expectedalert', [(None, '88888888', '请输入用户名'),('byhy', None, '请输入密码'),('byh', '88888888', '登录失败 : 用户名或者密码错误'),('byhy', '8888888', '登录失败 : 用户名或者密码错误'),('byhy', '888888888', '登录失败 : 用户名或者密码错误'),])def test_UI_0001_0005(self, username, password, expectedalert):alertText = loginAndCheck(username, password)assert alertText == expectedalert
9.YAML文件详情
1.用于全局的配置文件 ini或yaml
2.用于写测试用例(接口测试用例)
yaml简介:
yaml是一种数据格式,支持注释,换行,多行字符串,裸字符串(整形,字符串)
语法规则:
1.区分大小写
2.使用缩进表示层级,不能使用tab键缩进,只能用空格(和python一样)3.缩进没有数量的,只要前面是对其的就行。
4.注释是#
数据组成
1.Map对象,键值对键:(空格)值
多行形式:
[msxy]
name: 小名
age:18
一行的写法:
msxy:{name: 小名,age:18}
2、数组(list)
msxy:
-name:xiaoming
-age:18
一行的写法:
msxy: [{name:百里},{age:18}]
10.Allure测试报告
第一步:先要下载,下载后放到D盘,然后解压
第二步:配置Allure的环境变量,path增加
D:\allure-2.21.0\bin
验证环境:allure --version
第三步:生成临时的json报告,配置添加第一句,在temp文件下,生成临时报告
[pytest]
#配置常用运行参数
addopts = -vs --alluredir=./temps --clean-alluredir
#配置测试用例的位置
testpaths = ./testcases
#配置模块名的规则
python_files = test_*.py
#配置类的规则
python_classes = Test*
#配置测试用例(方法)的规则
python_functions = test_*
第四步:直接根据临时json报告生成Allure报告到reports文件下
if __name__ == '__main__':pytest.main()time.sleep(1)os.system("allure generate ./temps -o ./reports --clean")