pytest基础-new
规范
1、首先创建 py 文件命名以 test_ 开始或者以 _test 结尾
2、若是新建类,测试类需要以 Test_开头
3、测试用例(方法)需要以 test_开头
assert 断言
assert xx:判断 xx 为真
assert not xx:判断 xx 不为真
assert a in b:判断 b 包含 a
assert a == b:判断 a 等于 b
assert a !=b:判断 a 不等于 b
from calculator import add, subtractdef test_add(): #passassert add(2, 3) == 5
def test_subtract(): #passassert subtract(5, 3) != 1def test_zhen():#failassert 0
def test_zhen2():#passassert 1def test_jia():#passassert not 0
def test_jia1():#failassert not 1def test_include():#passassert 1 in [1,2,3]
def test_include2():#failassert 4 in [1,2,3]
执行
命令行执行:
pytest .\test_calculator.py 指定执行某文件
pytest 文件夹下所有test_* 或者*_test的文件,全部执行
pytest test_tt.py::Test::test_case1 pytest路径/文件名::类名::方法名 执行指定文件指定方法
pytest test_tt.py::test_case1 pytest路径/文件名::方法名
ide执行:
if __name__ == '__main__':
- pytest.main(['test_tt.py']) 指定文件, 指定执行某文件
- pytest.mian() 没有指定文件,文件夹下所有test_* 或者*_test的文件,全部执行
- pytest.main(['test_tt.py::Test::test_case1']) 指定文件指定类指定方法
- pytest.main(['test_tt.py::test_case1']) 指定文件指定方法
命令行参数执行:
pytest -q 简化控制台的输出
pytest -q test_calculator.py
pytest -v 输出用例更加详细的执行信息,比如用例所在文件和用例名称
pytest -v test_calculator.py
pytest -k 执行用例中包含‘关键字’的用例 pytest -v -k "include" 方法名包含include的会被执行
pytest -k "include" test_calculator.py
pytest -s 输出用例中的调试信息,比如 print 打印信息,如果不加参数则不输出打印信息
pytest -s test_calculator.py
pytest -m 执行‘标记’的内容,执行特定的测试用例,执行有相同标记的测试用例
标记方法:方法前 @pytest.mark.run_these 打标run_these
执行 pytest -m run_these test_calculator.py test_calculator文件中的所有打标run_these的都会被执行
pytest -x执行失败则停止执行,后面的用例不会被执行
pytest -x test_calculator.py 执行出fail就会break
pytest --maxfail=n执行失败 n 次之后停止执行,n 是执行失败的次数
pytest --maxfail=3 test_calculator.py 执行,当fail3次的时候,break
pytest --count=n 执行用例 n 次,n=2 就是执行两次
pytest --count=2 test_calculator.py 执行2次
pytest --lf (last failed)重新运行上次失败的用例,若没有失败的会全部跑
pytest --lf test_calculator.py 将上次失败的again一遍,没有失败的就all again
pytest --ff (failed first)重新运行所有用例,但首先运行上次失败的用例
pytest --ff test_calculator.py 所有全部执行一遍,上次失败的优先执行
标记跳过执行
skipif(condition, reason=None) 参数:
condition:跳过的条件,必传参数reason:标注原因,必传参数
使用方法:
@pytest.mark.skipif(condition, reason="xxx") condition 条件为真时跳过
@pytest.mark.skip()
标记预期失败
xfail(condition=None, reason=None, raises=None, run=True, strict=False)
常用参数:
condition:预期失败的条件,必传参数reason:失败的原因,必传参数
使用方法:
@pytest.mark.xfail(condition, reason="xx")condition 为真则标记失败
在某种条件不满足的时候, 预期它是失败的, 就将它标记为预期失败, 若condition 条件不满足则正常执行
预期失败:
没有条件,失败就是xfailed
有条件,条件满足为真,失败就是xfailed
有条件,条件不满足为假,失败就是failed
参数化
方法:
parametrize(argnames, argvalues, indirect=False, ids=None, scope=None)
常用参数:
argnames:参数名
argvalues:参数对应值,类型必须为 list 当参数为一个时格式:[value]
当 参 数 个 数 大 于 一 个 时 , 格 式 为 : [(param_value1,param_value2.....),(param_value1,param_value2 )]
使用方法:
@pytest.mark.parametrize(argnames,argvalues) 参数名,参数值
@pytest.mark.parametrize("a",[3,6])单参数
@pytest.mark.parametrize("a,b",[(1,2),(0,3)])多参数
参数值为 N 个,测试方法就会运行 N 次
标记用例多次执行
首先安装 repeat: pip install pytest-repeat
@pytest.mark.repeat(n)执行当前用例 n 次 然后再往下执行其他用例
标记用例执行顺序
使用:
安 装 pip install pytest-ordering
@pytest.mark.run(order=1)---第1个执行
@pytest.mark.run(order=2)---第2个执行
@pytest.mark.last---最后执行
fixtrue
自定义测试用例预置条件--pytest 精髓fixture
@pytest.fixture()(scope="function",params=None,autouse=False, ids=None, name=None)
调用时被优先执行 预处理或者重复操作scope:被标记方法的作用域
function(default):作用于每个测试方法,每个 test 都运行一次
class:每个 class类执行开始时执行一次 @pytest.fixture()(scope="class",autouse=True)
module:每个 module 的所有 test开始前只执行一次 @pytest.fixture()(scope="module",autouse=True)
session:多个.py 文件的用例的时候, 如果多个用例只需调用一次fixture,那就可以设置为 scope="session"。
params:(list 类型)提供参数数据,供调用标记方法的函数使用
autouse:是否自动运行,默认为 False 不运行,设置为 True 自动运行
若不为 True 则需要调用才会优先执行。
@pytest.fixture()
定义函数,命名不要以 test 开头与用例区分开,fixture 有返回值, 没有返回值默认为 None。用例调用 fixture 返回值,直接就是把 fixture 的函数名称当做变量名称。
生成测试报告
想要生成测试报告,需要先安装 pytest-html
安装命令: pip install pytest-html
命令行生成:pytest --html==路径/文件名.html 执行用例文件.py
pytest --html==./report_sample.html test_sample.py html报告
pytest --junit-xml==./report_sample.xml test_sample.py xml报告
使用 PyCharm 生成报告
if name == "__main__": pytest.main('-s','-v','--html==./report_sample.html','test_samle.py')