pytest下allure

python 复制代码
import pytest


def test_case01():
    '''用例01~'''
    print('用例01')


class Test_mokuai01:
    def test_case02(self):
        '''用例02~'''
        print('用例02')


if __name__=='__main__':
    #pytest.main(['-vs','test_sample-2.py'])
    pytest.main(['-vs','test_sample-2.py','--alluredir','./result2'])#生成allure报告,参数写法
    #命令行cmd下执行命令:allure generate ./result2 -o ./report # -o就是输出的意思(out)
    # 解释:从生成结果的/result2文件下,输出报告到./report下(先生成测试结果,再输出allure报告)

命令写错会报如下错误,我当时找了好久才发现原因
ERROR: usage: test_sample-2.py [options] [file_or_dir] [file_or_dir] [...]

test_sample-2.py: error: unrecognized arguments: --allure-dir E:/pythonXM/test/result2

inifile: None

rootdir: E:\pythonXM\test\allure

.\result2 does not exist

Report successfully generated to .\report2

cmd下执行命令:allure generate ./result2 -o ./report,如果提示:.\result2 does not exists

Report successfully generated to .\report

说明没找到result2文件夹,需要手动去创建一个

优化后:

python 复制代码
import pytest
import allure
import os

def test_case01():
    '''用例01~'''
    print('用例01')
    return '用例01**'

class Test_mokuai01:
    def test_case02(self):
        '''用例02~'''
        print('用例02')
        return '用例02**'
if __name__ == '__main__':
    # 生成测试结果
    pytest.main(['-vs', 'test_sample-2.py', '--alluredir', './result2'])
    # 生成 HTML 报告
    os.system('allure generate ./result2 -o ./report2 --clean')
    # 打开 HTML 报告(Windows 系统)
    os.system('allure open ./report2')

说明

  • os.system('allure generate ./result2 -o ./report2 --clean'):调用 allure generate 命令将 ./result2 目录下的测试结果文件转换为 HTML 报告,并存储在 ./report2 目录中,--clean 表示在生成报告前先清空目标目录。
  • os.system('allure open ./report2'):调用 allure open 命令在默认浏览器中打开生成的 HTML 报告(此命令在 Windows 系统下有效,在 Linux 或 macOS 系统下可能需要适当调整)

最后附上cmd 命令行命令:

pytest -vs --alluredir=./result/ --clean-alluredir

allure generate ./result2 -o ./report2 --clean

相关推荐
IVEN_14 小时前
只会Python皮毛?深入理解这几点,轻松进阶全栈开发
python·全栈
Ray Liang16 小时前
用六边形架构与整洁架构对比是伪命题?
java·python·c#·架构设计
AI攻城狮16 小时前
如何给 AI Agent 做"断舍离":OpenClaw Session 自动清理实践
python
千寻girling16 小时前
一份不可多得的 《 Python 》语言教程
人工智能·后端·python
AI攻城狮19 小时前
用 Playwright 实现博客一键发布到稀土掘金
python·自动化运维
曲幽19 小时前
FastAPI分布式系统实战:拆解分布式系统中常见问题及解决方案
redis·python·fastapi·web·httpx·lock·asyncio
孟健1 天前
Karpathy 用 200 行纯 Python 从零实现 GPT:代码逐行解析
python
码路飞2 天前
写了个 AI 聊天页面,被 5 种流式格式折腾了一整天 😭
javascript·python
曲幽2 天前
FastAPI压力测试实战:Locust模拟真实用户并发及优化建议
python·fastapi·web·locust·asyncio·test·uvicorn·workers