You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Allure Report中保留历史趋势?

如何在Allure Report中保留历史趋势?

我来帮你搞定这个问题!你遇到的核心痛点就是mvn clean会清空target目录里的allure-results,导致历史数据丢失。要自动保留Allure报告的历史趋势,关键就是在每次测试和报告生成的流程里,把历史数据妥善备份、复用。下面给你几种实用的方案,都是和Maven流程无缝集成的:

方案一:用Maven资源插件自动同步历史数据

这是最推荐的方式,直接在pom.xml里配置插件,让它在测试前后自动处理历史数据,完全不用手动操作。

核心思路是:把历史数据存在项目根目录的固定文件夹里(避开target,防止被clean删掉),测试开始前把历史复制到新的allure-results/history,测试完成后再把新生成的历史备份回固定文件夹。

在你的pom.xml的<build><plugins>里添加以下配置:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>3.3.1</version>
    <executions>
        <!-- 测试前:把备份的历史数据复制到新的allure-results -->
        <execution>
            <id>copy-history-to-results</id>
            <phase>pre-test</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.build.directory}/allure-results/history</outputDirectory>
                <resources>
                    <resource>
                        <directory>${project.basedir}/allure-history</directory>
                        <filtering>false</filtering>
                    </resource>
                </resources>
            </configuration>
        </execution>
        <!-- 测试后:把新生成的历史备份到项目根目录的固定文件夹 -->
        <execution>
            <id>backup-history-from-results</id>
            <phase>post-test</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.basedir}/allure-history</outputDirectory>
                <resources>
                    <resource>
                        <directory>${project.build.directory}/allure-results/history</directory>
                        <filtering>false</filtering>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>

配置完成后,你只需要正常运行mvn clean test,插件会自动帮你处理历史数据,生成的报告里就会包含之前的测试趋势了。

方案二:用Allure命令行参数指定历史目录

如果你习惯用Allure命令行生成报告,可以直接通过--history-dir参数指定历史数据的存储位置,这样Allure会自动读取和更新历史。

你可以把这个命令集成到Maven的exec插件里,让它在测试完成后自动执行:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>3.1.0</version>
    <executions>
        <execution>
            <id>generate-allure-report</id>
            <phase>post-test</phase>
            <goals>
                <goal>exec</goal>
            </goals>
            <configuration>
                <executable>allure</executable>
                <arguments>
                    <argument>generate</argument>
                    <argument>${project.build.directory}/allure-results</argument>
                    <argument>--history-dir</argument>
                    <argument>${project.basedir}/allure-history</argument>
                    <argument>--clean</argument>
                    <argument>-o</argument>
                    <argument>${project.build.directory}/allure-report</argument>
                </arguments>
            </configuration>
        </execution>
    </executions>
</plugin>

这个配置会在测试结束后,调用Allure命令生成报告,同时从项目根目录/allure-history读取历史数据,生成带趋势的报告,并且自动更新这个目录里的历史文件。

方案三:自定义脚本手动控制(适合灵活需求)

如果你需要更灵活的控制逻辑,可以写一个简单的脚本,在运行测试前后处理历史数据。

比如Linux/Mac的shell脚本(保存为run-tests-with-allure-history.sh):

#!/bin/bash

# 创建历史备份目录
mkdir -p ./allure-history

# 备份上一次的历史数据
cp -r target/allure-results/history/* ./allure-history/ 2>/dev/null || true

# 运行测试
mvn clean test

# 把备份的历史复制到新的allure-results
mkdir -p target/allure-results/history
cp -r ./allure-history/* target/allure-results/history/ 2>/dev/null || true

# 生成报告
allure generate target/allure-results --clean -o target/allure-report

Windows的batch脚本(保存为run-tests-with-allure-history.bat):

@echo off

REM 创建历史备份目录
if not exist ".\allure-history" mkdir ".\allure-history"

REM 备份上一次的历史数据
xcopy /E /I /Y "target\allure-results\history\*" ".\allure-history\" 2>NUL || true

REM 运行测试
mvn clean test

REM 把备份的历史复制到新的allure-results
if not exist "target\allure-results\history" mkdir "target\allure-results\history"
xcopy /E /I /Y ".\allure-history\*" "target\allure-results\history\" 2>NUL || true

REM 生成报告
allure generate target/allure-results --clean -o target/allure-report

使用的时候直接运行脚本就行,它会帮你完成所有历史同步和测试、报告生成的流程。


备注:内容来源于stack exchange,提问作者ConfusedUser

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.15 10:57:59