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

相关推荐
yongui478341 天前
基于MATLAB的8QAM调制解调仿真与BER性能分析
开发语言·matlab
Blossom.1181 天前
AI“点亮”萤火虫:边缘机器学习让微光成像走进4K时代
人工智能·pytorch·python·深度学习·数码相机·opencv·机器学习
早日退休!!!1 天前
C 内存布局
c语言·开发语言
linuxoffer1 天前
composer 安装与开启PHP扩展支持
开发语言·php·composer
come112341 天前
Go 和云原生 的现状和发展前景
开发语言·云原生·golang
专家大圣1 天前
Bililive-go+cpolar:跨平台直播录制的远程管理方案
开发语言·网络·后端·golang·内网穿透·设计工具
Eiceblue1 天前
Python 将 HTML 转换为纯文本 TXT (HTML 文本提取)
开发语言·vscode·python·html
跟橙姐学代码1 天前
给Python项目加个“隔离间”,从此告别依赖纠缠!
前端·python·ipython
张人玉1 天前
C# TCP - 串口转发
开发语言·tcp/ip·c#