Python 在远程计算机上运行测试并使用pytest捕获输出

Python 在远程计算机上运行测试并使用pytest捕获输出,python,pytest-xdist,Python,Pytest Xdist,如果我在本地机器上执行测试,我可以看到来自测试用例的控制台输出。但是,在使用pytest在远程计算机上运行测试时,不会捕获控制台输出。下面是示例测试用例- 测试样本.py import os import pytest @pytest.mark.set1 def test_command(): output = os.popen("ifconfig") print(output.read()) pytest.ini [pytest] markers =

如果我在本地机器上执行测试,我可以看到来自测试用例的控制台输出。但是,在使用pytest在远程计算机上运行测试时,不会捕获控制台输出。下面是示例测试用例-

测试样本.py

import os
import pytest

@pytest.mark.set1
def test_command():
    output = os.popen("ifconfig")
    print(output.read())
pytest.ini

[pytest]
markers =
    set1: mark a test as a set1.
    set2: mark a test as a set2.
~/.ssh/config

Host ubuntu
    HostName 10.203.114.68
    User root
    Port 22
    IdentityFile ~/.ssh/id_rsa
Pytest命令在本地VM上执行测试

py.test -s -m set1 -k test_command -v 控制台输出

========================================================================================================= test session starts ========================================================================================================= platform linux -- Python 3.6.12, pytest-6.0.2, py-1.9.0, pluggy-0.13.1 -- /usr/bin/python3.6 cachedir: .pytest_cache metadata: {'Python': '3.6.12', 'Platform': 'Linux-4.4.0-142-generic-x86_64-with-Ubuntu-16.04-xenial', 'Packages': {'pytest': '6.0.2', 'py': '1.9.0', 'pluggy': '0.13.1'}, 'Plugins': {'html': '2.1.1', 'forked': '1.3.0', 'xdist': '2.1.0', 'metadata': '1.11.0'}} rootdir: /home/kulkarniabhi/pytaf, configfile: pytest.ini plugins: html-2.1.1, forked-1.3.0, xdist-2.1.0, metadata-1.11.0 collected 19 items / 18 deselected / 1 selected test_demo5.py::test_command ens160 Link encap:Ethernet HWaddr 00:50:56:93:9a:1d inet addr:10.198.36.31 Bcast:10.198.39.255 Mask:255.255.252.0 inet6 addr: fe80::250:56ff:fe93:9a1d/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:1748980902 errors:0 dropped:5419 overruns:0 frame:0 TX packets:54277009 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:252392842770 (252.3 GB) TX bytes:101276948055 (101.2 GB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:5683112 errors:0 dropped:0 overruns:0 frame:0 TX packets:5683112 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1 RX bytes:1720657136 (1.7 GB) TX bytes:1720657136 (1.7 GB) PASSED ================================================================================================== 1 passed, 18 deselected in 0.03s =================================================================================================== ========================================================================================================= test session starts ========================================================================================================= platform linux -- Python 3.6.12, pytest-6.0.2, py-1.9.0, pluggy-0.13.1 -- /usr/bin/python3.6 cachedir: .pytest_cache metadata: {'Python': '3.6.12', 'Platform': 'Linux-4.4.0-142-generic-x86_64-with-Ubuntu-16.04-xenial', 'Packages': {'pytest': '6.0.2', 'py': '1.9.0', 'pluggy': '0.13.1'}, 'Plugins': {'html': '2.1.1', 'forked': '1.3.0', 'xdist': '2.1.0', 'metadata': '1.11.0'}} rootdir: /home/kulkarniabhi/pytaf, configfile: pytest.ini plugins: html-2.1.1, forked-1.3.0, xdist-2.1.0, metadata-1.11.0 gw0 Iroot@10.203.114.68's password: ********** [gw0] linux Python 3.8.5 cwd: /home/bit9qa/pyexecnetcache [gw0] Python 3.8.5 (default, Jul 28 2020, 12:59:40) -- [GCC 9.3.0] gw0 [1] scheduling tests via LoadScheduling test_demo5.py::test_command [gw0] PASSED test_demo5.py::test_command ========================================================================================================== 1 passed in 8.04s ========================================================================================================= ==================================================================================================================================================================测试会话开始========================================================================================================= 平台linux——Python 3.6.12、pytest-6.0.2、py-1.9.0、pluggy-0.13.1--/usr/bin/python3.6 cachedir:.pytest\u缓存 元数据:{'Python':'3.6.12','Platform':'Linux-4.4.0-142-generic-x86_64-with-Ubuntu-16.04-xenial','Packages':{'pytest':'6.0.2','py':'1.9.0','pluggy':'0.13.1','Plugins':{'html':'2.1.1.1','forked','1.3.0','xdist':'2.1.1.1.0',' rootdir:/home/kulkarniabhi/pytaf,配置文件:pytest.ini 插件:html-2.1.1、forked-1.3.0、xdist-2.1.0、metadata-1.11.0 gw0Iroot@10.203.114.68的密码:********** [gw0]linux Python 3.8.5 cwd:/home/bit9qa/pyexecnetcache [gw0]Python 3.8.5(默认值,2020年7月28日,12:59:40)--[GCC 9.3.0] gw0[1] 通过LoadScheduling安排测试 test_demo5.py::test_命令 [gw0]通过了test\u demo5.py::test\u命令 =====================================================================================================================================================================================1以8.04秒通过========================================================================================================= **本地和远程vm上的My pytest版本==>**

[root@localhost]$ python3.6 -m py.test --version pytest 6.0.2 [root@localhost]$python3.6-m py.test--版本 pytest 6.0.2 我已经验证了最新的pytest xdist支持--capture/-s选项

无需应用以下变通方法

[root@localhost]$ python3.6 -m py.test --version pytest 6.0.2