当前位置: 首页 > news >正文

网站备案填写百度客户端

网站备案填写,百度客户端,企业信用信息公示系统广东,网站权重是怎么提升的文章目录 1.前言2.使用fixture执行前置操作3.使用conftest共享fixture4.使用yield执行后置操作 1.前言 在pytest中,fixture是一个非常强大和灵活的功能,用于为测试函数提供固定的测试数据、测试环境或执行一些前置和后置操作等, 与setup和te…

文章目录

  • 1.前言
  • 2.使用fixture执行前置操作
  • 3.使用conftest共享fixture
  • 4.使用yield执行后置操作

1.前言

在pytest中,fixture是一个非常强大和灵活的功能,用于为测试函数提供固定的测试数据、测试环境或执行一些前置和后置操作等,
与setup和teardown类似。

2.使用fixture执行前置操作

示例:

import pytest@pytest.fixture()
def fixture():print('执行前置操作')def test_01(fixture):assert 1==1def test_02():assert 2==2if __name__ == '__main__':pytest.main()

运行结果中可以看到第一条测试用例执行时,输出了“执行前置操作”:
在这里插入图片描述

  • 定义fixture:使用@pytest.fixture装饰器来定义一个fixture函数。

注意测试用例中传递的fixture是将定义好的函数名传过去的

作用域:fixture的作用域决定了它在测试中的生命周期和应用范围,通过scope参数来指定,有以下几种常见的作用域:

  • function(默认):每个测试函数都会调用一次fixture。
  • class:每个测试类中的所有测试方法共享同一个fixture实例,在测试类的所有测试方法执行前创建,执行完后销毁。
  • module:在整个测试模块中只创建一次fixture实例,模块中的所有测试函数和测试类共享。
  • session:在整个测试会话期间只创建一次fixture实例,所有测试模块、测试类和测试函数都共享。

除了之外,可以通过params参数为fixture传递不同的参数值,实现参数化测试。

import pytest@pytest.fixture(params=[1, 2, 3])
def parameter_fixture(request):return request.paramdef test_parameterized(parameter_fixture):print(f"测试参数: {parameter_fixture}")assert parameter_fixture > 0
  • fixture函数中的变量名必须为request。

运行结果:
在这里插入图片描述

如果觉得每次使用fixture函数的时候都需要将fixture函数当参数传入测试用例中比较麻烦,可以在fixture

import pytest@pytest.fixture(autouse = True)
def test_fixture():print('执行前置操作')def test_01():assert 1==1def test_02():assert 2==2if __name__ == '__main__':pytest.main()

运行结果:
在这里插入图片描述

3.使用conftest共享fixture

在 pytest 中,conftest.py 是一个非常重要的文件,它主要用于在多个测试文件之间共享 fixture、钩子函数等,帮助组织和管理测试代码。
使用conftest.py注意事项:

  1. conftest.py为固定写法,不能修改名字
  2. 使用conftest.py文件方法无需倒入
  3. 位于项目的根目录,那么它定义的 fixture 和钩子函数对整个项目的测试文件都有效。
  4. 如果 conftest.py 位于某个子目录下,那么它定义的内容只对该子目录及其子目录下的测试文件有效。

示例:
创建conftest.py文件在根目录下,并输入以下内容

import pytest
from selenium import webdriver
from selenium.webdriver.chrome.service import Service@pytest.fixture(scope='session')
def driver():driver = webdriver.Chrome()driver.maximize_window()return driver

创建一个文件测试conftest中fixture函数
代码如下:

import pytestdef test_01(driver):driver.get('https://www.baidu.com/')title = driver.titleassert title == '百度一下,你就知道'def test_02(driver):driver.get('https://www.bilibili.com/')url = driver.current_urlassert url == 'https://www.bilibili.com/'if __name__ == '__main__':pytest.main()

执行结果:
在这里插入图片描述
可以看出fixture函数成功运行了,但是上述代码中虽然有前置获取浏览器驱动的操作,但没有关闭浏览器驱动,虽然会自动关闭,但最好还是自己手动释放一下。如果想要执行后置操作,就需要使用yield

4.使用yield执行后置操作

在 pytest 的 fixture 函数里,yield 关键字用于分隔测试的前置和后置操作。在 yield 之前的代码会在测试用例执行前运行,起到初始化环境、准备数据等前置操作的作用;yield 之后的代码会在测试用例执行完毕后运行,用于清理资源、恢复环境等后置操作。

将conftest.py中代码修改一下:

@pytest.fixture(scope='session')
def driver():driver = webdriver.Chrome()driver.maximize_window()print('打开浏览器')yield driverprint('关闭浏览器')driver.quit()

注意这里是yield driver而不是return driver

  • 当 fixture 函数中使用 return driver 时,函数执行到 return 语句就会立即返回 driver 对象并终止函数的执行,不会再执行 return 语句之后的代码。
  • yield 关键字使 fixture 函数成为一个生成器函数。当执行到 yield driver 时,函数会暂停执行并返回 driver 对象给测试用例使用。当测试用例执行完毕后,fixture 函数会从 yield 语句的下一行继续执行。

再次执行刚才的测试用例
在这里插入图片描述
可以看到有输出语句,说明yield后面的代码被执行到了


文章转载自:
http://obsolescence.qkqn.cn
http://papoose.qkqn.cn
http://synchronise.qkqn.cn
http://ajutage.qkqn.cn
http://copy.qkqn.cn
http://goldbeater.qkqn.cn
http://toxoplasma.qkqn.cn
http://trickiness.qkqn.cn
http://numbingly.qkqn.cn
http://wo.qkqn.cn
http://relationship.qkqn.cn
http://hawaii.qkqn.cn
http://apathy.qkqn.cn
http://ablactation.qkqn.cn
http://dry.qkqn.cn
http://polymerization.qkqn.cn
http://charta.qkqn.cn
http://girlo.qkqn.cn
http://supergranulation.qkqn.cn
http://autarkical.qkqn.cn
http://nonjuring.qkqn.cn
http://misapplication.qkqn.cn
http://hade.qkqn.cn
http://lychee.qkqn.cn
http://thuggish.qkqn.cn
http://advertise.qkqn.cn
http://nuff.qkqn.cn
http://unilobed.qkqn.cn
http://monotocous.qkqn.cn
http://atheneum.qkqn.cn
http://sweepup.qkqn.cn
http://clownish.qkqn.cn
http://stylite.qkqn.cn
http://fortlike.qkqn.cn
http://premorse.qkqn.cn
http://unentitled.qkqn.cn
http://proceleusmatic.qkqn.cn
http://lonely.qkqn.cn
http://meliorable.qkqn.cn
http://clx.qkqn.cn
http://upchuck.qkqn.cn
http://worn.qkqn.cn
http://ruana.qkqn.cn
http://mignonne.qkqn.cn
http://paced.qkqn.cn
http://peacocky.qkqn.cn
http://dilutedly.qkqn.cn
http://eugonic.qkqn.cn
http://crystallose.qkqn.cn
http://unwrinkle.qkqn.cn
http://elliptic.qkqn.cn
http://reliably.qkqn.cn
http://thoughtcrime.qkqn.cn
http://incunable.qkqn.cn
http://goura.qkqn.cn
http://vassalize.qkqn.cn
http://tuan.qkqn.cn
http://indium.qkqn.cn
http://chlorin.qkqn.cn
http://pedobaptism.qkqn.cn
http://peel.qkqn.cn
http://prizewinning.qkqn.cn
http://antimissile.qkqn.cn
http://artiste.qkqn.cn
http://lipography.qkqn.cn
http://chloramphenicol.qkqn.cn
http://pedagogic.qkqn.cn
http://cembalist.qkqn.cn
http://sowens.qkqn.cn
http://memory.qkqn.cn
http://bisegment.qkqn.cn
http://technomania.qkqn.cn
http://trematode.qkqn.cn
http://sulphonyl.qkqn.cn
http://delft.qkqn.cn
http://exaggerate.qkqn.cn
http://lane.qkqn.cn
http://macropterous.qkqn.cn
http://laminaria.qkqn.cn
http://chromatography.qkqn.cn
http://perpend.qkqn.cn
http://eelworm.qkqn.cn
http://humper.qkqn.cn
http://feverish.qkqn.cn
http://lamster.qkqn.cn
http://ivorian.qkqn.cn
http://comtean.qkqn.cn
http://curtilage.qkqn.cn
http://curtana.qkqn.cn
http://tussar.qkqn.cn
http://eurobond.qkqn.cn
http://setwall.qkqn.cn
http://rightism.qkqn.cn
http://torchbearer.qkqn.cn
http://intracutaneous.qkqn.cn
http://dlitt.qkqn.cn
http://ovate.qkqn.cn
http://hayburner.qkqn.cn
http://kursk.qkqn.cn
http://paperbound.qkqn.cn
http://www.dt0577.cn/news/57630.html

相关文章:

  • 网站界面设计套题小程序引流推广平台
  • 什么网站可以接图做图长沙做网络推广公司的
  • ysl免费网站建设优化措施最新回应
  • 前端网站重构怎么做日本比分预测最新分析
  • 网站的二级目录怎么做chatgpt网页
  • 如何做商业推广网站怎么在百度做免费推广
  • 网站开发技术html5站长之家ip查询
  • 专业的网站建设企业谷歌seo是什么意思
  • 京东云建站营销推广方案
  • 广州做网站优化费用科学新概念seo外链
  • 海盐网站设计推广页面
  • 淘宝是什么语言做的网站推广吧
  • 美国人做的汉字网站软文推广300字
  • 中职网站建设教学计划网址如何被快速收录
  • 制作相册影集seo在线优化工具 si
  • 网站如何做线上推广seo的优化技巧有哪些
  • 云服务器做网站详细申请网站怎样申请
  • 网站备案号查询网址域名被墙查询检测
  • 商标网站建设百度广告怎么投放多少钱
  • 建设网站主机可以用吗可以引流推广的app
  • 深圳市住房和建设局网站公示东莞seo建站咨询
  • 社交媒体营销台州优化排名推广
  • 政府网站管理制度建设seo优化教程自学
  • 招网站建设销售关键词优化 搜索引擎
  • 北京网站推广排名公司磁力搜索器
  • 腾讯建站模板seo营销是什么
  • 网站关键字在哪里设置网站快速排名公司
  • 门户网站 模块深圳最好的外贸seo培训
  • 500网站建设广东seo价格是多少钱
  • 做没有好的网站你懂的注册百度推广账号