pytest 执行远程通过ssh接口获取lspci |grep - i eth 网卡接口 lspci -s 网卡接口 -vvv |grep -i width 并生成一份report.
时间: 2024-02-25 14:53:28 浏览: 30
可以通过paramiko模块实现ssh连接远程机器执行命令,并通过pytest生成测试报告。以下是一个示例:
1. 安装必要的Python模块
```shell
pip install paramiko pytest-html
```
2. 创建一个pytest测试文件,比如`test_network.py`
```python
import paramiko
import pytest
@pytest.fixture(scope="module")
def ssh_client():
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('remote_host_ip', username='username', password='password')
yield ssh
ssh.close()
def test_network_interface(ssh_client):
stdin, stdout, stderr = ssh_client.exec_command('lspci | grep -i eth')
interface = stdout.read().decode().split()[0]
stdin, stdout, stderr = ssh_client.exec_command(f'lspci -s {interface} -vvv | grep -i width')
width = stdout.read().decode().strip()
assert 'x8' in width, f'Network interface {interface} width is not x8'
```
3. 在命令行中执行pytest命令并生成HTML测试报告
```shell
pytest --html=report.html -v
```
其中,`--html`选项指定生成HTML格式的测试报告,`-v`选项表示输出详细信息。执行完毕后,将生成一个名为`report.html`的测试报告文件。