将服务器/网卡测试命令文件转换为 pytest 测试用例。 每个 txt 文件生成一个对应的 pytest 文件。
将待转换的 txt 命令文件放到目录:
text
/home/admin/.openclaw/tytest/txt_contents/txt/*.txt
bash
python3 /home/admin/.openclaw/workspace/skills/server-test-converter/convert_commands.py
转换后的文件:
text
/home/admin/.openclaw/tytest/
├── test_framework.py # 通用框架(需要配置)
├── test_xxx.py # 生成的测试用例
└── txt_contents/ # 原始命令文件
生成的测试代码依赖 test_framework.py,使用方式:
python
from test_framework import send_r5, send_host, send_r5_wait, send_host_wait, tl_log, TARGET, TARGET_R5
复制到测试服务器: 将 test_framework.py 和 test_xxx.py 复制到测试服务器
实现 send_a_cmd(): 根据你的测试环境实现命令执行逻辑
python
# 方式一: SSH
import paramiko
def send_a_cmd(cmd, target):
ssh = paramiko.SSHClient()
ssh.connect(hostname='192.168.1.100', username='admin', password='xxx')
stdin, stdout, stderr = ssh.exec_command(cmd)
return stdout.read()
# 方式二: 调用已有框架
from your_framework import execute_cmd
def send_a_cmd(cmd, target):
return execute_cmd(cmd, target)
# 方式三: 本地执行
import subprocess
def send_a_cmd(cmd, target):
result = subprocess.run(cmd, shell=True, capture_output=True)
return result.stdout
配置目标设备: 修改 TARGET 和 TARGET_R5 的值
运行测试:
bash
pip install pytest
pytest test_xxx.py -v
命令类型示例执行方式内存操作md, mwsend_r5_wait调度器txsch_test_*, dmif_txsch_*send_r5_waitDFX诊断dmif_eoc_*, *_show_dfxsend_r5_wait网卡命令ice_*, test_*, nicif_*send_r5_wait其他Shell 命令等send_host_wait
python
#!/usr/bin/env python3
"""
自动生成的测试用例: xxx.txt
"""
import pytest
import sys
import os
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from test_framework import send_r5, send_host, send_r5_wait, send_host_wait, tl_log, TARGET, TARGET_R5
class TestXxx:
def test_all_commands(self, env):
"""执行所有命令"""
commands = ['命令1', '命令2']
for cmd in commands:
send_r5_wait(TARGET_R5, cmd)
send_a_cmd() 函数TARGET 和 TARGET_R5