Pytest框架入门
基本使用
示例代码
# 1.导包
import pytest
# 2.编写测试方法
def fun(x):
return x+1
# 执行测试用例
def test_a():
print("---test a---")
assert fun(3) == 4 # 断言 (成功)
def test_b():
print("--test b---")
assert fun(3) == 5 # 断言 (失败)
# 执行
if __name__ == '__main__':
pytest.main(["-s","pytest_01.py"]) # "-s" :显示代码中print的打印,
命令行运行
pytest -s pytest_01.py
用例前置和后置执行
-
函数级别 : setup/teardown
- 运行于测试的始末
- 运行一次测试用例就会运行一次setup和teardown
-
类级别 : set_class/teardown_class
- 运行于测试类的始末
- 一个测试内只运行一次setup_class和teardown_class
import pytest # 类之外 # 1. 编写测试用例test def test_a(): print("---test a---") def test_b(): print("---test b---") # 2. 测试用例之前要运行setup() def setup(): print("---setup---") # 3. 测试用例之后要运行teardown() def teardown(): print("---teardown---") if __name__ == '__main__': pytest.main(['-s', 'test_method.py']) # 结果 test_method.py ---setup--- ---test a--- .---teardown--- ---setup--- ---test b--- .---teardown--- -------------------------------------------------- import pytest # 类之内 class TestMethod: def test_a(self): print("---test a---") def test_b(self): print("---test b---") # 2. 测试用例之前要运行setup() def setup(self): print("---setup---") # 3. 测试用例之后要运行teardown() def teardown(self): print("---teardown---") def setup_class(self): print("---setup_class") def teardown_class(self): print("---teardown_class") if __name__ == '__main__': pytest.main(['-s', 'test_method.py']) ------------------------------------------ # 结果 test_method.py ---setup_class ---setup--- ---test a--- .---teardown--- ---setup--- ---test b--- .---teardown--- ---teardown_class
pytest默认运行规则
- 测试文件以
test_*.py
开头或*_test.py
结尾pytest
会执行当前目录及子目录下所有test_*.py
及*_test.py
格式的文件
- 测试类以
Test
开头,且不能带有__init__
方法 - 测试函数以
test_
开头 - 命令行下,执行代码
pytest -v
会执行当前目录下所有符合规则的文件
自定义运行规则
文件名pytest.ini
创建在当前目录下,值对当前目录下的所有文件及子目录起作用
[pytest]
addopts = -s # -v 不打印print
# 当前目录下的script文件夹 - 可自定义
testpaths = testcase
# 当前目录下的script文件夹下,以test_开头,以.py结尾的所有文件 - 可自定义
python_files = test_*.py
# 当前目录下的script文件夹下,以test_开头,以.py结尾的所有文件中,以Test_开头的类 - 可自定义
python_classes = Test_*
# 当前目录下的script文件夹下,以test_开头,以.py结尾的所有文件中,以Test_开头的类内,以test_开头的方法
python_funcitions = test_*
断言
- 判断xx为真
assert xx
- 判断xx不为真
assert not xx
- 判断吧包含a
assert a in b
- 判断a等于b
assert a == b
- 判断a不等于b
assert a != b
标记mark使用
pytest提供了标记机制,允许使用mark对测试函数做标记
@pytest.mark.标记名
def test_a(self):
pass
1. 一个测试函数可以有多核标记
2. 一个mark可以标记多个测试函数
3. 运行参数pytest -m 标记名
4. 运行多个参数 pytest -m "标记名1 or 标记名2"
5. 不运行某个被标记的函数 pytest -m "not 标记名"
跳过测试
- skip
- 标记skip表示跳过该测试用例,运行不执行
- skip(reason=None)
-
skipif
- 条件判断验证是否忽略不执行
- 格式 :
skipif(condition,reason=None)
import pytest # 类之内 class Test_Method: # @pytest.mark.skip # @pytest.mark.skip(reason="不运行这个") @pytest.mark.skipif(2<1,reason="如果2小于1就不运行") def test_a(self): print("---test a---") def test_b(self): print("---test b---") if __name__ == '__main__': pytest.main(['-s', 'test_method.py']) # 也可以放在类的上边,那样类里面所有的测试用例都不会执行
pytest数据参数化
1. 传入单个参数
@pytest.mark.parametrize(argnames,argvalues)
- argnames : 参数名
- argvalues : 参数对应值,类型必须是可迭代的,一般使用list
import pytest
# 类之内
class Test_Method:
@pytest.mark.parametrize("name",["小明","小红","小强"])
def test_a(self,name):
print("---test a---")
print(name)
def test_b(self):
print("---test b---")
if __name__ == '__main__':
pytest.main(['-s', 'test_method.py'])
2. 传入多个参数
@pytest.mark.parametrize(("username","password"),[("xiaoming","123"),("xiaoqiang","456")])
-
list的每个元素都是一个元组,元组的每个元素按照参数顺序一一对应
import pytest # 类之内 class Test_Method: @pytest.mark.parametrize("name",["小明","小红","小强"]) def test_a(self,name): print("---test a---") print(name) @pytest.mark.parametrize(("username", "password"), [("xiaoming", "123"), ("xiaoqiang", "456")]) def test_b(self,username,password): print("---test b---") print(username) print(password) if __name__ == '__main__': pytest.main(['-s', 'test_method.py'])
常用插件
测试报告插件
-
应用场景 : 自动化测试焦恩最终执行时通过还是不通过,需要通过测试报告体现出来
-
安装脚本 :
pip install pytest-html
-
使用 :
-
在配置文件
pytest.ini
中命令行参数addopts后添加代码 :--html=用户路径/.report.html
[pytest] addopts = -v --html=report.html testpaths = testcase python_files = test_*.py python_classes = Test_* python_funcitions = test_*
-
失败重试插件
-
应用场景 :当失败后尝试再次运行
-
安装 :
pip install pytest-rerunfailures
-
配置 : 在配置文件中的命令行参数addopts后增加
--reruns n
。n表示重复运行次数 -
如果期望加上出错重试的等待时间,addopts后增加
--reruns-delay s
。s表示等待时间,单位秒[pytest] addopts = -v --html=report.html --reruns 2 --reruns-delay 1 testpaths = testcase python_files = test_*.py python_classes = Test_* python_funcitions = test_*