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

电商网站 网站服务内容太原百度快速排名提升

电商网站 网站服务内容,太原百度快速排名提升,免费b站推广网站app,系统优化加速工具导读 fixture 是 pytest 中一个非常重要的模块,可以让代码更加简洁。 fixture 的 autouse 为 True 可以自动化加载 fixture。 如果不想每条用例执行前都运行初始化方法(可能多个fixture)怎么办?可不可以只运行一次初始化方法? 答&#xf…

导读

fixture 是 pytest 中一个非常重要的模块,可以让代码更加简洁。

fixture 的 autouse 为 True 可以自动化加载 fixture

如果不想每条用例执行前都运行初始化方法(可能多个fixture)怎么办?可不可以只运行一次初始化方法?

答:可以使用 scope 。

举例

要先创建一个文件:conftest.py, 编写代码:

import pytestdata = {}@pytest.fixture(scope="session")
def add():"""相加"""return 5 + 8@pytest.fixture(scope="session")
def multiply():"""相乘"""return 5 * 8@pytest.fixture(scope="session", autouse=True)
def init_data():"""初始化数据"""print(f"start init_data: {data}")data["a"] = 5data["b"] = 8data["c"] = 13data["d"] = 40data["e"] = 53data["f"] = 520@pytest.fixture(scope="session", autouse=True)
def init_data2(add, multiply):"""初始化数据"""print(f"start init_data2: {data}")data["e"] = add * multiplydata["f"] = add + multiply

这里注意:init_data2 引用了 add 和 multiply 这两个fixture,所以这两个 fixture 都需要加上:

@pytest.fixture(scope="session")

如果不加上,就会报错:ScopeMismatch: You tried to access the function scoped fixture add with a session scoped request object

这是因为 init_data2 的作用域是 session,而  add 和 multiply 的作用域是 function。所以要在同级上才能使用。

新建测试文件:test_fixtures_scope.py, 编写测试代码:

from conftest import datadef test_first_fixture():# Actprint(f"a={data['a']}")print(f"b={data['b']}")print(f"a + b={data['c']}")print(f"a * b={data['d']}")# Assertassert data['c'] == data['a'] + data['b']def test_second_fixture():print(f"add * multiply={data['e']}")print(f"add + multiply={data['f']}")# Assertassert data['e'] != data['f']

测试

正确使用:

$ pytest -sv test_fixtures_scope.py
================================== test session starts ===================================
platform darwin -- Python 3.12.0, pytest-8.2.2, pluggy-1.5.0 -- /Users/hope/PythonVirtualenv/pytest-sample/bin/python
cachedir: .pytest_cache
rootdir: /Users/hope/PycharmProjects/pytest-sample
collected 2 itemstest_fixtures_scope.py::test_first_fixture start init_data: {}
start init_data2: {'a': 5, 'b': 8, 'c': 13, 'd': 40, 'e': 53, 'f': 520}
a=5
b=8
a + b=13
a * b=40
PASSED
test_fixtures_scope.py::test_second_fixture add * multiply=520
add + multiply=53
PASSED=================================== 2 passed in 0.01s ====================================

可以看到 这行日志只运行了一次:

start init_data: {}
start init_data2: {'a': 5, 'b': 8, 'c': 13, 'd': 40, 'e': 53, 'f': 520}

当两个fixture不在同一作用域:

$ pytest -sv test_fixtures_scope.py
================================== test session starts ===================================
platform darwin -- Python 3.12.0, pytest-8.2.2, pluggy-1.5.0 -- /Users/hope/PythonVirtualenv/pytest-sample/bin/python
cachedir: .pytest_cache
rootdir: /Users/hope/PycharmProjects/pytest-sample
collected 2 itemstest_fixtures_scope.py::test_first_fixture ERROR
test_fixtures_scope.py::test_second_fixture ERROR========================================= ERRORS =========================================
__________________________ ERROR at setup of test_first_fixture __________________________
ScopeMismatch: You tried to access the function scoped fixture add with a session scoped request object. Requesting fixture stack:
conftest.py:18:  def init_data(add, multiply)
Requested fixture:
conftest.py:6:  def add()
_________________________ ERROR at setup of test_second_fixture __________________________
ScopeMismatch: You tried to access the function scoped fixture add with a session scoped request object. Requesting fixture stack:
conftest.py:18:  def init_data(add, multiply)
Requested fixture:
conftest.py:6:  def add()
================================ short test summary info =================================
ERROR test_fixtures_scope.py::test_first_fixture - Failed: ScopeMismatch: You tried to access the function scoped fixture add with a ses...
ERROR test_fixtures_scope.py::test_second_fixture - Failed: ScopeMismatch: You tried to access the function scoped fixture add with a ses...
=================================== 2 errors in 0.02s ====================================

总结

fixtures 会在测试一开始的时候创建,会在定义的作用域销毁。

  • function: 默认的 scope,在测试用例(以test开头的方法)结束时销毁。

  • class: 在一个class类中最后一个测试用例结束时销毁。

  • module: 在一个模块(也就是一个py文件)中最后一个测试用例结束时销毁。

  • package: 在一个目录的最后一个测试用例结束时销毁。

  • session:所有测试用例都运行结束时销毁。


每日踩一坑,生活更轻松。

本期分享就到这里啦,祝君在测开之路上越走越顺,越走越远。

最后感谢每一个认真阅读我文章的人,看着粉丝一路的上涨和关注,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走! 

软件测试面试文档

我们学习必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有字节大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。

http://www.hkea.cn/news/992189/

相关文章:

  • 怎么做网站端口代理沧州网络推广外包公司
  • php wordpress 目录seo课程培训机构
  • 常州网站建设方案优化引流app推广软件
  • 网络营销网站建设实训网络营销步骤
  • 网站都有后台吗百度竞价开户公司
  • 秭归网站建设网站seo优化心得
  • wordpress电影网站模板seo运营
  • 公司注册网上核名业务如何终止网站排名优化怎么做
  • 网站建设伍金手指下拉2网上推广平台
  • 沧州网站建设公司翼马爱情链接
  • 计算机学了出来干嘛免费优化推广网站的软件
  • 宁波网站建设优化湖南seo优化按天付费
  • 门户网站手机版google官网入口
  • 深圳市工程建设交易服务中心网站软文什么意思
  • 大型网架加工厂成都网站建设方案优化
  • 导航网站的广告怎么做的千锋教育官方网
  • etc网站开发票网站制作软件免费下载
  • 上海seo网站设计2022十大网络营销案例
  • 还有做网站的必要吗网站运营推广方案
  • 企业营销型网站建设厂家品牌搜索引擎服务优化
  • 学校网站建设计划怎么成为百度推广代理商
  • 普陀网站开发培训学校seo快速优化
  • 建一个商城网站多少钱免费的网站推广软件
  • 手机网站解决方案看网站搜什么关键词
  • 顺企网江西网站建设宜昌今日头条新闻
  • 坪山网站建设行业现状网页设计与制作代码成品
  • 网站建设需求文档模板下载学大教育一对一收费价格表
  • 小型网站怎样优化百度首页官网
  • 网站开发与iso9001关系百度上做推广怎么做
  • wordpress怎么设置导航镇江seo