目录
1、安装及快速入门
2、pytest的前后置
3、运行规则
4、断言
5、标记mark的使用
安装命令:pip3 install pytest
创建第一个测试用例
import pytest
def func(x):
return x+1
def test001():
print("-----test001用例-----")
assert func(3) == 6 # 断言失败
def test002():
print("-----test002用例-----")
assert func(5) == 6 #断言成功
if __name__ == '__main__':
pytest.main(["-vs", "demo_pytest_testcase.py"])
函数级别:setup、teardown
用于测试方法的始末
运行一次测试用例会运行一次setup、teardown
import pytest
def test001():
print("test001用例_类外")
def test002():
print("test002用例_类外")
def setup():
print("用例执行开始前执行")
def teardown():
print("用例执行后执行")
if __name__ == '__main__':
pytest.main(["-vs", "demo_pytest_setup_and_teardown_func_level.py"])
类级别:setup_class、teardown_class
运行于测试类的始末
类中方法级别:setup_method、teardowm_class
模块级别(需放置类外):setup_module、teardown_module
所有模块的始末
import pytest
def setup_module(self):
print("模块开始前执行")
def teardown_module(self):
print("模块结束后运行")
class Testclass():
def setup_class(self):
print("类开始前运行")
def teardown_class(self):
print("类结束后运行")
def setup_method(self):
print("类中方法开始前执行")
def teardown_method(self):
print("类中方法结束后执行")
def test001(self):
print("test001用例-类中")
def test002(self):
print("test002用例-类中")
def test003(self):
print("test003用例-类中")
if __name__ == '__main__':
pytest.main(["-vs", "demo_pytest_setup_and_teardown_class_module_method.py"])
默认运行规则
1.测试文件以test_.py开头或以.test.py结尾
2.测试类以Test开头,并且不能带有__init__方法
3.测试函数以test_开头
自定义运行规则
文件名:pytest.ini(不可更改)
作用域:当前目录和其子目录下所有case
注意:pytest_ini # ini文件不能有中文,可以用notepad++修改编码utf-8为ansi
[pytest]
addopts = -vs
testpaths = ./testcases
python_files = test_*.py demo_*
python_calsses = Test_* Test*
python_functions = test_* test*
判断为真:assert XX
判断不为真:assert not XX
判断a包含b:assert a in b
判断a等于b:assert a==b
判断a不等于b:assert a!=b
一个测试函数可以有多个标记
一个mark可以标记多个测试函数
运行参数pytest -m 标记名
运行多个参数pytest -m "标记名1 or 标记名2"
pytest -m ds001
pytest -m "ds002 or ds003"
pytest -m ds003
pytest -m "ds001"
import pytest
class Test_mark():
@pytest.mark.ds001
@pytest.mark.ds002
def test_a(self):
pass
@pytest.mark.ds001
def test_b(self):
pass
@pytest.mark.ds003
def test_c(self):
pass
跳过测试
1.skip # 标记skip表示跳过该测试用例运行不执行
skip(reason=None)
2.skipif
条件判断验证是否忽略不执行
表达式:skipif(condition,reason=None)
skip在方法上
import pytest
class Test_skip():
@pytest.mark.skip
def test_a(self):
pass
def test_b(self):
pass
if __name__ == '__main__':
pytest.main(["-vs", "demo_pytest_skip.py"])
在类上
import pytest
@pytest.mark.skip
class Test_skip():
def test_a(self):
pass
def test_b(self):
pass
if __name__ == '__main__':
pytest.main(["-vs", "demo_pytest_skip.py"])
skipif
import pytest
age = 17
class Test_skip():
@pytest.mark.skipif(age<18, reason="年龄小于18不运行!")
def test_a(self):
pass
def test_b(self):
pass
if __name__ == '__main__':
pytest.main(["-vs", "demo_pytest_skipif.py"])
pytest参数化
argnames:参数名
argvalues:参数对应值,类型必须为可迭代类型,一般为list
import pytest
class Test_parametrize(object):
@pytest.mark.parametrize("name", ["小明", "小红"])
def test_a(self, name):
print("名字是:%s" % name)
if __name__ == "__main__":
pytest.main(["-vs", "demo_pytest_parametrize_one_parameter.py"])
传入多个参数:list的每个元素都是一个元组,元组里的元素和参数里的顺序一一对应
import pytest
class Test_parametrize(object):
@pytest.mark.parametrize("username, password", [("小明", 18), ("小红", 17)])
def test_a(self, username, password):
print("名字是:%s, 密码是:%s" % (username, password))
if __name__ == "__main__":
pytest.main(["-vs", "demo_pytest_parametrize_many_parameter.py"])
pytest常用插件
1.测试报告
安装:pip install pytest-html
使用:在配置文件中的命令行参数添加--html=用户路径/report.html
# 失败使用下面
pip install pytest-html -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
2.失败重试
安装:pip install pytest-rerunfailres
# 失败用下面,偶尔还是会失败,多执行几次
pip3 install pytest-rerunfailures -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
使用:
1.在配置文件命令行参数书中添加-rerun n
2.出错重试的等待时间,reruns-delay
[pytest]
addopts = -vs --html=../report/report.html --reruns 2 --reruns-delay 3
testpaths = ./testcases
python_files = test_*.py demo_*
python_calsses = Test_* Test*
python_functions = test_* test*
用例:
import pytest
class Test_rerun_fail(object):
def test_a(self):
assert False
def test_b(self):
pass
if __name__ == '__main__':
pytest.main(["-vs", "test_rerun.py"])