wordpress 建网站视频遵义网站网站建设
wordpress 建网站视频,遵义网站网站建设,python自动写wordpress,android应用开发详解领取资料#xff0c;咨询答疑#xff0c;请➕wei: June__Go
上一小节我们学习了pytest fixture的基本使用方法#xff0c;本小节我们讲解一下fixture的作用域。
fixture前后置区分
控制fixture的前置和后置操作是通过yield关键字进行来区分的#xff0c;代码在yield前面…领取资料咨询答疑请➕wei: June__Go
上一小节我们学习了pytest fixture的基本使用方法本小节我们讲解一下fixture的作用域。
fixture前后置区分
控制fixture的前置和后置操作是通过yield关键字进行来区分的代码在yield前面的属于前置操作代码在yield后面的属于后置操作。并且fixture也没有强烈的要求必须要前后置同时存在可以只存在前置也可以只存在后置。fixture如果有后置内容无论遇到什么问题都会进行执行后置的代码。
from selenium import webdriver
pytest.fixture()
def open_browser_init():# 前置chrome浏览器webdriver初始化driver webdriver.Chrome()driver.get(https://www.baidu.com)yield driver# 后置 chrome浏览器webdriver关闭driver.quit()
fixture作用域
Unittest框架中setup的作用是每条测试用例执行之前都会执行一次setupclass的作用是每个测试用例类执行之前都会执行一次。
pytest的fixture同样有这样的作用域且使用更广泛更灵活。
关键代码pytest.fixture(scope作用范围)参数如下
function默认作用域每个测试用例都运行一次class每个测试类只执行一次module每个模块只执行一次(模块:一个.py文件)package每个python包只执行一次session整个会话只执行一次即运行项目时整个过程只执行一次
fixture后面的括号不加任何参数就代表默认作用域与function作用一样。
1、function级别范围
每个测试用例之前运行一次
import pytestpytest.fixture()
def test_fixture():a helloprint(每个测试用例之前运行一次)yield a
def test_01(test_fixture):print(这是test_01)assert e in test_fixture
def test_02(test_fixture):print(这是test_02)assert h in test_fixture运行结果为 test session starts
collecting ... collected 2 itemstest_demo.py::test_01 每个测试用例之前运行一次
PASSED [ 50%]这是test_01test_demo.py::test_02 每个测试用例之前运行一次
PASSED [100%]这是test_02 2 passed in 0.01s
2、class级别范围
如果一个class里面有多个用例都调用了此fixture那么fixture只在此class里所有用例开始前执行一次。
import pytestpytest.fixture(scopeclass)
def test_fixture():a helloprint(每个类之前运行一次)yield apytest.mark.usefixtures(test_fixture)
class TestDemo:def test_demo01(self, test_fixture):assert 9 in test_fixturedef test_demo02(self, test_fixture):assert o in test_fixture运行结果 test session starts
collecting ... collected 2 itemstest_demo.py::TestDemo::test_demo01 每个类之前运行一次
FAILED [ 50%]
test_demo.py:20 (TestDemo.test_demo01)
9 ! helloExpected :hello
Actual :9
Click to see differenceself test_demo.TestDemo object at 0x10e0a60a0, test_fixture hellodef test_demo01(self, test_fixture):assert 9 in test_fixture
E AssertionError: assert 9 in hellotest_demo.py:22: AssertionErrortest_demo.py::TestDemo::test_demo02 PASSED [100%] 1 failed, 1 passed in 0.04s
3、module级别范围 在当前.py脚本里面所有用例开始前只执行一次。
import pytest
pytest.fixture(scopemodule)
def test_fixture():a helloprint(在当前文件下执行一次)yield a
def test_01(test_fixture):print(这是test_01)assert e in test_fixture
pytest.mark.usefixtures(test_fixture)
class TestDemo:def test_demo01(self,test_fixture):print(这是test_demo01)assert h in test_fixturedef test_demo02(self,test_fixture):print(这是test_demo02)assert o in test_fixture
运行结果 test session starts
collecting ... collected 3 itemstest_demo.py::test_01 在当前文件下执行一次
PASSED [ 33%]这是test_01test_demo.py::TestDemo::test_demo01
test_demo.py::TestDemo::test_demo02 3 passed in 0.01s 4、session级别范围
session级别是可以跨模块调用的多个模块下的用例只需调用一次fixture那就可以设置为scopesession并且写到conftest.py文件里。
conftest.py作用域放到项目的根目录下就可以全局调用了如果放到某个package下那就在改package内有效。
conftest.py的fixture调用方式无需导入直接使用。
文件目录 conftest.py
import pytestpytest.fixture(scopesession)
def test_fixture():a helloprint(这是session范围的作用域多个文件共享)yield a
test_demo1.py
import pytestdef test_01(test_fixture):print(这是test_01)assert e in test_fixturedef test_02(test_fixture):print(这是test_02)assert h in test_fixture
test_demo2.py
import pytestdef test_01(test_fixture):print(这是test_01)assert e in test_fixturepytest.mark.usefixtures(test_fixture)
class TestDemo:def test_demo01(self, test_fixture):print(这是test_demo01)assert h in test_fixturedef test_demo02(self, test_fixture):print(这是test_demo02)assert o in test_fixture
运行结果 test session starts
collecting ... collected 5 itemstest_demo1.py::test_01 这是session范围的作用域多个文件共享
PASSED [ 20%]这是test_01test_demo1.py::test_02 PASSED [ 40%]这是test_02test_demo2.py::test_01 PASSED [ 60%]这是test_01test_demo2.py::TestDemo::test_demo01 PASSED [ 80%]这是test_demo01test_demo2.py::TestDemo::test_demo02 PASSED [100%]这是test_demo02 5 passed in 0.01s 最后感谢每一个认真阅读我文章的人礼尚往来总是要有的虽然不是什么很值钱的东西如果你用得到的话可以直接拿走希望可以帮助到大家领取资料咨询答疑请➕wei: June__Go
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/90110.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!