如何理解pytest的参数化fixture,fixture工厂怎么用
Admin 2022-08-04 群英技术资讯 702 次浏览
fixture有个params参数,允许我们传递数据。
语法格式:
# conftest.py文件 # fixture的params参数 # 取value1时,会把依赖此fixture的用例执行一遍。 # 取value2时,会把依赖此fixture的用例执行一遍。 # 取value3时,会把依赖此fixture的用例执行一遍。 # params有几个参数,就会将依赖此fixture的用例执行几遍。 @pytest.fixture(params=[value1, value2, value3..]) def fix_name(): # do something
当我们需要多次调用fixture时,则可以用到fixture的参数化功能。
但它并不是并发的,是串行执行的。
比如,测试对象有多种配置方式,那么参数化可以帮我们在多种配置方式下执行用例。
接下来,以网页自动化为案例。
需求:要在google、firefox浏览器下执行测试用例,用例为打开百度搜索pytest。
1)先在conftest.py当中,定义fixture,并设置params=["google", "firefox"]
# conftest.py # params设置为google和firefox @pytest.fixture(params=["google", "firefox"]) def browser_fix(request): if request.param == "google": driver = webdriver.Chrome() elif request.param == "firefox": driver = webdriver.Firefox() else: driver = None yield driver if driver: driver.quit()
2)在测试用例文件test_baidu_action.py中,编写测试用例,并调用browser_fix
# test_baidu_action.py @pytest.mark.usefixtures("browser_fix") def test_baidu(browser_fix): driver = browser_fix driver.get("https://www.baidu.com/") driver.find_element(By.ID, "kw").send_keys("pytest", Keys.ENTER) loc = (By.XPATH, '//h3') WebDriverWait(driver,10).until(EC.visibility_of_element_located(loc)) driver.find_element(*loc).click()
3)运行2)中的用例,会依次在google浏览器中执行完成,然后在firefox浏览器中执行完成。一共是2条测试用例。
当我们在一个用例当中,需要多次调用fixture时,就可以使用fixture工厂
利用的是装饰器的方式
在fixture内部,定义一个函数。fixture返回的是函数。
以下案例来自官网:
@pytest.fixture def make_customer_record(): def _make_customer_record(name): return {"name": name, "orders": []} return _make_customer_record # 用例内部,多次调用了fixture. def test_customer_records(make_customer_record): customer_1 = make_customer_record("Lisa") # 第1次调用 customer_2 = make_customer_record("Mike") # 第2次调用 customer_3 = make_customer_record("Meredith") # 第3次调用
如果工厂创建的数据需要管理,那么fixtue可以如下处理:
@pytest.fixture def make_customer_record(): # 管理工厂的数据。在前置中创建。在后置中销毁 created_records = [] def _make_customer_record(name): record = models.Customer(name=name, orders=[]) # 前置中添加数据 created_records.append(record) return record yield _make_customer_record # 返回内部函数 # 销毁数据 for record in created_records: record.destroy() # 测试用例 def test_customer_records(make_customer_record): customer_1 = make_customer_record("Lisa") customer_2 = make_customer_record("Mike") customer_3 = make_customer_record("Meredith")
pytest内置的名为requests的fixture,主要功能: 提供请求fixture的测试用例/测试类的信息的。
我们定义fixture之后,通常都是测试用例/测试类,来请求fixture。
而request fixture就会记录 测试用例/测试类 相关信息。
request fixture是通过FixtureRequest来实现的,有以下属性(列举部分)可以使用:
更多的请查阅官网:https://docs.pytest.org/en/stable/reference.html
既然requests是fixture,那么我们定义的fixture,就可以直接把requests作为函数参数来用。
下面,以简单案例来演示。
定义一个fixture,将requests作为参数。
import pytest @pytest.fixture(params=[1,2]) def init(request): print("用例名称:", request.function) print("fix参数 ", request.param) print("fix的作用域 ", request.scope) print("用例所在的类 ", request.cls)
定义一个测试类,直接请求名为init的fixture:
@pytest.mark.usefixtures("init") class TestABC: def test_hello(self): print("-------------------------")
执行结果如下:
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要介绍了python流程控制,文章主要根据python流程控制的相关资料展开全文内容,具有一定的参考解释,需要的小伙伴可以参考一下,希望对你有所帮助
网上GAN的教程太多了,这边也谈一下自己的理解,本文给大家介绍一下GAN的两部分组成,有需要的朋友可以借鉴参考下,希望能够有所帮助
简介JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,它基于ECMAScript的一个子集。 JSON采用完全独立于语言的文本格式
其实关于YOLOv5的网络结构其实网上相关的讲解已经有很多了,但是觉着还是有必要再给大家介绍下,下面这篇文章主要给大家介绍了关于YOLOv5中SPP/SPPF结构源码的相关资料,需要的朋友可以参考下
这篇文章主要介绍了语义分割任务中Unet一个有意思的模型-Keras。Keras是一个由Python编写的开源人工神经网络库,可进行深度学习模型的设计、调试、评估、应用和可视化。感兴趣的小伙伴快来跟随小编一起学习一下吧
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008