pytest学习(五) - fixture(三)
本文关于fixture的内容如下:
1、参数化fixture
2、fixture工厂
3、request这个fixture
1、参数化fixture
fixture有个params参数,允许我们传递数据。
语法格式:
1 2 3 4 5 6 7 8 9 10 | # 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"]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | # 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
1 2 3 4 5 6 7 8 9 10 | # 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条测试用例。
2、fixture工厂
当我们在一个用例当中,需要多次调用fixture时,就可以使用fixture工厂
利用的是装饰器的方式
在fixture内部,定义一个函数。fixture返回的是函数。
以下案例来自官网:
1 2 3 4 5 6 7 8 9 10 11 12 | @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可以如下处理:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | @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" ) |
3、request这个fixture
pytest内置的名为requests的fixture,主要功能: 提供请求fixture的测试用例/测试类的信息的。
我们定义fixture之后,通常都是测试用例/测试类,来请求fixture。
而request fixture就会记录 测试用例/测试类 相关信息。
request fixture是通过FixtureRequest来实现的,有以下属性(列举部分)可以使用:
request.param:获取fixture的params参数值
request.scope:获取fixture的作用域
request.function:获取调用fixture的用例函数名称。如果fixture是函数级别的作用域。
request.cls:获取测试用例是从哪个测试类里收集的。
request.module:获取测试用例/测试类从哪个python模块里收集的。
request.config:从pytest的config文件当中,获取与当前请求有关的配置信息
更多的请查阅官网:https://docs.pytest.org/en/stable/reference.html#request
既然requests是fixture,那么我们定义的fixture,就可以直接把requests作为函数参数来用。
下面,以简单案例来演示。
定义一个fixture,将requests作为参数。
1 2 3 4 5 6 7 8 | 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:
1 2 3 4 5 | @pytest .mark.usefixtures( "init" ) class TestABC: def test_hello( self ): print ( "-------------------------" ) |
执行结果如下:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
2017-10-12 nvm的使用
2017-10-12 nrm的使用