# Pytest架构之fixture详细说明（三）

> 作者/来源: UCloud 运营管理员
> 发布时间: 2022-12-23T06:20:00.000Z
> 分类: 推荐奖励
> 标签: AI, Python
> 原文链接: http://117.50.162.249:3000/yun/articles/1972

---

# Pytest架构之fixture详细说明（三）

> 来源: https://www.ucloud.cn/yun/128731.html
> 作者: 89542767
> 发布日期: 发布于2022-12-23 14:20

原文中详细的讲解了Pytest架构之fixture，原文中根据实例编码推荐的十分详尽。对大家学习培训和工作具有很强的参阅参考意义，需用的小伙伴可以参考一下

　　原文中有关fixture的具体内容如下所示：

　　1、参数化设计fixture

　　2、fixture工厂

　　3、request这一fixture

　　1、参数化设计fixture

　　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条测试用例。

![01.png](https://ucloud-blog.cn-bj.ufileos.com/articles/128731/images/128731_000.png)

　　2、fixture工厂

　　我们在1个功能测试之中，需用数次调用fixture时，就可以用fixture工厂

　　运用的是装饰器的形式

　　在wixture内部，界定一个函数。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")
```

　　3、request这一fixture

　　pytest内嵌的名为requests的fixture,基本功能：给予要求fixture的功能测试/测试类的数据的。

　　大家界定fixture以后，一般都是功能测试/测试类，来要求fixture。

　　而requestfixture便会纪录功能测试/测试类有关信息。

　　requestfixture是根据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

　　即然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("-------------------------")
```

　　执行结果如下：

![02.png](https://ucloud-blog.cn-bj.ufileos.com/articles/128731/images/128731_001.png)

　　综上所述，这篇文章就给大家介绍到这里了，希望可以给大家带来帮助。