温馨提示×

温馨提示×

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

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

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

发布时间:2022-05-31 13:52:34 来源:亿速云 阅读:316 作者:iii 栏目:开发技术

本篇内容介绍了“pytest如何使用parametrize将参数化变量传递到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

测试用例不执行。

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

向AI问一下细节

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

AI