pytest之fixture和mark参数化测试

fixture和mark参数化测试区别:

fixture参数化运用于多个测试用例

mark参数化运用化单个测试用例

======================================

fixture支持参数化

params 所有的数据
param 本次测试用到的数据
 
import pytest


@pytest.fixture(scope="function",
                params=[
                    ("abc", "123"),
                    ("cdf", "456"),
                ]
                )
def _test_data(request):
    # 此处可以写代码
    return request.param


def test_01_yiyi(_test_data):
    a, b = _test_data  # 拆包
    print(a + "" + b)

运行结果

 更多

import time

import pytest


class TestApi:
    @pytest.mark.parametrize('a', ['1', '2'])  # 列表
    def test_huahua(self, a):
        time.sleep(5)
        print(a)

    # 跟unittest的ddt里面的@unpack解包的一样
    @pytest.mark.parametrize('a', [['1', '2'], ['11', '22']])
    def test_huahua1(self, a):
        time.sleep(5)
        print(a)

    @pytest.mark.parametrize('a', ('1', '2'))  # 元组
    def test_huahua2(self, a):
        time.sleep(5)
        print(a)

    @pytest.mark.parametrize('a',  [{'huahua', '4'}, {'caocao', '6'}])  # 字典列表
    def test_huahua3(self, a):
        time.sleep(5)
        print(a)

    @pytest.mark.parametrize('a', ({'huahua', '88'}, {'caocao', '66'}))  # 字典元组
    def test_huahua4(self, a):
        time.sleep(5)
        print(a)

    @pytest.mark.parametrize('a, b', [['', '4'], ['', '6']])  # 列表
    def test_chen(self, a, b):
        print(a, b)

    @pytest.mark.parametrize('a, b', [{'hua', '4'}, {'cao', '6'}, {'yun', '8'}])  # 字典列表
    def test_chen1(self, a, b):
        print(a, b)


if __name__ == '__main__':
    pytest.main(['-vs'])

 

 

=============================================================

@pytest.mark.parametrize
@pytest.mark.parametrize("name", [1, 2, 3, 'haha', 5, 6])
def test_mark(name):
    print(name)

 

运行结果:

 

@pytest.mark.parametrize("name, pp", [(1, 2), ('1', 'may'), (3, 'haha'), (5, 6)])
def test_mark(name, pp):
    print(name)
    print(pp)

 

posted @ 2023-09-20 22:47  yimu-yimu  阅读(63)  评论(0编辑  收藏  举报