pytest使用parametrize将参数化变量传递到fixture

作者:把苹果咬哭的测试笔记 时间:2022-03-28 23:30:18 

分享一个关于在pytest中,如何将测试用例文件中的变量传递到fixture函数。

一、交代应用场景

  • 目前组内的项目,在根目录下是有一个conftest.py文件的,这里有个生成api token的fixture函数,就叫它gen_token()吧。

  • 每个case包下,也会有个conftest.py,用于存放适用于本模块下测试用例的fixture函数,比如有个叫setup_before()。

  • 因为拿token是请求接口的前提,所以在case里,比如有个test_case()里,要传顶层的fixture函数,也就是这样test_case(gen_token)。

  • 顶层的gen_token(),是需要3个传参的。因为不同case可能涉及到的生成不同用户的token,所以我们把这个参数放在了case文件里。

ok,大背景是这样的。

现在有小伙伴来需求了,她要在setup_before()里去造数,通过请求另一个接口,这个请求也需要使用token。

那么,问题也就可以转化为:

  • 要将case文件里的参数,传递到fixture函数中。

  • gen_token()里返回的值,setup_before()和test_case()里都要拿到。

二、使用@pytest.mark.parametrize、以及fixture的调用来解决

这里把实际代码抽象一下,转化为简易代码,方便演示和理解:

# 目录结构
-- /demo_top
 -- /demo_sub
     __init__.py
     conftest.py
     test_case.py
 __init__.py
 conftest.py

以下分别是/demo_top/conftest.py、/demo_top/demo_sub/conftest.py、/demo_top/demo_sub/test_case.py的内容。

1. /demo_top/conftest.py

# content of /demo_top/conftest.py
import pytest
@pytest.fixture()
def gen_token(request):
   params = request.param
   print("\n根目录下gen_token()拿到的参数:", params)
   if params[0] + params[1] == 5:
       return "api_token"
   else:
       return None

这里,模拟生成token的fixture函数,当传过来的值相加等于5,就会返回"api_token",否则返回None。

2. /demo_top/demo_sub/conftest.py

# content of /demo_top/demo_sub/conftest.py
import pytest
@pytest.fixture()
def setup_before(request, gen_token):
   print("执行子级setup_before,拿到的传参:", request.param)
   print("执行子级setup_before,拿到gen_token的返回值:", gen_token)
   if gen_token:
       yield "造数完成"
       print("测试用例test_case执行完毕,清理测试数据")
   else:
       pytest.skip("跳过")

这里模拟了给测试用例造数据的fixture函数,如果没拿到token的话,就跳过测试用例。

3. /demo_top/demo_sub/test_case.py

# content of /demo_top/demo_sub/test_case.py
import pytest
test_param = [(1, 4)]
@pytest.mark.parametrize("gen_token", test_param, indirect=True)
@pytest.mark.parametrize("setup_before", test_param, indirect=True)
def test_case1(gen_token, setup_before):
   print("\n测试用例里拿到的gen_token返回值:", gen_token)
   print("测试用例里拿到的setup_before返回值:", setup_before)
   print("执行测试用例test_case1...")
if __name__ == '__main__':
   pytest.main(['-s', 'test_case.py'])

这是测试用例文件了,里面有个测试函数test_case1,因为它需要用到2个fixture函数返回的值,所以gen_token, setup_before都请求。

参数传递

  • @pytest.mark.parametrize:使用pytest内置的parametrize,来把参数传递给目标fixture函数,你希望把参数传递给哪个fixture函数就加哪个。比如这里的gen_token和setup_before,注意名称与fixture名称一致。

  • indirect=True:作用是让parametrize中的参数名称,也就是"gen_token"当成函数执行,并且后面的参数值test_param,作为"gen_token"的传参。

  • request.param:接受传参的fixture函数,使用request.param来获取值。

fixture调用fixture

fixture之间的相互调用,在之前的文章里已经有过详述了。既然这里setup_before依赖gen_token,之间传递调用即可setup_before(request, gen_token)。

在各环节做了些print打印出信息,帮助理解执行过程。

test_case.py                                                            [100%]
============================== 1 passed in 0.08s ==============================
根目录下gen_token()拿到的参数: (1, 4)
执行子级setup_before,拿到的传参: (1, 4)
执行子级setup_before,拿到gen_token的返回值: api_token
.
测试用例里拿到的gen_token返回值: api_token
执行测试用例test_case1...
测试用例test_case执行完毕,清理测试数据
Process finished with exit code 0

再看下gen_token不返回token的情况,改下传参test_param = [(2, 4)]。

test_case.py                                                            [100%]
============================= 1 skipped in 0.08s ==============================s
根目录下gen_token()拿到的参数: (2, 4)
执行子级setup_before,拿到的传参: (2, 4)
执行子级setup_before,拿到gen_token的返回值: None
Skipped: 跳过
Process finished with exit code 0

测试用例不执行。

来源:https://blog.csdn.net/wessonlan/article/details/124813048

标签:pytest,parametrize,变量传递,fixture
0
投稿

猜你喜欢

  • asp sql去左右空格函数

    2008-03-04 17:29:00
  • asp如何写入超长的字符串?

    2010-06-09 18:53:00
  • Asp.net实现简单的文字水印

    2007-08-24 09:28:00
  • 防盗链接ASP函数

    2011-03-07 11:02:00
  • PHP 检查扩展库或函数是否可用的代码

    2023-07-22 23:34:34
  • Reflow

    2009-10-25 12:34:00
  • Python二叉树的镜像转换实现方法示例

    2023-09-28 18:36:35
  • ie6 img onload

    2009-04-08 17:23:00
  • Study jQuery in a Simplified Way

    2010-01-30 12:55:00
  • iframe框架用JavaScript子页面控制父页面

    2009-01-19 13:43:00
  • python交互式图形编程实例(一)

    2022-11-12 14:44:53
  • pytest解读fixtures之Teardown处理yield和addfinalizer方案

    2023-06-18 22:13:01
  • CSS浏览器兼容方案

    2008-06-10 12:21:00
  • 详解appium+python 启动一个app步骤

    2021-12-19 02:44:55
  • IE6局部调用PNG32合并图片

    2009-03-11 21:24:00
  • VBScript中清除数组元素Erase语句

    2008-06-27 13:05:00
  • 详细讲解如何为MySQL数据库添加新函数

    2008-11-27 17:06:00
  • 空间session失效过快的解决办法

    2010-09-15 10:01:00
  • 禁止在网页里面是用搜狗的云输入法

    2009-11-29 15:50:00
  • python 计算t分布的双侧置信区间

    2023-08-01 03:06:05
  • asp之家 网络编程 m.aspxhome.com