pytest基本使用
setup/teardown函数
函数级别:
import pytest
class Test002:
def setup(self):
print("---------start")
pass
def test_001(self):
print("----------test001")
def test_002(self):
print("----------test002")
def teardown(self):
print("----------stop")
pass
if __name__ == '__main__':
pytest.main(['-s', 'test002.py'])
结果:
---------start
----------test001
.----------stop
---------start
----------test002
.----------stop
每执行一个函数就会执行一次setup和teardown
类级别:
import pytest
class Test002:
def setup_class(self):
print("---------start")
pass
def test_001(self):
print("----------test001")
def test_002(self):
print("----------test002")
def teardown_class(self):
print("----------stop")
pass
if __name__ == '__main__':
pytest.main(['-s', 'test002.py'])
结果:
---------start
----------test001
.----------test002
.----------stop
只执行一次setup和teardown
配置文件
主配置文件:pytest.ini
格式为:
[pytest]
addopts = -s --html=./report/report.html --reruns 3
testpaths = ./scripts_test
python_files = test_*.py
python_classes = Test_*
python_functions = test_*
addopts = :命令行参数,使用命令行执行文件时自动添加
testpaths =:测试路径
python_files =:搜索文件名
python_classes =:搜索测试类名
python_functions =:搜索测试方法名
addopts = -rsxX
–rsxX 表示pytest报告所有测试用例被跳过、预计失败、预计失败但实际被通过的原因
使用pytest --help指令可以查看pytest.ini的设置选项
[pytest] ini-options in the first pytest.ini|tox.ini|setup.cfg file found:
markers (linelist) markers for test functions
empty_parameter_set_mark (string) default marker for empty parametersets
norecursedirs (args) directory patterns to avoid for recursion
testpaths (args) directories to search for tests when no files or dire
console_output_style (string) console output: classic or with additional progr
usefixtures (args) list of default fixtures to be used with this project
python_files (args) glob-style file patterns for Python test module disco
python_classes (args) prefixes or glob names for Python test class discover
python_functions (args) prefixes or glob names for Python test function and m
xfail_strict (bool) default for the strict parameter of
addopts (args) extra command line options
minversion (string) minimally required pytest version
此处运行时遇到报错:UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0xb0 in position 26: illegal multibyte sequence
解决:将pytest.ini文件编码格式改为GBK,且文件中不能有中文,否则编译不通过
其他配置文件:
conftest.py :测试用例的一些fixture配置
init.py :识别该文件夹为python的package包
tox.ini :与pytest.ini类似,用tox工具时候才有用
setup.cfg :也是ini格式文件,影响setup.py的行为
插件
生成测试报告:pytest-html
安装该插件后,直接在pytest.ini文件中addopts = 添加参数“- -html=./report/report.html”
失败重试:pytest-rerunfailures
安装该插件后,直接在pytest.ini文件中addopts = 添加参数–reruns n (n:为重试的次数)
控制被测试函数执行的顺序:pytest-ordering
安装该插件后,通过修饰符写在函数上一行@pytest.mark.run(order=3)
使用方法:
import pytest
@pytest.mark.run(order=3)
def test_01():
print("--------test01")
assert 1
@pytest.mark.run(order=-1)
def test02():
print("---------test02")
assert 0
@pytest.mark.run(order=1)
def test_03():
print("----------test03")
@pytest.mark.run(order=2)
def test04():
print("------------test04")
if __name__ == '__main__':
pytest.main(['-s', 'test001.py'])
1、标记于被测试函数, @pytest.mark.run(order=x)
2、根据order传入的参数来解决运行顺序
3、order值全为正数或负数时,值越小优先级越高
4、正负数同时存在时,正数优先极高
5、已标记和未标记的函数,已标记的函数优先极高