Pytest10 :Fixture的参数化

以前介绍过测试函数的参数化,fixture 本身也是函数 也可以进行参数化.
比如我们想用不同的用户进行登录,就可以用这个特性, 进行登录初始化.

对测试函数进行参数化处理,可以多次运行的只是该测试函数,而使用参数化fixture
,每个使用该fixture的测试函数都可以被运行多次,fixture 的这个特性非常强大.

直接看例子 :

import pytest

data=[1,2,4]
@pytest.fixture(params=data)
def class_scope(request):
    return  request.param+1

def test_01(class_scope):
    print(class_scope)
    assert class_scope==2

打印结果 被认为三个方法 :

platform win32 -- Python 3.6.6, pytest-4.0.0, py-1.7.0, pluggy-0.8.0
rootdir: D:\tasks_proj\tests, inifile: pytest.inicollected 3 items

test_add.py .2
F3

func\test_add.py:7 (test_01[2])
2 != 3

Expected :3
Actual   :2
 <Click to see difference>

class_scope = 3

    def test_01(class_scope):
        print(class_scope)
>       assert class_scope==2
E       assert 3 == 2

test_add.py:10: AssertionError
F5

func\test_add.py:7 (test_01[4])
2 != 5

Expected :5
Actual   :2
 <Click to see difference>

class_scope = 5

    def test_01(class_scope):
        print(class_scope)
>       assert class_scope==2
E       assert 5 == 2

test_add.py:10: AssertionError
                                                          [100%]

================================== FAILURES ===================================
_________________________________ test_01[2] __________________________________

class_scope = 3

    def test_01(class_scope):
        print(class_scope)
>       assert class_scope==2
E       assert 3 == 2

test_add.py:10: AssertionError
---------------------------- Captured stdout call -----------------------------
3
_________________________________ test_01[4] __________________________________

class_scope = 5

    def test_01(class_scope):
        print(class_scope)
>       assert class_scope==2
E       assert 5 == 2

test_add.py:10: AssertionError
---------------------------- Captured stdout call -----------------------------
5
===================== 2 failed, 1 passed in 0.09 seconds ======================
Process finished with exit code 0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值