一、遇到的问题

python代码调用命令行 allure命令报错,提示找不到allure这个命令。python虚拟环境中已经安装了pytest-allure

代码如下:

import subprocess

if __name__ == "__main__":
    # retcode = pytest.main()

    subprocess.call(['pytest', '--alluredir=./report/allure'])
    subprocess.call(['allure', 'serve', './report/allure'])

报错如下:

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 目录下

修改代码如下即可:

import subprocess

if __name__ == "__main__":
    # retcode = pytest.main()

    subprocess.call(['pytest', '--alluredir=./report/allure'])
    subprocess.call(['allure.bat', 'serve', './report/allure'])

Logo

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。

更多推荐