SONiC-mgmt系列3:编写测试用例代码

参考tests目录下用例,根据几个常见基础场景生成测试用例并进行注释: 端口Ethernet0 up ,光模块在位、 收光功率 大于10dbm 。 实际工作中这3个场景应该放到3个不同的py 文件,但本文为了便于展示就放在一个文件中。

脚本的核心逻辑是:

  1. tests/platformtests/cli 封装show命令
  2. 解析上一步show命令的输出,获取用于下一步断言判断的值
  3. 断言判断
python 复制代码
# 导入日志模块(sonic-mgmt 官方脚本必备)
import logging
# 导入正则表达式模块,用于解析命令输出
import re
# 导入 pytest 测试框架
import pytest

# 导入 sonic-mgmt 官方输出解析工具(和 test_sfpshow.py 一致)
from .util import parse_output

# 导入 sonic-mgmt 官方断言函数
from tests.common.helpers.assertions import pytest_assert


# 官方脚本固定的日志配置
logger = logging.getLogger(__name__)

# SONiC 交换机的CLI 命令进行封装
CMD_INTERFACE_STATUS = "show interface status Ethernet0"  # 查接口状态
CMD_SFP_PRESENCE   = "show interface transceiver presence"  # 查模块在位
CMD_SFP_DOM        = "show interface transceiver eeprom --dom"  # 查 DOM 诊断

# ==============================
# pytest 标记:
# topology('any'):任何拓扑都能跑
# device_type('hw'):只在物理机(hardware)上跑
# ==============================
pytestmark = [
    pytest.mark.topology('any'),
    pytest.mark.device_type('hw')  # 物理机测试
]

def test_ethernet0_sfp_present_up_rx(duthosts, enum_frontend_dut):
    """
    验证 Ethernet0 端口:
    1. 接口状态 Up
    2. 光模块在位
    3. 收光功率 > 10 dBm
    """
    # 获取 DUT 交换机对象
    duthost = enum_frontend_dut
    test_port = "Ethernet0"

    # ==================================================
    # 步骤1:执行命令,获取接口状态
    # ==================================================
    logger.info("=== 检查接口 %s 状态 ===" % test_port)
    intf_output = duthost.command(CMD_INTERFACE_STATUS)["stdout"]

    # 用官方 parse_output 方法解析输出
    intf_table = parse_output(intf_output)
    port_info = intf_table.get(test_port, None)

    pytest_assert(port_info is not None, "端口 {} 不存在".format(test_port))

    # 断言接口 oper status 为 up
    oper_status = port_info.get("oper", "").lower()
    pytest_assert(
        oper_status == "up",
        "{} 接口未UP,当前状态:{}".format(test_port, oper_status)
    )

    # ==================================================
    # 步骤2:检查光模块在位
    # ==================================================
    logger.info("=== 检查光模块在位 ===")
    presence_output = duthost.command(CMD_SFP_PRESENCE)["stdout"]

    presence_table = parse_output(presence_output)
    port_present = presence_table.get(test_port, None)

    pytest_assert(port_present is not None, "{} 无模块信息".format(test_port))

    present = port_present.get("presence", "").lower()
    pytest_assert(
        present == "present",
        "{} 光模块不在位!".format(test_port)
    )

    # ==================================================
    # 步骤3:检查 Rx 收光功率 > 10 dBm
    # ==================================================
    logger.info("=== 检查收光功率 Rx Power ===")
    dom_output = duthost.command(CMD_SFP_DOM)["stdout"]

    # 匹配收光功率(正则:官方 test_sfpshow.py 常用方式)
    rx_pattern = r"{}[\s\S]*?Rx Power\s+:\s+([\d\-.]+)\s+dBm".format(test_port)
    match = re.search(rx_pattern, dom_output)

    pytest_assert(match, "{} 未获取到 Rx Power".format(test_port))

    rx_power = float(match.group(1))
    pytest_assert(
        rx_power > 10.0,
        "{} 收光功率过低:{} dBm,要求 > 10 dBm".format(test_port, rx_power)
    )

    logger.info(" 所有检查通过:接口UP、模块在位、Rx Power 满足要求")
相关推荐
2302_805771071 天前
Day24:DDT+EXCEL数据驱动
功能测试·单元测试·自动化·测试用例·需求分析·web自动化
TomEval2 天前
【测AI】第10篇:AI 模型功能测试实战 —— 测试用例怎么设计?
人工智能·python·功能测试·测试用例
j7~3 天前
【软件测试】(用例篇)
软件测试·测试用例·测试用例的设计方法·场景设计·错误猜测法·测试用例的粒度和评价
2302_805771073 天前
Day20:实战:接口自动化测试框架封装之一------接口关联封装
功能测试·自动化·测试用例
霍格沃兹测试学院-小舟畅学3 天前
AI 智能化测试:从测试用例生成到自动执行的工程实践
人工智能·测试用例
测试者家园14 天前
不写测试用例也能做自动化测试?
自动化测试·软件测试·人工智能·测试用例·智能化测试
测试员周周14 天前
【jev应用到测试】94 条 UI 报错交给 Jev,三档判对 85.1%,开单闸门定在 0.6
人工智能·python·测试工具·单元测试·测试用例
zhangduang_KHKW14 天前
ISO 26262功能安全标准解析与测试用例设计
测试用例
jingli917 天前
一个测试用例把连接池卡死 30 秒:SQLite 连接池 max_size=1 下的重入自锁复盘
jvm·sqlite·测试用例
啄缘之间24 天前
1.【SVA】什么是SVA?
经验分享·学习·测试用例·verilog·sva