pytest常用插件介绍
1、pytest-html:html格式测试报告插件,terminal中直接执行pytest调用pytest.ini配置文件即可。格式如下
addopts = -s --htmp=report/report.html
2、pytest-ordering:调整执行顺序,在需要调整顺序的函数使用@pytest.nark.run(order=x)x可以是小数整数或者负数。0优先级最高,下来是正数从小到大;再下来是没有装饰的方法,再下来是负数从小到大的顺序
@pytest.mark.run(order=4) def test_01(): print("this is my pytest_case_01") assert 1 #断言成功 @pytest.mark.run(order=1) def test_02(): print("this is my pytest case_02") assert 0 #断言失败,用来判断用例是否通过或或失败
3、pytest-rerunfailures:测试失败重跑,重跑次数不包括原先的一次,重跑中任意一次成功即进行下一个用例测试,此重跑用例标记为pass。使用方法为在pytest.ini文件添加如下配置项
addopts = -s --htmp=report/report.html --rerun2=2表示测试中有失败重跑两次
4、skipif:skipif(condition,reason=None)跳过测试函数,根据特定条件不执行标识的测试函数.pytest.marl.skipif(conditions,reason="")当conditions是一个表达式,条件满足则跳过测试,原因就是reason;当测试条件为真则跳过测试
@pytest.mark.skipif(7>6, reason="this is a skipif test") def test_class_01_method_01(self): print("这是测试类的方法01") assert 1
5、xfail:标记为预期失败:xfail(condition,reason="") condition表达式为真表示预期失败否则表示预期成功,reason表示预期失败的原因。预期成功结果成功标记为pass,预期成功执行失败标记fail。预期失败结果成功标记为xpass 也统计在unexpect pass。预期失败结果失败结果标记为xfail 也统计在expect failures。xfail_strict=true配置文件加上此配置后预期状态与实际执行状态不一致都只会统计在fail,预期成功执行成功统计在pass,预期失败执行失败只标记在expect failure
import pytest class TestXfail(): #xpass @pytest.mark.xfail(1<2, reason="预期失败") def test_case_01(self): print("预期失败结果成功") assert 1 #xpass @pytest.mark.xfail(1 > 2, reason="预期成功") def test_case_02(self): print("预期成功结果成功") assert 1 #failed @pytest.mark.xfail(1 > 2, reason="预期成功") def test_case_03(self): print("预期成功结果失败") assert 0 #xfailed @pytest.mark.xfail(1 < 2, reason="预期失败") def test_case_04(self): print("预期失败结果失败") assert 0 #pass def test_case_05(self): print("预期成功") assert 1 #fail def test_case_06(self): print("预期失败") assert 0 if __name__ == '__main__': pytest.main(['-s', 'test_xfail.py'])
6、参数化:parametrize(argnames,argvalues) argvalues必须是list格式。一个参数时(参数名,[参数值1,参数值2……]);多个参数时((参数名1,参数名2 ……),[(参数1_1,参数2_1 ……),(参数1_2,参数2_2……),……]),参数值[]有多少个元素用例就执行多少次
import pytest class Testparametrize(): @pytest.mark.parametrize("my_key", [1, 2, 3]) def test_one_key(self, my_key): print("this is my key {mykey}".format(mykey=my_key)) @pytest.mark.parametrize(("my_key1", "my_key2"), [(1, 2), (3, 4), (5, 6)]) def test_two_key(self, my_key1, my_key2): print("this is my two key test key1 {key1} key2 {key2}".format(key1=my_key1, key2=my_key2))
7、简单举例说明fixture的几种使用场景,包括yield addfinalizer
#创建不带参数的fixture @pytest.fixture() def my_fix(): print("this is my test fixture") return 5 #使用fixture def test_fix(my_fix): print("正在使用fixture") tmp=my_fix print(tmp) def test_normal(): print("this is a normal test") """ this is my test fixture 正在使用fixture 5 .this is a normal test . """ #实现setup和teardown,使用yield实现程序互相调用;即使调用fixture的用例执行失败,yield后面的部分也会执行。 @pytest.fixture() def my_fix1(): print("开始使用fixture") yield print("fixture使用完毕") def test_fix1(my_fix1): print("正在使用fixture") """ 开始使用fixture 正在使用fixture .fixture使用完毕 """ #fixture addfinalizer,终结函数。 # yield当用例执行完毕后会执行后面的代码但是不能return,addfinalizer和yield功能一样并且可以return参数,传给后面的用例 @pytest.fixture() def open(request): #request是pytest的内置关键字,后面调用addfinalizer使用 tmp_list = [1, 2, 3] #创建一个内存对象 tmp_list1 = [11, 22, 23] print("创建临时内存对象,地址为", id(tmp_list) ) def end(): #对内存对象操作 tmp_list.append(0) print("临时列表追加0,内存地址为", id(tmp_list), "open值", tmp_list) request.addfinalizer(end) #相当于yied关键字和后面的teardown调用 return tmp_list1 #此处return那个变量后续调用open时候就引用那个对象 def test_addfinalizer(open): print("打印open初始值", open, "open id", id(open)) open.append(4) """ 创建临时内存对象,地址为 62265256 打印open初始值 [11, 22, 23] open id 62265128 res [11, 22, 23, 4] .临时列表追加0,内存地址为 62265256 open值 [1, 2, 3, 0] . """
浙公网安备 33010602011771号