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

发布时间:2022-05-31 13:52:34 作者:iii
来源:亿速云 阅读:318

本篇内容介绍了“pytest如何使用parametrize将参数化变量传递到fixture”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

一、交代应用场景

ok,大背景是这样的。

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

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

二、使用@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都请求。

参数传递

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

测试用例不执行。

“pytest如何使用parametrize将参数化变量传递到fixture”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

推荐阅读:
  1. Pytest框架之fixture的详细使用教程
  2. 如何在Pytest中使用parametrize

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

pytest parametrize fixture

上一篇:django channels使用、配置及实现群聊的方法

下一篇:vue怎么通过日期筛选数据

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》