> 文章列表 > pytest基础用法

pytest基础用法

pytest基础用法

项目中需要,只记录目前需要的功能,后面会陆续补充

安装 pip install pytest

使用:
1。配置文件

[pytest]

addopts = -s
testpaths = ./test_case
python_files = test_.py
python_class = Test

python_functions = test_*

这其实是默认配置。

  1. @pytest.mark.skip(reason=“”) 跳过,可加reason
    @pytest.mark.skipif() 判断条件跳过
    @pytest.mark.xfail(raise=ZeroDivisionError) 标记预期失败

  2. @pytest.mark.parametrize([‘a’,‘b’],[[],[],[],[],[]]) 前面形参,后面实参

  3. 夹具:
    模块级别
    setup_module
    teardown_module

    类级别:
    setup_class
    teardown_class

    实例级别:
    setup_function
    teardown_function

    方法级别:
    setup_method
    teardown_method

    默认为方法级别:
    setup
    teardown

  4. 失败重跑
    需要装插件:
    pip install pytest-rerunfailures
    运行方式:
    命令行:pytest --reruns 5
    配置文件方式:
    addopts = -s --reruns 5

  5. 失败截图

  6. allure结合使用
    windows环境:
    pip install allure-pytest
    allure包下载:https://github.com/allure-framework/allure2/releases
    解压 -> 进入bin目录 -> 运行allure.bat,
    把bin目录加入PATH环境变量
    cmd+r 运行一下allure --version看下有没有成功

    centos环境: