脚本宝典收集整理的这篇文章主要介绍了Python测试框架pytest(05)fixture - error和failed、fixture实例化、多个fixture,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
1、在测试用例里面断言失败,结果为failed。
创建test_fixture_failed.py文件
脚本代码:
#!/usr/bin/env python # -*- coding: utf-8 -*- """ 微信公众号:AllTests软件测试 """ import pytest @pytest.fixture() def user(): print("用户名") name = "AllTests软件测试" return name def test_case(user): assert user == "软件测试" # 测试用例失败结果为failed if __name__ == "__main__": pytest.main(["-s", "test_fixture_failed.py"])
运行结果:
测试用例失败结果为failed。
2、在fixture里面断言失败,结果为error。
创建test_fixture_error.py文件
脚本代码:
#!/usr/bin/env python # -*- coding: utf-8 -*- """ 微信公众号:AllTests软件测试 """ import pytest @pytest.fixture() def user(): print("用户名") name = "AllTests软件测试" assert name == "软件测试" # fixture失败结果为error return name def test_case(user): assert user == "AllTests软件测试" if __name__ == "__main__": pytest.main(["-s", "test_fixture_error.py"])
运行结果:
fixture失败结果为error。
fixture 的 scope 实例化优先级:session > package > module > class > function。即较高 scope 范围的 fixture(session)在较低 scope 范围的 fixture( function 、 class )之前实例化。
具有相同作用域的 fixture 遵循测试函数中声明的顺序,并遵循 fixture 之间的依赖关系。在 fixture_A 里面依赖的 fixture_B 优先实例化,然后到 fixture_A 实例化。
自动使用(autouse=True)的 fixture 将在显式使用(传参或装饰器)的 fixture 之前实例化。
1、创建test_fixture2.py文件
脚本代码:
#!/usr/bin/env python # -*- coding: utf-8 -*- """ 微信公众号:AllTests软件测试 """ import pytest order = [] @pytest.fixture(scope="session") def s1(): order.append("s1") @pytest.fixture(scope="package") def p1(): order.append("p1") @pytest.fixture(scope="module") def m1(): order.append("m1") @pytest.fixture(scope="class") def c1(): order.append("c1") @pytest.fixture(scope="function") def f0(): order.append("f0") @pytest.fixture def f1(f3, a1): # 先实例化f3, 再实例化a1, 最后实例化f1 order.append("f1") assert f3 == 123 @pytest.fixture def f3(): order.append("f3") a = 123 yield a @pytest.fixture def a1(): order.append("a1") @pytest.fixture def f2(): order.append("f2") def test_order(f1, c1, m1, f0, f2, s1, p1): # 按scope的优先级,按顺序执行s1,p1,m1,c1,f1(优先执行f3,之后a1,最后f1),f0,f2 assert order == ["s1", "p1", "m1", "c1", "f3", "a1", "f1", "f0", "f2"]
2、运行结果:断言成功
按 scope 的优先级,按顺序执行 s1,p1,m1,c1,f1(优先执行f3,之后a1,最后f1),f0,f2
1、创建test_fixture_2.py文件
脚本代码:
#!/usr/bin/env python # -*- coding: utf-8 -*- """ 微信公众号:AllTests软件测试 """ import pytest @pytest.fixture() def fixture_one(): print("====fixture_one====") @pytest.fixture() def fixture_two(): print("====fixture_two====") def test_case(fixture_two, fixture_one): print("====执行用例====")
2、运行结果:
test_case函数执行前按顺序先执行fixture_two,之后执行fixture_one。
以上是脚本宝典为你收集整理的Python测试框架pytest(05)fixture - error和failed、fixture实例化、多个fixture全部内容,希望文章能够帮你解决Python测试框架pytest(05)fixture - error和failed、fixture实例化、多个fixture所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。