700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Python测试框架pytest(05)fixture - error和failed fixture实例化 多个fixture

Python测试框架pytest(05)fixture - error和failed fixture实例化 多个fixture

时间:2018-08-16 19:25:04

相关推荐

Python测试框架pytest(05)fixture - error和failed fixture实例化 多个fixture

Python测试框架pytest系列可以查看下列

Python测试框架pytest(01)简介、安装、快速入门_编程简单学的博客-CSDN博客

Python测试框架pytest(02)PyCharm设置运行pytest、pytest.main()_编程简单学的博客-CSDN博客

软件测试资料领取方式 (#1) · Issue · 可可爱爱的程序员 / 软件测试资料合集 · GIT CODE

————————————————

1、在测试用例里面断言失败,结果为failed。

创建test_fixture_failed.py文件

脚本代码:

#!/usr/bin/env python# -*- coding: utf-8 -*-"""微信公众号:AllTests软件测试"""import pytest@pytest.fixture()def user():print("用户名")name = "AllTests软件测试"return namedef test_case(user):assert user == "软件测试" # 测试用例失败结果为failedif __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失败结果为errorreturn namedef test_case(user):assert user == "AllTests软件测试"if __name__ == "__main__":pytest.main(["-s", "test_fixture_error.py"])复制代码

运行结果:

fixture失败结果为error。

2、fixture的实例化顺序

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 pytestorder = []@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.fixturedef f1(f3, a1):# 先实例化f3, 再实例化a1, 最后实例化f1order.append("f1")assert f3 == 123@pytest.fixturedef f3():order.append("f3")a = 123yield a@pytest.fixturedef a1():order.append("a1")@pytest.fixturedef f2():order.append("f2")def test_order(f1, c1, m1, f0, f2, s1, p1):# 按scope的优先级,按顺序执行s1,p1,m1,c1,f1(优先执行f3,之后a1,最后f1),f0,f2assert order == ["s1", "p1", "m1", "c1", "f3", "a1", "f1", "f0", "f2"]复制代码

2、运行结果:断言成功

按 scope 的优先级,按顺序执行 s1,p1,m1,c1,f1(优先执行f3,之后a1,最后f1),f0,f2

3、使用多个fixture

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。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。