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

redis 在网站开发中怎么用产品的推广及宣传思路

redis 在网站开发中怎么用,产品的推广及宣传思路,免费网站站长推广,drupal 网站建设pytest作为Python技术栈下最主流的测试框架,功能极为强大和灵活。其中Fixture夹具是它的核心。而且pytest中对Fixture的作用范围也做了不同区分,能为我们利用fixture带来很好地灵活性。 下面我们就来了解下这里不同scope的作用 fixture的scope定义 首…

pytest作为Python技术栈下最主流的测试框架,功能极为强大和灵活。其中Fixture夹具是它的核心。而且pytest中对Fixture的作用范围也做了不同区分,能为我们利用fixture带来很好地灵活性。

下面我们就来了解下这里不同scope的作用

fixture的scope定义

首先根据官网的说明,Pytest中fixture的作用范围支持5种设定,分别是function(默认值), classs, module, package, session

作用范围说明
function默认值,对每个测试方法(函数)生效,生命周期在测试方法级别
class对测试类生效,生命周期在测试类级别
module对测试模块生效,生命周期在模块(文件)级别
package对测试包生效,生命周期在测试包(目录)级别
session对测试会话生效,生命周期在会话(一次pytest运行)级别

下面结合代码来说明,假设目前有这样的代码结构

在这里插入图片描述

run_params是被测方法

def deal_params(p):  print(f"input :{p}")  if type(p) is int:  return p*10  if type(p) is str:  return p*3  if type(p) in (tuple, list):  return "_".join(p)  else:  raise TypeError

test_ch_param, test_fixture_scope中分别定义了参数化和在测试类中的不同测试方法

import pytest@pytest.mark.parametrize("param",[10, "城下秋草", "软件测试", ("示例", "代码")])  
def test_params_mark(param):  print(deal_params(param))
import pytest  class TestFixtureScope1:  def test_int(self):  assert deal_params(2) == 20  def test_str(self):  assert deal_params("秋草") == "秋草秋草秋草"  class TestFixtureScope2:  def test_list(self):  assert deal_params(["城下","秋草"]) == "城下_秋草"  def test_dict(self):  with pytest.raises(TypeError):  deal_params({"name": "秋草"})

在公共方法文件conftest.py中定义fixture: prepare, 设置了autouse=True,即会根据fixture的设置范围自动应用

@pytest.fixture(autouse=True, scope='function')  
def prepare():  print('-----some setup actions.....')  yield  print('-----some teardown actions!!')

这里我们分别调整prepare的scope为不同取值,然后得到对应的输出

function

(.venv) C:\Chengxiaqiucao
pytest
======================================== test session starts =========================================
collected 8 items                                                                                     BlogDemo/testDemo/test_ch_parm.py::test_params_mark[10] -----some setup actions.....
input :10
100
PASSED-----some teardown actions!!BlogDemo/testDemo/test_ch_parm.py::test_params_mark[城下秋草] -----some setup actions.....
input :城下秋草
城下秋草城下秋草城下秋草
PASSED-----some teardown actions!!BlogDemo/testDemo/test_ch_parm.py::test_params_mark[软件测试] -----some setup actions.....
input :软件测试
软件测试软件测试软件测试
PASSED-----some teardown actions!!BlogDemo/testDemo/test_ch_parm.py::test_params_mark[param3] -----some setup actions.....
input :('示例', '代码')
示例_代码
PASSED-----some teardown actions!!BlogDemo/testDemo/test_fixture_scope.py::TestFixtureScope1::test_int -----some setup actions.....
input :2
PASSED-----some teardown actions!!BlogDemo/testDemo/test_fixture_scope.py::TestFixtureScope1::test_str -----some setup actions.....      
input :秋草
PASSED-----some teardown actions!!BlogDemo/testDemo/test_fixture_scope.py::TestFixtureScope2::test_list -----some setup actions.....     
input :['城下', '秋草']
PASSED-----some teardown actions!!BlogDemo/testDemo/test_fixture_scope.py::TestFixtureScope2::test_dict -----some setup actions.....     
input :{'name': '秋草'}
PASSED-----some teardown actions!!========================================= 8 passed in 0.27s ========================================== 

fixture运行了8次

class

(.venv) C:\Chengxiaqiucao
pytest
======================================== test session starts =========================================
collected 8 items                                                                                     BlogDemo/testDemo/test_ch_parm.py::test_params_mark[10] -----some setup actions.....
input :10
100
PASSED-----some teardown actions!!BlogDemo/testDemo/test_ch_parm.py::test_params_mark[城下秋草] -----some setup actions.....
input :城下秋草
城下秋草城下秋草城下秋草
PASSED-----some teardown actions!!BlogDemo/testDemo/test_ch_parm.py::test_params_mark[软件测试] -----some setup actions.....
input :软件测试
软件测试软件测试软件测试
PASSED-----some teardown actions!!BlogDemo/testDemo/test_ch_parm.py::test_params_mark[param3] -----some setup actions.....
input :('示例', '代码')
示例_代码
PASSED-----some teardown actions!!BlogDemo/testDemo/test_fixture_scope.py::TestFixtureScope1::test_int -----some setup actions.....
input :2
PASSED
BlogDemo/testDemo/test_fixture_scope.py::TestFixtureScope1::test_str input :秋草
PASSED-----some teardown actions!!BlogDemo/testDemo/test_fixture_scope.py::TestFixtureScope2::test_list -----some setup actions.....     
input :['城下', '秋草']
PASSED
BlogDemo/testDemo/test_fixture_scope.py::TestFixtureScope2::test_dict input :{'name': '秋草'}
PASSED-----some teardown actions!!========================================= 8 passed in 0.27s ========================================== 

test_ch_param中的测试方法,因为直接定义在文件中,也属于类级别,所以每次赋值参数,fixture也被调用。 而 test_fixture_scope中明确定义了两个测试类,所以运行了2次

module

(.venv) C:\Chengxiaqiucao
pytest
======================================== test session starts =========================================
collected 8 items                                                                                     BlogDemo/testDemo/test_ch_parm.py::test_params_mark[10] -----some setup actions.....
input :10
100
PASSED
BlogDemo/testDemo/test_ch_parm.py::test_params_mark[城下秋草] input :城下秋草
城下秋草城下秋草城下秋草
PASSED
BlogDemo/testDemo/test_ch_parm.py::test_params_mark[软件测试] input :软件测试
软件测试软件测试软件测试
PASSED
BlogDemo/testDemo/test_ch_parm.py::test_params_mark[param3] input :('示例', '代码')
示例_代码
PASSED-----some teardown actions!!BlogDemo/testDemo/test_fixture_scope.py::TestFixtureScope1::test_int -----some setup actions.....
input :2
PASSED
BlogDemo/testDemo/test_fixture_scope.py::TestFixtureScope1::test_str input :秋草
PASSED
BlogDemo/testDemo/test_fixture_scope.py::TestFixtureScope2::test_list input :['城下', '秋草']
PASSED
BlogDemo/testDemo/test_fixture_scope.py::TestFixtureScope2::test_dict input :{'name': '秋草'}
PASSED-----some teardown actions!!========================================= 8 passed in 0.27s ========================================== 

修改为module范围后,可以看到,每个模块文件调用了一次fixture

package

(.venv) C:\Chengxiaqiucao
pytest
======================================== test session starts =========================================
collected 8 items                                                                                     BlogDemo/testDemo/test_ch_parm.py::test_params_mark[10] -----some setup actions.....
input :10
100
PASSED
BlogDemo/testDemo/test_ch_parm.py::test_params_mark[城下秋草] input :城下秋草
城下秋草城下秋草城下秋草
PASSED
BlogDemo/testDemo/test_ch_parm.py::test_params_mark[软件测试] input :软件测试
软件测试软件测试软件测试
PASSED
BlogDemo/testDemo/test_ch_parm.py::test_params_mark[param3] input :('示例', '代码')
示例_代码
PASSED
BlogDemo/testDemo/test_fixture_scope.py::TestFixtureScope1::test_int input :2
PASSED
BlogDemo/testDemo/test_fixture_scope.py::TestFixtureScope1::test_str input :秋草
PASSED
BlogDemo/testDemo/test_fixture_scope.py::TestFixtureScope2::test_list input :['城下', '秋草']
PASSED
BlogDemo/testDemo/test_fixture_scope.py::TestFixtureScope2::test_dict input :{'name': '秋草'}
PASSED-----some teardown actions!!========================================= 8 passed in 0.27s ========================================== 

修改为package, 这是因为两个测试文件位于同一个package内, 所以运行了一次

session

(.venv) C:\Chengxiaqiucao
pytest
======================================== test session starts =========================================
collected 8 items                                                                                     BlogDemo/testDemo/test_ch_parm.py::test_params_mark[10] -----some setup actions.....
input :10
100
PASSED
BlogDemo/testDemo/test_ch_parm.py::test_params_mark[城下秋草] input :城下秋草
城下秋草城下秋草城下秋草
PASSED
BlogDemo/testDemo/test_ch_parm.py::test_params_mark[软件测试] input :软件测试
软件测试软件测试软件测试
PASSED
BlogDemo/testDemo/test_ch_parm.py::test_params_mark[param3] input :('示例', '代码')
示例_代码
PASSED
BlogDemo/testDemo/test_fixture_scope.py::TestFixtureScope1::test_int input :2
PASSED
BlogDemo/testDemo/test_fixture_scope.py::TestFixtureScope1::test_str input :秋草
PASSED
BlogDemo/testDemo/test_fixture_scope.py::TestFixtureScope2::test_list input :['城下', '秋草']
PASSED
BlogDemo/testDemo/test_fixture_scope.py::TestFixtureScope2::test_dict input :{'name': '秋草'}
PASSED-----some teardown actions!!========================================= 8 passed in 0.27s ========================================== 

最后,当设置为session时,也就是运行pytest的一次执行会话,才会触发一次fixture调用

所以可以看到,我们通过fixture的不同scope定义,可以根据需要,来确定我们编写的fixture夹具的作用范围。有很好的灵活性

复杂fixture的scope灵活定义

有时在实际使用的时候,特别是我们的一些fixture初始化工作比较复杂但同时在不同作用范围下都可能会用到,这时如果仅仅因为针对不同的作用范围,就要编写多个不同的fixture,代码就显得比较冗余。这时可以怎么处理呢? 其实可以利用上下文contextmanager来灵活实现

比如我们再编写一个fixture的基本代码上下文:

@contextmanager  
def fixture_base():  print('~~~~~base fixture setup actions.....')  yield  print('~~~~~base fixture teardown actions!!')

然后针对不同的fixture,我们就可以根据不同的scope来定义不同的fixture并调用这里的context实现。 比如我们再定义一个scope为package的fixture

@pytest.fixture(autouse=False, scope='package')  
def fixture_module():  """  对于复杂的fixture但希望灵活处理scope,可以将公共代码放到一个contextmanager中,  再针对不同scope定义相关对应fixture  """    with fixture_base() as result:  yield result

👍👍这个方法来自pytest的社区总结,原始问题链接

不同scope的执行顺序

上面例子中我们其实看到package和session的执行效果,因为测试方法都在同一个package中,所以效果上没什么差异。但其实不同scope也是有执行顺序的

顺序总结如下:

session > package > module > class > function

这里增加到两个fixture以后,执行的结果:

(.venv) C:\Chengxiaqiucao
pytest
======================================== test session starts =========================================
collected 8 items                                                                                     BlogDemo/testDemo/test_ch_parm.py::test_params_mark[10] -----some setup actions.....
~~~~~base fixture setup actions.....
input :10
100
PASSED
BlogDemo/testDemo/test_ch_parm.py::test_params_mark[城下秋草] input :城下秋草
城下秋草城下秋草城下秋草
PASSED
BlogDemo/testDemo/test_ch_parm.py::test_params_mark[软件测试] input :软件测试
软件测试软件测试软件测试
PASSED
BlogDemo/testDemo/test_ch_parm.py::test_params_mark[param3] input :('示例', '代码')
示例_代码
PASSED
BlogDemo/testDemo/test_fixture_scope.py::TestFixtureScope1::test_int input :2
PASSED
BlogDemo/testDemo/test_fixture_scope.py::TestFixtureScope1::test_str input :秋草
PASSED
BlogDemo/testDemo/test_fixture_scope.py::TestFixtureScope2::test_list input :['城下', '秋草']
PASSED
BlogDemo/testDemo/test_fixture_scope.py::TestFixtureScope2::test_dict input :{'name': '秋草'}
PASSED~~~~~base fixture teardown actions!!
-----some teardown actions!!========================================= 8 passed in 0.27s ========================================== 

可以看到 sessionpackage 更早执行,同时更晚被销毁。

那么以上就是关于pytest scope作用范围的总结



文章转载自:
http://umber.zfqr.cn
http://clubwoman.zfqr.cn
http://twelve.zfqr.cn
http://nymphaeaceous.zfqr.cn
http://bacteric.zfqr.cn
http://gargoyle.zfqr.cn
http://morphactin.zfqr.cn
http://disyllable.zfqr.cn
http://scapulary.zfqr.cn
http://climber.zfqr.cn
http://honorific.zfqr.cn
http://lowing.zfqr.cn
http://rubicundity.zfqr.cn
http://chiffchaff.zfqr.cn
http://confiscable.zfqr.cn
http://disentail.zfqr.cn
http://masjid.zfqr.cn
http://alpinist.zfqr.cn
http://asid.zfqr.cn
http://tattler.zfqr.cn
http://perjury.zfqr.cn
http://flareback.zfqr.cn
http://inexertion.zfqr.cn
http://pragmatics.zfqr.cn
http://alveolate.zfqr.cn
http://unavoidably.zfqr.cn
http://ajog.zfqr.cn
http://agaric.zfqr.cn
http://thwartships.zfqr.cn
http://deodorant.zfqr.cn
http://cornelius.zfqr.cn
http://palette.zfqr.cn
http://androecium.zfqr.cn
http://gumban.zfqr.cn
http://semiprivate.zfqr.cn
http://wivern.zfqr.cn
http://maine.zfqr.cn
http://antipyrotic.zfqr.cn
http://monophonic.zfqr.cn
http://bombe.zfqr.cn
http://cephalitis.zfqr.cn
http://dover.zfqr.cn
http://haptometer.zfqr.cn
http://mathematics.zfqr.cn
http://feudalist.zfqr.cn
http://unsayable.zfqr.cn
http://nudism.zfqr.cn
http://lockup.zfqr.cn
http://geometrid.zfqr.cn
http://nope.zfqr.cn
http://pignus.zfqr.cn
http://rootlike.zfqr.cn
http://inform.zfqr.cn
http://jagt.zfqr.cn
http://suggested.zfqr.cn
http://insidious.zfqr.cn
http://ligamentum.zfqr.cn
http://unrestraint.zfqr.cn
http://ascap.zfqr.cn
http://georgina.zfqr.cn
http://dispensary.zfqr.cn
http://transductant.zfqr.cn
http://jewess.zfqr.cn
http://flatbed.zfqr.cn
http://unemotional.zfqr.cn
http://archaeopteryx.zfqr.cn
http://element.zfqr.cn
http://antirheumatic.zfqr.cn
http://squirarch.zfqr.cn
http://etiolate.zfqr.cn
http://scallion.zfqr.cn
http://scorcher.zfqr.cn
http://metallic.zfqr.cn
http://undee.zfqr.cn
http://stover.zfqr.cn
http://elementary.zfqr.cn
http://cyborg.zfqr.cn
http://hemocyte.zfqr.cn
http://miasmatic.zfqr.cn
http://cataclysm.zfqr.cn
http://periosteum.zfqr.cn
http://paddlefish.zfqr.cn
http://metapsychical.zfqr.cn
http://recognise.zfqr.cn
http://jis.zfqr.cn
http://riffy.zfqr.cn
http://napkin.zfqr.cn
http://actable.zfqr.cn
http://viable.zfqr.cn
http://dessert.zfqr.cn
http://oceanaut.zfqr.cn
http://tirewoman.zfqr.cn
http://hypothyroidism.zfqr.cn
http://scheelite.zfqr.cn
http://aquiform.zfqr.cn
http://cantiga.zfqr.cn
http://ligniferous.zfqr.cn
http://retrospectus.zfqr.cn
http://foldboater.zfqr.cn
http://coachwork.zfqr.cn
http://www.hrbkazy.com/news/57614.html

相关文章:

  • 内网网站建设所需硬件设备阿里巴巴推广
  • 网站建设自学网络营销的作用
  • vue做的网站影响收录么常德论坛网站
  • 南阳百度网站推广seo长尾关键词
  • 如何介绍网站模板下载地址一键制作网站
  • 后台java语言做网站永久免费自动建站
  • qq空间怎么跟网站做链接吗成人短期培训学校
  • 小说网站开发思路网站模板搭建
  • 如何做装修网站网站建立具体步骤是
  • 广东网站建设英铭科技seo的优点有哪些
  • 娄底网站开发个人网站建站流程
  • 电商网站建设bt磁力搜索引擎
  • 成都医院网站建设域名是什么 有什么用
  • 街道口做网站拉新app推广接单平台
  • 如何制作网站视频发表文章的平台有哪些
  • 怎么在网站做谷歌广告传统营销与网络营销的区别
  • 做网站有一行一行写代码的吗抖音关键词挖掘工具
  • 找券网站怎么做英文外链seo兼职在哪里找
  • 郑州市建设厅网站百度指数特点
  • 做网站需要会写代码6软文广告素材
  • 动态ip怎么建设网站关键词推广方式
  • 商城网站要怎样设计seo网站优化服务商
  • 广告公司网站源码百度做推广一般要多少钱
  • 网络游戏排行榜2020前十名北京网站优化体验
  • wordpress iplaysoft湖南seo优化报价
  • 溧阳网站开发百度一下你就知道网页
  • 做拼多多网站赚钱吗seo优化课程
  • wordpress导入json南昌seo排名
  • 如何做自己的独立的网站百度营销官网
  • 一番赏公众号开发国内好的seo