pytest学习和使用-Pytest的fixture如何使用request传入参数?

【阅读目录】

  • 1 使用场景

  • 2 传单个参数

  • 3 传多个参数

  • 4 多个fixtrue

1 使用场景

  • 当我们为了提高用例的复用性,会用到不同的fixture,比如登陆场景;

  • 但是如果登陆场景,我们使用不同的账号进行测试,那如何来做?

  • 此时不能使用fixture把账号直接写死,需要通过传参的方式来实现。

2 传单个参数

 
  1. import pytest

  2. # 传一个参数

  3. @pytest.fixture()

  4. def user_login(request):

  5. user_name = request.param

  6. return user_name

  7. data = ["user_name01", "user_name02"]

  8. @pytest.mark.parametrize("user_login", data, indirect=True)

  9. def test_login(user_login):

  10. print(f"登陆用户的名称为:{user_login}")

  11. if __name__ == '__main__':

  12. pytest.main(["-s", "test_request.py"])

 
  1. test_request.py::test_login[user_name01] PASSED

  2. [ 50%]登陆用户的名称为:user_name01

  3. test_request.py::test_login[user_name02] PASSED

  4. [100%]登陆用户的名称为:user_name02

  5. ============================== 2 passed in0.03s ==============================

  • 其中indirect=True  参数是为了把 user_login 当成一个函数去执行,而不是一个参数,并且将data当做参数传入函数。

3 传多个参数

  • 传多个参数,需要通过字典去传。

 
  1. import pytest

  2. # 传多个参数

  3. @pytest.fixture()

  4. def user_login(request):

  5. user_name = request.param

  6. return user_name

  7. data = [{"user_name": "user_name01", "passwd": "passwd01"},

  8. {"user_name": "user_name02", "passwd": "passwd02"}

  9. ]

  10. @pytest.mark.parametrize("user_login", data, indirect=True)

  11. def test_login(user_login):

  12. print(f"登陆用户的名称为:{user_login['user_name']},登陆的密码: {user_login['passwd']}")

  13. if __name__ == '__main__':

  14. pytest.main(["-s", "test_request01.py"])

 
  1. test_request01.py::test_login[user_login0] PASSED

  2. [ 50%]登陆用户的名称为:user_name01,登陆的密码: passwd01

  3. test_request01.py::test_login[user_login1] PASSED

  4. [100%]登陆用户的名称为:user_name02,登陆的密码: passwd02

  5. ============================== 2 passed in0.04s ==============================

4 多个fixtrue

 
  1. import pytest

  2. # 传多个fixture

  3. @pytest.fixture()

  4. def user_name(request):

  5. name = request.param

  6. return name

  7. @pytest.fixture()

  8. def user_pwd(request):

  9. pwd = request.param

  10. return pwd

  11. data = [("user_name01", "pwd01"), ("user_name02", "pwd02")]

  12. @pytest.mark.parametrize("user_name, user_pwd", data, indirect=True)

  13. def test_login(user_name, user_pwd):

  14. print(f"登陆信息为{user_name}, {user_pwd}")

  15. if __name__ == '__main__':

  16. pytest.main(["-s", "test_request02.py"])

 
  1. test_request02.py::test_login[user_name01-pwd01] PASSED

  2. [ 50%]登陆信息为user_name01, pwd01

  3. test_request02.py::test_login[user_name02-pwd02] PASSED

  4. [100%]登陆信息为user_name02, pwd02

  5. ============================== 2 passed in0.03s ==============================

 

感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:

这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!有需要的小伙伴可以点击下方小卡片领取   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值