python-pytest用例参数化

  1. 用例参数化
    1. @pytest.mark.parametrize
  2. json文件参数化
  3. csv文件参数化
  4. yaml文件参数化

用例参数化

@pytest.mark.parametrize

  • 传递单个参数
  • 传递多个参数
import pytest

# 传递单个参数num
num_list=[1,2,3]
@pytest.mark.parametrize("num", num_list)
def test_simple(num):
    assert num in num_list


def add(x, y):
    result = x + y
    return result


# 传递多个参数:x, y, expected
# 参数化的名字argname,要与方法中的参数名,一一对应
# 如果传递多个参数,要放在列表中,列表中嵌套列表/元组
# ids 参数用于用例名称重命名,参数数量要与argvalues用例条目一致
@pytest.mark.parametrize(
    "x,y,expected",
    [(1, 2, 3), (1, 2.5, 3.5)],
    ids=["整数相加", "整数与浮点数相加"]
)
def test_add(x, y, expected):
    assert add(x, y) == expected

执行用例 pytest test_sample.py -vs 后,执行日志如下:

================================================================================ test session starts ================================================================================
collected 5 items                                                                                                                                                                    

test_sample.py::test_simple[1] PASSED
test_sample.py::test_simple[2] PASSED
test_sample.py::test_simple[3] PASSED
test_sample.py::test_add[\u6574\u6570\u76f8\u52a0] PASSED
test_sample.py::test_add[\u6574\u6570\u4e0e\u6d6e\u70b9\u6570\u76f8\u52a0] PASSED

================================================================================= 5 passed in 0.07s =================================================================================

通过日志可见,用例根据参数化,test_simple执行了3次。test_add 用例执行了2次。

其中可注意到,test_add 用例名称中文输出为unicode格式,可通过pytest hook 函数进行格式转换,如下

conftest.py:

def pytest_collection_modifyitems(items):
    """
    测试用例收集完成时,将收集到的item的name和nodeid中文显示
    :param items:
    :return:
    """

    for item in items:
        item.name = item.name.encode("utf-8").decode("unicode_escape")
        item._nodeid = item.nodeid.encode("utf-8").decode("unicode_escape")

重新运行用例 pytest test_sample.py -vs 后,可见日志如下:

test_sample.py::test_simple[1] PASSED
test_sample.py::test_simple[2] PASSED
test_sample.py::test_simple[3] PASSED
test_sample.py::test_add[整数相加] PASSED
test_sample.py::test_add[整数与浮点数相加] PASSED

json文件参数化

data.json:

{
  "case1":[1,2,3],
  "case2":[1,2.5,3.5]
}

test_sample.py:

import pytest
import json

def add(x, y):
    result = x + y
    return result


def get_json():
    """
    通过json文件返回参数列表
    :return: [[1,2,3],[1,2.5,3.5]]
    """
    ret=[]
    with open("data.json","r") as fp:
        data = json.load(fp)
        for item in data.values():
            ret.append(item)

    return ret


@pytest.mark.parametrize(
    "x,y,expected",
    get_json(),
    ids=["整数相加", "整数与浮点数相加"]
)
def test_add(x, y, expected):
    assert add(float(x), float(y)) == float(expected)

csv文件参数化

data.csv:

1,2,3
1,2.5,3.5

test_sample.py

import pytest
import csv

def add(x, y):
    result = x + y
    return result


def get_csv():
    """
    通过csv文件返回参数列表
    :return: [[1,2,3],[1,2.5,3.5]]
    """
    ret=[]
    with open("data.csv","r") as fp:
        raw = csv.reader(fp)
        for line in raw:
            ret.append(line)

    return ret


@pytest.mark.parametrize(
    "x,y,expected",
    get_csv(),
    ids=["整数相加", "整数与浮点数相加"]
)
def test_add(x, y, expected):
    assert add(float(x), float(y)) == float(expected)

yaml文件参数化

data.yaml:

case_add:
  - [1,2,3]
  - [1,2.5,3.5]

test_sample.py:

import pytest
import yaml

def add(x, y):
    result = x + y
    return result


def get_yml(case_name):
    """
    通过yml文件返回参数列表
    :return: [[1,2,3],[1,2.5,3.5]]
    """
    data={}
    with open("data.yml","r") as fp:
        data = yaml.safe_load(fp)
    return data.get(case_name)


@pytest.mark.parametrize(
    "x,y,expected",
    get_yml("case_add"),
    ids=["整数相加", "整数与浮点数相加"]
)
def test_add(x, y, expected):
    assert add(float(x), float(y)) == float(expected)

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。
My Show My Code