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

网站开发与服务器交互长沙人才招聘网官网

网站开发与服务器交互,长沙人才招聘网官网,企业综合查询网站,做阿里巴巴网站要多少钱1.pytest中的xunit风格前后置处理 pytest中用例的前后置可以直接使用类似于unittest中的前后置处理,但是pytest中的前后置处理方式更 加丰富,分为模块级、类级、方法级、函数级等不同等级的前后置处理,具体见下面的代码: test_…

1.pytest中的xunit风格前后置处理

pytest中用例的前后置可以直接使用类似于unittest中的前后置处理,但是pytest中的前后置处理方式更

加丰富,分为模块级、类级、方法级、函数级等不同等级的前后置处理,具体见下面的代码:

test_demo_001.py文件

def setup_function():#类外使用
print('函数级的前置,每个函数级用例前均运行一次')
def teardown_function():#类外使用
print('函数级的前置,每个函数级用例后均运行一次')
def test_function_001():
print('测试:函数级的用例test_function_001')
def test_function_002():
print('测试:函数级的用例test_function_002')
def setup_module():
print('模块级的前置,每个文件中所有用例执行前仅运行一次')
def teardown_module():
print('模块级的后置,每个文件中所有用例执行后仅运行一次')
class TestDemo001:
@classmethod
def setup_class(cls):
print('类级的前置,TestDemo001类下的所有用例执行前运行一次')
@classmethod
def teardown_class(cls):
print('类级的后置,TestDemo001类下的所有用例执行后运行一次')
def setup_method(self): #类中使用
print('方法级的前置,每个方法级用例前均运行一次')
def teardown_method(self): #类中使用
print('方法级的后置,每个方法级用例后均运行一次')
def test_aaa_001(self):
print('测试:类下方法级的用例test_aaa_001')
def test_aaa_002(self):
print('测试:类下方法级的用例test_aaa_002')
class TestDemo002:
@classmethod
def setup_class(cls):
print('类级的前置,TestDemo002类下的所有用例执行前运行一次')
@classmethod
def teardown_class(cls):
print('类级的后置,TestDemo002类下的所有用例执行后运行一次')
def setup_method(self): #等价于def setup(self):
print('方法级的前置,每个方法级用例前均运行一次')
def teardown_method(self): #等价于def teardown(self):
print('方法级的后置,每个方法级用例后均运行一次')
def test_bbb_001(self):
print('测试:类下方法级的用例test_bbb_001')
def test_bbb_002(self):
print('测试:类下方法级的用例test_bbb_002')
'''
setup/teardown 等价于 setup_method/teardown_method;2者仅使用一种即可,如果同时时候,只使
用setup_method/teardown_method
整体运行过程:
setup_module
setup_function--->test_function_001--->teardown_function
setup_function--->test_function_002--->teardown_function
setup_class #TestDemo001的
setup_method--->test_aaa_001--->teardown_method
setup_method--->test_aaa_002--->teardown_method
teardown_class #TestDemo001的
setup_class #TestDemo002的
setup_method--->test_bbb_001--->teardown_method
setup_method--->test_bbb_002--->teardown_method
teardown_class #TestDemo002的
teardown_module
'''

2.使用fixture实现灵活的用例前后置处理

使用Xunit风格的前置后置的使用,我们发现这种前后置无法复用,在另外的一个.py文件或者.py文件中 的类使用时,需要重新再写,相对不灵活。pytest结合fixture可以实现用例进行灵活的前后置处理。

2.1.@pytest.fixture进行用例的前置处理

import pytest
@pytest.fixture()
def aaa():
print('这是用例的前置操作')
def test_001(aaa):
print('用例test_001')
@pytest.fixture #fixture中不传参数,可以不带括号
def bbb():
print('用例前置操作,并给返回值')
return 100
def test_002(bbb):
print('用例test_002')
print('可以使用前置函数的名称来作为其返回值使用:bbb={}'.format(bbb))
def test_003(bbb,aaa):
print('用例test_003')
print('可以同时做多个前置操作,操作顺序按传递的参数顺序来进行')
print('我们可以发现前置函数aaa,bbb一次定义,可以多次使用,体现了灵活性')
print(bbb,aaa)
if __name__ == '__main__':
pytest.main()

2.2.@pytest.fixture的嵌套使用

#fixture的嵌套,会先调用里层的fixture,在调用外层的fixture
@pytest.fixture()
def aaa():
print('111111111111')
return 'aaa'
@pytest.fixture()
def bbb(aaa):
print('22222222')
return ['bbb',aaa]
def test_001(bbb):
print('测试用例test_001')
print(bbb)
if __name__ == '__main__':
pytest.main()

2.3.@pytest.fixture结合@pytest.mark.parametrize()实现参数

import yaml,pytest

#读取参数化数据的文件内容
def get_yaml_data(yaml_path):
with open(yaml_path,'r',encoding='utf-8') as f:
data = yaml.safe_load(f)
return data
#获取yaml文件的数据
yaml_data = get_yaml_data('./login.yaml')
@pytest.fixture()
def case_data(request):
return request.param
#indirect=True时'case_data'当做函数运行,就是这里定义的fixture
#indirect=False时'case_data'当做普通变量使用,默认值为False
#工作中建议使用默认值:False,就当做普通变量使用即可,这时可以不使用fixture进行参数化
@pytest.mark.parametrize('case_data',yaml_data,indirect=True)
def test_002(case_data):
print(case_data)
if __name__ == '__main__':
pytest.mian(['-vs','demo1.py'])

2.4.直接使用@pytest.fixture()来进行参数化

#演示代码时,需要先在当前目下新建login.yaml文件
#不使用@pytest.mark.parametrize()来参数化,直接利用@pytest.fixture()的传参功能进行参数化
import yaml,pytest
#封装读取参数化数据的文件内容
def get_yaml_data(yaml_path):
with open(yaml_path,'r',encoding='utf-8') as f:
data = yaml.safe_load(f)
return data
#通过在fixture中使用parmas接受参数化的值,将值传递给固定的接受参数requests
#如果传入的是列表,会将列表中的每个元素返回,如果传入的是字典会将字的每个key返回
@pytest.fixture(params=get_yaml_data('./login.yaml')[0])
def case_data(request):
return request.param
#测试用例test_003使用fixture:case_data,case_data的值就是fixture函数中的返回值
def test_003(case_data):
print(case_data)

2.5.结合yield来进行前后置处理

import pytest,allure
from selenium import webdriver
@pytest.fixture()
def brower():
driver = webdriver.Chrome()
yield driver
driver.quit()

 

 


文章转载自:

http://00000000.LqkLf.cn
http://00000000.LqkLf.cn
http://00000000.LqkLf.cn
http://00000000.LqkLf.cn
http://00000000.LqkLf.cn
http://00000000.LqkLf.cn
http://00000000.LqkLf.cn
http://00000000.LqkLf.cn
http://00000000.LqkLf.cn
http://00000000.LqkLf.cn
http://00000000.LqkLf.cn
http://00000000.LqkLf.cn
http://00000000.LqkLf.cn
http://00000000.LqkLf.cn
http://00000000.LqkLf.cn
http://00000000.LqkLf.cn
http://00000000.LqkLf.cn
http://00000000.LqkLf.cn
http://00000000.LqkLf.cn
http://00000000.LqkLf.cn
http://00000000.LqkLf.cn
http://00000000.LqkLf.cn
http://00000000.LqkLf.cn
http://00000000.LqkLf.cn
http://00000000.LqkLf.cn
http://00000000.LqkLf.cn
http://00000000.LqkLf.cn
http://00000000.LqkLf.cn
http://00000000.LqkLf.cn
http://00000000.LqkLf.cn
http://www.dtcms.com/wzjs/613425.html

相关文章:

  • 做平面什么网站好用长沙网站建设好处
  • 无锡网站建设系统营销型网站建设团队
  • 合肥网站建设+一浪怎么识别网站是用什么语言做的
  • 引航博景网站做的好吗microsoft免费网站
  • 公司网站开发建设知名企业logo
  • 什么是门户类型的网站做外围代理要有自己的网站
  • 网站建设 绍兴的公司哪家好域名和网站空间怎么做解析
  • 内蒙古网站建设信息找工程项目的平台
  • 网站如何跳转html代码是什么
  • 深圳建设局官网站首页一个网站多大
  • 网站怎么做流程重庆人居建设集团网站
  • 中迅做网站是模板站吗企业服务专员
  • 网站 网址 域名互联网开发技术
  • 济宁有做企业网站吗网站制作难点
  • 上海礼品定制网站网络营销的缺点及建议
  • 做竞价要会做网站吗360建筑网 诚是什么意思
  • 做网站一般什么问题网站诊断分析案例
  • 做请帖的网站瑞士自助游 做的好的网站
  • 东莞市做网站的公司外贸网站建设可以吗
  • 网站建设中 提示建设行政主管部门相关网站
  • 网站建设考试多选题查国外企业信息的网站
  • 做淘客网站怎么怎么样做电影网站
  • 网站 简约网络营销心得体会300字
  • 企业网站建设策划书方案范文咸宁做网站的公司
  • 聊城那里有做网站建设网站市场分析
  • 网站设计排行榜青岛网站设计推广
  • 网站建设合同2018常州网站建设智博
  • 免费1级做爰片动漫在线观看网站如何制作网站和网页
  • 访问国外的网站很慢宠物网页设计图片
  • 网上做网站过程厦门公司注册网站