pytest进阶(一)

pytest.ini配置文件

pytest.ini文件是Pytest的主配置文件,执行测试用例

pytest.ini文件要注意:

  1. 存放位置:一般放在项目的根目录(即当前项目的顶级文件夹下)。

  2. 编码格式:必须是ANSI编码格式,可以使用notpad++修改编码格式。

  3. pytest.ini文件中不能使用任何中文符号(也可以用,最好少用)。

  4. 配置了pytest.ini文件后,不管是主函数模式运行用例,还是命令行模式运行用例,都会去读取pytest.ini配置文件中的内容。

  5. CMD命令行中执行pytest -h命令,可以查看pytest.ini的设置选项。

基础的pytest.ini文件的编写 (用的时候删掉中文):

[pytest]
# 命令行的参数,用空格分隔
addopts = -s --alluredir report
# 测试用例的路径
testpaths = scripts
# 模块名的规则
python_files = test_parametrize.py
# 类名的规则
python_classes = Test*
# 方法名的规则
python_functions = test*

用例执行顺序pytest-ordering

Pytest默认从上到下顺序执行测试用例, 可以通过函数装饰器的⽅式,标记被测试函数来决定用例执⾏的顺序。需要使用pytest-ordering插件。安装⽅式:pip install pytest-ordering

使用方法:

  1. 标记于被测试函数之上,@pytest.mark.run(order=x)

  2. 根据order传⼊的参数来解决运⾏顺序。

  3. order值全为正数或全为负数时,运⾏顺序:值越⼩,优先级越⾼。

  4. 正数和负数同时存在:正数优先级⾼。

跳过测试用例skip/skipif

  • @pytest.mark.skip 标记在需要跳过的测试用例上 ,直接跳过用例

  • @pytest.mark.skipif(condition='xx',reason='xx') 符合条件则跳过

    • 参数condition:跳过的条件,为True则跳过测试,为False则继续执行测试,默认为True。

    • 参数reason:标注跳过的原因,必填参数。

失败重试pytest-rerunfailures

需要下载pytest-rerunfailures插件。安装方式:pip install pytest-rerunfailures

  • 方式一: 在命令行或者main()函数中使用

    • main() 函数: pytest.main(['-vs','test_a.py','--reruns=2'])

    • 命令行: pytest -vs ./test_a.py --reruns 2 --reruns-delay 2

      reruns为重跑次数,reruns_delay为间隔时间,单位s

  • 方式二: pytest.ini配置文件中使用(推荐)

pytest.ini配置文件中addopts添加reruns重试参数

[pytest]
addopts = -s --reruns 2 --reruns-delay 2
testpaths = scripts
python_files = test_01.py
python_classes = Test*
python_functions = test*

分组执行用例mark

pytest中的mark主要用于在测试用例/测试类中给用例打标记,实现测试分组功能,并能和其它插件配合设置测试方法执行顺序等。 使用步骤:

1、先在pytest.ini中注册标签

[pytest] # 固定的section名
markers= # 固定的option名称,注意缩进。
  标签名1: 标签名的说明内容。
  标签名2: 不写也可以
  标签名N

2、使用 @pytest.mark.标签名 标记测试用例/测试类

执行用例:pytest 测试套件名 -m 标签名

预期失败

期望测试用例是失败的,但是会运行此测试用例,并且也不会影响其他测试用例的的执行。(即xfail标记并不会影响用例的运行)

如果预期失败的测试用例执行失败的则结果是xfail(不会额外显示出错误信息),执行成功的则结果是xpass。在报告中会将这些用例列在“预期失败XFAIL或意外传递XPASS部分。

@pytest.mark.xfail(self,condition=None, reason=None, raises=None, run=True, strict=False)

  • condition: 如果满足条件则标记用例执行失败, 默认为True

    说明:condition表示预期结果,然后用例实际执行的结果,与预期结果对比,会出现4种测试结果状态。

    failed, passed, xfailed, xpassed

    提示:condition可以等于True或者False,也可以等于一个表达式,如:condition=1>2等。

  • reason:说明用例标记为预期失败的原因, 默认为None。(必填)

  • raises=None:在raises参数中指定单个异常或异常组,表明期望用例抛出这些异常。

    如果用例失败不是因为这些异常,那用例会被标记为failed

    如果测试用例失败的异常与raises参数标识的异常一致,则标记为xfailed

    一般很少这样用,会使用pytest.raises先抛出接口异常,再断言异常信息是否符合预期。

  • run:标识是否执行此用例, 若为True则执行,若为False则用例不执行用例,直接标记该用力为XFAIL,(防止在xfail死循环。)

    默认为True,执行此测试用例。

  • strictstrict默认为False。当strict=True时,如果测试用例被标识为xpass,则把该用例标识为失败fail

    我们也可以不修改strict属性值,在全局配置文件pytest.ini中添加一行配置:xfail_strict=true即可,作用是一样的。

忽略xfail标识

pytest --runxfail,也就是--runxfail参数可以将全部@pytest.mark.xfail()标识忽略掉。

断言

判断包含或不包含:

  • assert a in b:判断b包含a

  • assert a not in b:判断b不包含a

对类型的判断:

  • assert isinstance(a,int):判断a是否是int类型数据。

判断方法或者函数的返回值是否为真:

  • assert xx:判断xx结果为真。

  • assert not xx:判断xx结果不为真

errorMsg

  • assert xx,'error msg'

测试报告

pytest-html

安装:pip install pytest-html,使用方法:

  • main()函数:pytest.main(['--html=./report/report_01.html'])

  • 命令行:pytest test_pytest.py --html=./report/report.html

  • pytest.ini文件方式:addopts = -s --html=../report/report.html

Allure报告

1、下载Allure压缩包,解压并把bin目录配置到环境变量中,验证配置是否成功 allure --version

2、下载allure库:pip install allure-pytest

3、设置生成的Json格式临时报告的存放位置

配置pytest.ini文件,在pytest.ini全局配置文件中的addopts属性中配置:

addopts = -vs --alluredir ../report/temp_jsonreports

也可以在命令行或者main()函数中配置:pytest --alluredir report

4、生成Allure测试报告

将上面/report/temp_jsonreport文件夹中的json格式的测试报告转化为HTML格式的测试报告

执行命令:allure generate ./report/temp_jsonreport -o ./report/html --clean

  • allure generate: 固定命令。

  • ./report/temp_jsonreport:生成的Json格式的临时报告的路径。

  • -o:输出output。

  • ./report/html:生成的Allure报告的路径。

  • --clean:清空./report/html路径中原来的Allure测试报告。

或者在main()函数中执行

if __name__ == '__main__':
   pytest.main()
   os.system("allure generate ./report/temp_jsonreport -o ./report/html --clean")

 

原文地址:https://www.cnblogs.com/yjh1995/p/14753235.html