一、遇到的问题
python代码调用命令行 allure命令报错,提示找不到allure这个命令。python虚拟环境中已经安装了pytest-allure
代码如下:
python
import subprocess
if __name__ == "__main__":
# retcode = pytest.main()
subprocess.call(['pytest', '--alluredir=./report/allure'])
subprocess.call(['allure', 'serve', './report/allure'])
报错如下:
python
Traceback (most recent call last):
File "D:\project\python\pytestDemo\main.py", line 17, in <module>
subprocess.call(['allure', 'serve', './report/allure'])
File "C:\Users\liyuanhong\AppData\Local\Programs\Python\Python311\Lib\subprocess.py", line 389, in call
with Popen(*popenargs, **kwargs) as p:
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\liyuanhong\AppData\Local\Programs\Python\Python311\Lib\subprocess.py", line 1026, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Users\liyuanhong\AppData\Local\Programs\Python\Python311\Lib\subprocess.py", line 1538, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [WinError 2] 系统找不到指定的文件。
查看了我的python虚拟环境: venv/Scripts 目录下确实没有 allure 命令
二、解决方法
下载 allure 命令行工具
下载地址:Central Repository: io/qameta/allure/allure-commandline
选择对应的版本下载,解压到项目目录如下:
创建一个 allure.bat的超链接,将超链接文件拷贝到 venv/Scripts 目录下
修改代码如下即可:
python
import subprocess
if __name__ == "__main__":
# retcode = pytest.main()
subprocess.call(['pytest', '--alluredir=./report/allure'])
subprocess.call(['allure.bat', 'serve', './report/allure'])