使用pytest-xdist运行测试用例,执行命令 pytest -n 2,执行时只有一个进程在执行。Python的版本:3.9.6,pytest-xdist插件版本:2.5.0。求解答
执行命令
PS D:\daima-tool> pytest -n 2 .\test_case.py
执行的结果
platform win32 -- Python 3.9.6, pytest-7.0.1, pluggy-1.0.0 -- D:\Python\python39.exe
cachedir: .pytest_cache
metadata: {'Python': '3.9.6', 'Platform': 'Windows-10-10.0.19045-SP0', 'Packages': {'pytest': '7.0.1', 'py': '1.11.0', 'pluggy': '1.0.0'}, 'Plugins': {'allure-pytest'
: '2.9.45', 'forked': '1.4.0', 'html': '3.1.1', 'metadata': '2.0.1', 'ordering': '0.6', 'rerunfailures': '10.2', 'xdist': '2.5.0'}, 'JAVA_HOME': 'D:\\java'}
rootdir: D:\daima-tool\controller-test, configfile: pytest.ini
plugins: allure-pytest-2.9.45, forked-1.4.0, html-3.1.1, metadata-2.0.1, ordering-0.6, rerunfailures-10.2, xdist-2.5.0
[gw0] win32 Python 3.9.6 cwd: D:\daima-tool\controller-test
[gw1] win32 Python 3.9.6 cwd: D:\daima-tool\controller-test
[gw0] Python 3.9.6 (tags/v3.9.6:db3ff76, Jun 28 2021, 15:26:21) [MSC v.1929 64 bit (AMD64)]
[gw1] Python 3.9.6 (tags/v3.9.6:db3ff76, Jun 28 2021, 15:26:21) [MSC v.1929 64 bit (AMD64)]
gw0 [2551] / gw1 [2551]
scheduling tests via LoadScopeScheduling
test_case.py::Test::test_voice[case0]
[gw0] [ 0%] FAILED test_case.py::Test::test_voice[case0]
test_case.py::Test::test_voice[case1]
[gw0] [ 0%] PASSED test_case.py::Test::test_voice[case1]
test_case.py::Test::test_voice[case2]
[gw0] [ 0%] FAILED test_case.py::Test::test_voice[case2]
test_case.py::Test::test_voice[case3]
[gw0] [ 0%] PASSED test_case.py::Test::test_voice[case3]
test_case.py::Test::test_voice[case4]
[gw0] [ 0%] PASSED test_case.py::Test::test_voice[case4]
test_case.py::Test::test_voice[case5]
[gw0] [ 0%] PASSED test_case.py::Test::test_voice[case5]
test_case.py::Test::test_voice[case6]
[gw0] [ 0%] PASSED test_case.py::Test::test_voice[case6]
可能是因为您的运行环境中有多个Python解释器,而pytest-xdist默认只在主解释器上运行测试用例,导致无法启动其他进程。
你可以尝试使用Python解释器来做下指定,可以参考:
pytest -n 2 --python=python3 test_case.py
其中,--python
选项指定要使用的Python解释器,您需要将其替换为您实际的Python解释器名称或路径。另外,您还可以尝试使用-vv
选项来查看更详细的运行过程和输出信息。
如果仍然无法解决问题,您可以尝试升级pytest-xdist插件版本或者重新安装该插件,以确保它与您的Python解释器版本兼容。
还有问题的话再问我好了
该回答通过自己思路及引用到GPTᴼᴾᴱᴺᴬᴵ搜索,得到内容具体如下:
根据您提供的信息,pytest-xdist在运行测试用例时只使用了一个进程,而不是两个进程。这可能是由于以下原因之一:
系统资源不足:如果系统资源不足,pytest-xdist可能无法启动第二个进程。确保您的计算机具有足够的RAM和CPU资源。
pytest-xdist版本不兼容:请确保您的pytest-xdist插件版本与您的pytest版本兼容。建议使用最新版本的pytest和pytest-xdist插件。
测试脚本不支持并行执行:如果测试脚本中存在共享资源或其他并发问题,pytest-xdist可能无法正确执行测试用例并导致进程阻塞。请检查测试用例是否支持并行执行。
解决方法:
1、 检查系统资源:确保您的计算机具有足够的RAM和CPU资源,并关闭其他应用程序以释放系统资源。
2、 更新pytest-xdist插件:在命令行中使用以下命令更新pytest-xdist插件:
pip install --upgrade pytest-xdist
3、 检查测试脚本:确保测试脚本支持并行执行,并解决任何可能导致进程阻塞或共享资源问题的代码。
如果以上方法无法解决问题,请提供更多信息,例如pytest和pytest-xdist的版本,以便更好地理解问题的来源。
如果以上回答对您有所帮助,点击一下采纳该答案~谢谢