pytest 的 fixture 进阶玩法 (2)

本贴最后更新于 1472 天前,其中的信息可能已经时移世改

pytest的fixture有很多用法,本文在上两篇的文章继续补充fixture的使用。

第一篇文章地址:http://testingpai.com/article/1603683663384

第二篇文章地址:http://testingpai.com/article/1614589542001


本文关于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()
  1. 在测试用例文件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()
  1. 运行2)中的用例,会依次在google浏览器中执行完成,然后在firefox浏览器中执行完成。一共是2条测试用例。

image.png


2、fixture工厂

当我们在一个用例当中,需要多次调用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")

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作为参数。

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("-------------------------")

执行结果如下:

image.png

回帖
请输入回帖内容 ...