xqqlyx

pytest测试range内置函数
点击查看代码
import pytest

# ------------------------------
# 一、基础操作:正序序列测试用例
# ------------------------------
@pytest.mark.parametrize(
    "start, end, step, expected",
    [
        # 默认步长(step=1)
        (None, 5, 1, [0, 1, 2, 3, 4]),  # 省略start,从0开始
        (1, 6, 1, [1, 2, 3, 4, 5]),     # 明确start和end
        # 列表索引对齐
        (0, len(["a", "b", "c"]), 1, [0, 1, 2]),  # 匹配列表长度的索引
        # 自定义正步长(间隔生成)
        (0, 10, 2, [0, 2, 4, 6, 8]),    # 步长2:偶数序列
        (3, 15, 3, [3, 6, 9, 12]),      # 步长3:3的倍数序列
        (1, 20, 5, [1, 6, 11, 16]),     # 步长5:间隔5递增
    ],
)
def test_range_forward(start, end, step, expected):
    """测试正序序列生成(start ≤ end,step为正)"""
    # 处理省略start的情况(range默认start=0)
    if start is None:
        result = list(range(end))
    else:
        result = list(range(start, end, step))
    assert result == expected


# ------------------------------
# 二、进阶操作:倒序序列测试用例
# ------------------------------
@pytest.mark.parametrize(
    "start, end, step, expected",
    [
        # 基础倒序(step=-1)
        (10, 0, -1, list(range(10, 0, -1))),  # 10→1:完整倒序
        (5, -1, -1, [5, 4, 3, 2, 1, 0]),     # 5→0:包含0
        # 自定义倒序步长(间隔递减)
        (10, -1, -2, [10, 8, 6, 4, 2, 0]),   # 步长-2:偶数倒序
        (15, 2, -3, [15, 12, 9, 6, 3]),      # 步长-3:3的倍数倒序
        (7, 0, -4, [7, 3]),                  # 步长-4:大幅递减
    ],
)
def test_range_backward(start, end, step, expected):
    """测试倒序序列生成(start > end,step为负)"""
    result = list(range(start, end, step))
    assert result == expected


# ------------------------------
# 三、边界场景:特殊序列测试用例
# ------------------------------
@pytest.mark.parametrize(
    "start, end, step, expected",
    [
        # 空序列场景
        (3, 1, 1, []),    # 正序但start>end
        (1, 3, -1, []),   # 倒序但start<end
        (5, 5, 1, []),    # start=end(左闭右开,无元素)
        (0, 0, -1, []),   # 倒序时start=end
        # 单元素序列
        (5, 6, 1, [5]),   # 正序:end=start+step
        (8, 7, -1, [8]),  # 倒序:end=start+step
        # 对齐固定长度(指定元素个数)
        (0, 10, 2, [0, 2, 4, 6, 8]),      # 5个元素(正序)
        (12, 0, -3, [12, 9, 6, 3]),       # 4个元素(倒序)
    ],
)
def test_range_boundary(start, end, step, expected):
    """测试边界场景:空序列、单元素序列、固定长度序列"""
    result = list(range(start, end, step))
    assert result == expected


# ------------------------------
# 四、实用场景:结合其他函数测试用例
# ------------------------------
def test_range_with_map():
    """测试range结合map生成规律列表(平方序列)"""
    result = list(map(lambda x: x*x, range(1, 11)))
    expected = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
    assert result == expected


def test_range_with_filter():
    """测试range结合filter生成筛选列表(偶数序列)"""
    result = list(filter(lambda x: x%2 == 0, range(10)))
    expected = [0, 2, 4, 6, 8]
    assert result == expected


def test_range_reverse_traverse():
    """测试range用于反向遍历(列表/字符串)"""
    # 反向遍历列表
    lst = ["a", "b", "c", "d"]
    reverse_lst = [lst[i] for i in range(len(lst)-1, -1, -1)]
    assert reverse_lst == ["d", "c", "b", "a"]

    # 反向遍历字符串
    s = "hello"
    reverse_s = "".join([s[i] for i in range(len(s)-1, -1, -1)])
    assert reverse_s == "olleh"


def test_range_matrix_index():
    """测试range生成多维度矩阵索引"""
    # 生成2行3列的索引对
    indices = []
    for i in range(2):
        for j in range(3):
            indices.append((i, j))
    expected = [(0,0), (0,1), (0,2), (1,0), (1,1), (1,2)]
    assert indices == expected


# ------------------------------
# 五、局限性测试(验证不支持的场景)
# ------------------------------
def test_range_float_error():
    """测试range不支持浮点数参数(应抛出TypeError)"""
    with pytest.raises(TypeError):
        range(0.1, 1.0, 0.2)  # 浮点数start/end/step不合法


def test_range_zero_step_error():
    """测试range步长为0(应抛出ValueError)"""
    with pytest.raises(ValueError):
        range(1, 5, 0)  # step=0不允许

posted on 2025-11-16 15:56  烫烫烫烫热  阅读(0)  评论(0)    收藏  举报