首先祝你们国庆节日快乐,这个假期由于我老婆要考注会,我也跟着每天去图书馆学了几天,学习的感受仍是很是不错的,这是一篇总结。html
这篇博客准备讲解一下pytest测试框架,这个框架是当前最流行的python语言最流行的单测框架,不掌握可不行,首先这个框架属于第三方模块,须要经过pip安装便可python
pip install pytest
下面咱们进入正题框架
一、测试文件的名称必需要以test_*.py的格式,或者*_test.py的格式函数
二、测试类的名称必需要以Test开头,且这个类还不能有构造方法(__init__)单元测试
三、测试函数的名称必需要以test开头学习
pytest默认的就按照上面的三条规则来执行案例,固然咱们能够自定义运行规则,这个咱们后面在讲,这个不重要,看一个最简单的例子测试
import os import pytest # pytest是python的单元测试框架 def func(x): return x + 1 def test_a(): print("____test_a____") assert func(2) == 5 def test_b(): print("____test_b____") assert func(2) == 3 if __name__ == '__main__': pytest.main(["-s","pytest1.py"])
若是同窗们以前用过unittest测试框架,对测试固件这个这个名词就不会陌生了,若是不清楚,能够看下以前我写的unittest测试框架的博客(https://www.cnblogs.com/bainianminguo/p/11616526.html)ui
pytest框架的测试固件有两种,一种函数级别的,一种是类级别,执行的顺序以下插件
a、执行类的前置条件3d
b、执行函数的前置条件
c、执行函数的后置条件
d、执行类的后置条件
使用也很是简单,当时函数的命名必定要和我下面的备注保持彻底一致
# pytest的前置和后置条件 # 一、函数级别 setup teardown # 运行于测试方法的开始和结束 # 运行一个测试用例,会运行一次setup和teardown # 二、类级 setup_class teardown_class # 运行于测试类的开始和结束 # 一个测试类只运行一次setup_class teardown_class
import os import pytest def func(x): return x + 1 def test_a(): print("____test_a____") assert func(2) == 5 def test_b(): print("____test_b____") assert func(2) == 3 def setup(): print("函数级别的前置") def teardown(): print("函数级别的后置")
执行结果以下
class Testclass: def test_a(self): print("____test_a____") assert func(2) == 5 def test_b(self): print("____test_b____") assert func(2) == 3 def setup(self): print("函数级别的前置") def teardown(self): print("函数级别的后置") if __name__ == '__main__': pytest.main(["-s","pytest2.py"])
执行结果以下
import pytest def func(x): return x + 1 class Testclass: def test_a(self): print("____test_a____") assert func(2) == 5 def test_b(self): print("____test_b____") assert func(2) == 3 def setup(self): print("函数级别的前置") def teardown(self): print("函数级别的后置") def setup_class(self): print("类级别的前置") def teardown_class(self): print("类级别的后置") if __name__ == '__main__': pytest.main(["-s","pytest3.py"])
结果以下
咱们在博客的第一部分介绍了pytest框架的运行规则,这里咱们能够修改pytest的配置文件,改变框架运行规则
首先咱们要在案例的目录下建立一个pytest.ini的配置文件
内容以下
# 建立pytest.ini文件 # [pytest] # addopts=-s #这个先这样写,这个主要是执行参数 # testpaths = testcase # 只执行这个目录下的文件 # # python_files = test_*.py #执行的文件的名字 # python_classes = Test_* #执行类的名字 # python_functions = test_* # 执行函数的名字
配置文件截图
经过上面的步骤,咱们就能够改变pytest的运行规则
pytest的断言是用python的断言,他不像unittest框架,他本身实现了断言
# -*- coding:utf-8 -*- # pytest是使用python自带的断言 import pytest def func(x): return x + 1 def test_a(): print("____test_a____") assert func(2) == 5 def test_b(): print("____test_b____") assert not func(2) == 3 def test_c(): print("____test_b____") assert func(2) in ["a","b","c"] def test_d(): print("____test_b____") assert func(2) not in ["a","b","c"] if __name__ == '__main__': pytest.main(["-s","pytest5.py"])
一个函数能够打多个标记,一个标记同时能够给多个函数打标记。只须要让这个标记的装饰器函数装饰咱们的测试类或者测试函数
class Test_mark(): @pytest.mark.test01 def test_a(self): print("mark test a") @pytest.mark.test02 def test_b(self): print("mark test b") if __name__ == '__main__': pytest.main(['-s',"pytest6.py"])
还有其它的执行方式
# pytest -m test01 # pytest -n "test01 or test02" # pytest -m "not test01"
# -*- coding:utf-8 -*- import pytest # skip跳过执行某个案例 @pytest.mark.skip(reson="只是这个函数用例不执行") def test_a(): print("testa") def test_b(): print("testb") @pytest.mark.skip(reson="整个类下的案例都不会执行") class Test_skip(): def test_a(self): print("testa") def test_b(self): print("testb") # 能够根据条件判断,为真,则不执行 @pytest.mark.skipif(1 > 2,reson="整个类下的案例知足条件都不会执行") class Test_skipif(): def test_a(self): print("testa") def test_b(self): print("testb")
# pytest的数据参数化 # 一、传入单个参数 # # pytest.mark.parametrize(argnames,argvalues) # argnames 参数的名称 # # argvalues 参数对应的值,类型必须是可迭代的类型,通常使用list @pytest.mark.skip(reson="只是这个函数用例不执行") def test_a(): print("testa") @pytest.mark.parametrize("name",["cui1","cui2","cui3","cui4"]) def test_b(name): print("testb----->{name}".format(name = name)) if __name__ == '__main__': pytest.main(["-s", "pytest8.py"])
实现的效果name做为参数的名称,这个案例会执行4次,参数分别是name=“cui1”\name="cui2"\....
import pytest # pytest的数据参数化 # 一、传入多个参数 # # pytest.mark.parametrize((argnames1,argnames2),[(argvalues1,argvalues1),(argvalues1,argvalues1)],(argvalues1,argvalues1)]]) @pytest.mark.skip(reson="只是这个函数用例不执行") def test_a(): print("testa") @pytest.mark.parametrize(("name","age"),[("cui1",12),("cui2",13),("cui3",14)]) def test_b(name,age): print("testb----->{name}----->{age}".format(name = name,age = age)) if __name__ == '__main__': pytest.main(["-s", "pytest9.py"])
实现的效果以下
# pip install pytest-html # 用来美化输出报告的插件 # 只须要在配置文件中加这个配置便可 # # addopts=-s --html=report.html
效果
# pip install pytest-rerunfailures # 失败重试的第三方插件 # 只须要在配置文件中加这个配置即 # --reruns 3 --reruns-delay 2
至此,pytest的框架基本使用已经讲解清楚,小伙伴们还有不清楚的吗?欢迎你们来沟通!!!