pytest-yaml-sanmu(七):使用fixture返回值

图片

fixture 是 pytest 中非常重要的功能,大部分项目都可能会用到 fixture。

pytest 的内置标记 usefixtures 可以帮助用例自动的使用 fixture

1. 创建 fixture

pytest 中的 fixtures 大致有两个用途

  1. 在用例执行之前、执行之后,自动的执行

  2. 通过 fixture 为用例提供数据

@pytest.fixture()def print_msg():    print('用例开始执行') #  yeild 之前为前置    yield
    print('用例执行结束')#  yeild 之后为前置

@pytest.fixturedef base_url():    yield 'https://www.baidu.com' #  yeild 指定返回值

当然了,也可以同时实现两种用途

@pytest.fixturedef base_url():    print('用例开始执行') #  yeild 之前为前置    yield 'https://www.baidu.com' #  yeild 指定返回值    print('用例执行结束')#  yeild 之后为前置

2. 请求 fixture

用例如果需要使用 fixture,可以通过标记 usefixtures 来进行请求。

注意标记名是 s 结尾,表明允许同时请求多个 fixture:​​​​​​​

test_name: 请求fixturemark:  - usefixtures:      - print_msg      - base_urlsteps:  - request:      method: get      url: http://baidu.com

执行结果如下:

图片

图 1. 请求 fixture 执行前后置操作

3. 使用 fixture 返回值

在 fixture 的第二个用途中,会为用例提供数据。

那么用例该如何获取和使用这个数据呢?一共有两种方式

第一种方式,

如果该数据是字符串或数字,可以直接写入 yaml 中使用的,可采用【参数变量】的方式进行使用​​​​​​​

test_name: fixture返回值mark:  - usefixtures:      - print_msg      - base_urlsteps:  - request:      method: get      url: ${base_url}/abc.html

执行结果如下:

图片

图 2. 使用 fixture 返回值

有结果可知,用例使用来 fixture 的返回值来决定服务器地址

第二种方式,

如果 fixture 返回值是一个对象,不能直接作为 yaml 内容,可在 hook 中更灵活的使用

首先创建 fixture:​​​​​​​

@pytest.fixture()def driver():    obj = webdriver.Chrome()    yield obj

接着在 yaml 中请求 fixture:​​​​​​​

test_name: 在hook中使用fixturemark:  - usefixtures:      - driversteps:  - request:      method: get      url: /abc.html

最后在 hook 中指定 fixture 返回值使用方式​​​​​​​

def pytest_yaml_run_step(item):    step = item.current_step    request = step.get('request')        fixture_client = item.usefixtures.get('client') # 获取usefixtures中的fixture        if fixture_client:        fixture_client.request(**request) # 使用fixture发送请求        return

第二种方法为用例的执行方式提供更多的灵活和扩展性,适合对 pytest 比较熟悉之后使用。

4. 参数化 fixture

pytest 的 fixture 也可以参数化,其效果和 mark.parametrize 相似,都会生成更多的用例来执行。

唯一的区别是:

  • mark.parametrize 的参数值由用例提供,写在 yaml 中

  • fixture 参数化的参数值由 fixture 提供,写在 python 中

下面是一个参数化的 fixture​​​​​​​

@pytest.fixture(params=['a', 'b', 'c'])def name(request):    return request.param

其返回值不是固定的,而是依次将 abc 作为返回值,这使得请求该 fixture 的用例也湖执行 3 次。

test_name: fixture返回值mark:  - usefixtures:      - namesteps:  - request:      method: get      url: https://www.baidu.com/?o=${name}

执行结果如下:

图片

图 3. 使用参数化 fixture

能看到这里说明是真爱,关注一下吧

相关推荐

  1. pytest前后和@pytest.fixtrue使用

    2024-07-09 22:32:08       29 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-09 22:32:08       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-09 22:32:08       71 阅读
  3. 在Django里面运行非项目文件

    2024-07-09 22:32:08       58 阅读
  4. Python语言-面向对象

    2024-07-09 22:32:08       69 阅读

热门阅读

  1. 使用工业自动化的功能块实现大语言模型应用

    2024-07-09 22:32:08       23 阅读
  2. Linux 内核编译与模块开发:深入掌握系统核心

    2024-07-09 22:32:08       27 阅读
  3. CUDA Kernel调试与优化--背景知识扫盲(LLM生成)

    2024-07-09 22:32:08       16 阅读
  4. C语言希尔排序详解与实例

    2024-07-09 22:32:08       22 阅读
  5. 多尺度旋转编码

    2024-07-09 22:32:08       23 阅读
  6. PostgreSQL的使用

    2024-07-09 22:32:08       21 阅读
  7. 定投就像还房贷

    2024-07-09 22:32:08       22 阅读
  8. Linux下python抓取动态网页内容

    2024-07-09 22:32:08       28 阅读