allure之pytest
pytest运行设置文件
1、文件名:tox.ini或pytest.ini
[pytest]
# command
addopts = -s --alluredir report --clean-alluredir
# search path
testpaths = ./Scripts
# search file
python_files = test_login.py
# search classes
python_classes = Test*
# search function
python_functions = test*
安装pytest和html报告插件
1、安装pytest
pip3 install pytest -i "https://pypi.doubanio.com/simple/"
2、安装html
pip install pytest-html -i "https://pypi.doubanio.com/simple/"
3、使用方法
pytest --html=./report\abc.html
安装修改测试方法默认执行顺序插件
1、安装插件
pip install pytest-ordering -i "https://pypi.doubanio.com/simple/"
2、使用方法(装饰)
@pytest.mark.run(order=n) , n代表顺序
安装失败重试插件
1、安装插件
pip install pytest rerunfailures -i "https://pypi.doubanio.com/simple/"
2、使用方法
pytest --reruns n(n代表重试几次,一般为2),运行结果:R代表重试
安装allure测试报告
1、安装插件
pip install allure-pytest -i "https://pypi.doubanio.com/simple/"
2、使用方法
pytest --alluredir report (生成为json报告)
–下一步:把allure-2.13.0放在纯英文路径,配置bin目录环境变量
–下一步:执行转换命令:allure generate json报告文件夹 -o ./html文件夹 --clean
注意:
- json报告文件夹:由 --alluredir 生成的
- html文件夹: 自定义结果:会生成一个html文件夹,里面index.html就是报告。
pytest框架基本使用
setup:函数级别,置于class内。
setup_class:类级别,置于class内。
setup_module:模块级别,置于class外。
列表推导式-断言
1、举例
assert “普通” in [i.text for i in results]
工厂函数
1、定义工厂函数:@pytest.fixture(name=“”) 不写name默认引用函数名=工厂函数名
2、引用:def test_01(self,name1,name2) 或函数引用: @pytest.mark.usefuxtures(“name”)。
3、注:工厂函数优先级高于方法级别初始化方法,低于类级别初始化方法。
4、另:@pytest.fixture(autouse=True) #工厂函数不调用,自运行。
@pytest.fixture(scope=“function”, autouse=Ture) #每个测试方法之前运行一次
@pytest.fixture(scope=“class”, autouse=Ture) #整个类开始只 运行一次
跳过测试方法(跳过不会执行)
@pytest.mark.skipif(条件,reason=“原因”)
1、条件为真 + 有原因 = 跳过
2、条件为假 + 有原因 = 执行
3、无条件 + 有原因 = 执行
4、无条件 + 无原因 = 跳过
预期失败方法(还会执行)
@pytest.mark.xfail(条件,reason=“原因”) xpassed, xfailed
参数化
@pytest.mark.parametrize(argnames,argvalues)
# argnames:参数,单个/多个
# argvalues:参数值(必须为list)
# 当参数为一个时,参数值格式:[value1,value2,value3]
# 当参数为多个时,参数值格式:[(va1,va2),(va1,va2),(va1,va2)]
添加测试步骤
@allure.step("我是操作步骤1")
添加测试描述
allure.attach("我是附件的内容",“附件名字”)
添加严重级别
@allure.severity(Severity)
参数:Severity:严重级别(BLOCKER,CRITICAL,NORMAL,MINOR,TRIVIAL)
使用方式:@allure.severity(allure.severity_level.CRITICAL)
持续集成:
PATH=$PATH;C:\python36\Scripts
pytest