单元测试中测试的执行时间 class 通过 maven surefire-report 在单个文件中以汇总格式
Execution time of tests in unit test class via maven surefire-report in a single file in summarized format
任何人都可以让我知道如何通过 maven-surefire
在单个文件中获取单元测试 class 中每个单元测试所花费的时间吗?我已经看到我的 target/surefire-report
它有每个测试的文件。基本上我正在寻找一个汇总了所有执行时间的文件。如果可能,还按每个测试的执行时间对结果进行排序。
我在 MacOSX 10.12.6 上使用 maven 3.5 和 surefire-plugin 2.4.2。
maven-surefire-plugin
目前不允许您这样做。它将所有结果写入单独的文件中。如果您觉得这是一个缺失的功能,您可以在其 issue tracker 中创建一个功能请求。
但是您可以使用一些 Linux 命令将输出转换为您需要的内容。这里有一些命令可以将单独的 XML 文件变成一个看起来像你想要的文件:
grep testcase target/surefire-reports/TEST-*.xml |
sed 's/.* name="\(.*\)" classname="\(.*\)" time="\(.*\)".*/#() - ms/g' |
sort -n -k 3 > output.txt
更新: 数值排序有分数不同的问题
数字。使用下面的 awk
版本来解决这个问题。
同样的事情可以用awk
来完成,更短一些,不那么神秘:
grep -h testcase target/surefire-reports/TEST-*.xml |
awk -F '"' '{printf("%s#%s() - %.3fms\n", , , ); }' |
sort -n -k 3 > output.txt
生成 surefire-reports 后,您必须从 maven 项目的顶层目录执行这些命令。
如果您有多模块项目,请改用它:
find . -name TEST-*.xml -exec grep -h testcase {} \; |
awk -F '"' '{printf("%s#%s() - %.3fms\n", , , ); }' |
sort -n -k 3 > output.txt
生成的文件是 output.txt
并且包含以下格式的行:
<classname>#<methodname>() - <time>ms
结果按消耗时间排序。
您可以使用 surefire-report-plugin 聚合结果(但仍然不会排序)
mvn surefire-report:report -DlinkXRef=false -Daggregate=true
或
mvn surefire-report:report-only -DlinkXRef=false -Daggregate=true
如果您已经使用 surefire 插件构建项目。
这将在您的根目标目录中生成 surefire-report.html,您可以在其中找到每个模块和每个测试套件的时间统计信息。
我用 Martin Höller 的代码片段编写了一个小的 bash 脚本,可能会对某人有所帮助:
#!/bin/bash
# This script goes through tests results in `target/surefire-reports` and sorts the results by the
# amount of time they took. This is helpful to identify slow tests.
set -e
# Make sure the surefire folder exists
if [ ! -d "target/surefire-reports" ]; then
echo "The folder 'target/surefire-reports' doesn't exists. Please run tests before using this script."
exit 1
fi
# Go through the surefire test reports and sort tests by time taken. Source for this snippet:
#
grep -h testcase target/surefire-reports/TEST-*.xml |
awk -F '"' '{print "#" "() - " "ms" }' |
sort -rn -k 3
任何人都可以让我知道如何通过 maven-surefire
在单个文件中获取单元测试 class 中每个单元测试所花费的时间吗?我已经看到我的 target/surefire-report
它有每个测试的文件。基本上我正在寻找一个汇总了所有执行时间的文件。如果可能,还按每个测试的执行时间对结果进行排序。
我在 MacOSX 10.12.6 上使用 maven 3.5 和 surefire-plugin 2.4.2。
maven-surefire-plugin
目前不允许您这样做。它将所有结果写入单独的文件中。如果您觉得这是一个缺失的功能,您可以在其 issue tracker 中创建一个功能请求。
但是您可以使用一些 Linux 命令将输出转换为您需要的内容。这里有一些命令可以将单独的 XML 文件变成一个看起来像你想要的文件:
grep testcase target/surefire-reports/TEST-*.xml |
sed 's/.* name="\(.*\)" classname="\(.*\)" time="\(.*\)".*/#() - ms/g' |
sort -n -k 3 > output.txt
更新: 数值排序有分数不同的问题
数字。使用下面的 awk
版本来解决这个问题。
同样的事情可以用awk
来完成,更短一些,不那么神秘:
grep -h testcase target/surefire-reports/TEST-*.xml |
awk -F '"' '{printf("%s#%s() - %.3fms\n", , , ); }' |
sort -n -k 3 > output.txt
生成 surefire-reports 后,您必须从 maven 项目的顶层目录执行这些命令。
如果您有多模块项目,请改用它:
find . -name TEST-*.xml -exec grep -h testcase {} \; |
awk -F '"' '{printf("%s#%s() - %.3fms\n", , , ); }' |
sort -n -k 3 > output.txt
生成的文件是 output.txt
并且包含以下格式的行:
<classname>#<methodname>() - <time>ms
结果按消耗时间排序。
您可以使用 surefire-report-plugin 聚合结果(但仍然不会排序)
mvn surefire-report:report -DlinkXRef=false -Daggregate=true
或
mvn surefire-report:report-only -DlinkXRef=false -Daggregate=true
如果您已经使用 surefire 插件构建项目。
这将在您的根目标目录中生成 surefire-report.html,您可以在其中找到每个模块和每个测试套件的时间统计信息。
我用 Martin Höller 的代码片段编写了一个小的 bash 脚本,可能会对某人有所帮助:
#!/bin/bash
# This script goes through tests results in `target/surefire-reports` and sorts the results by the
# amount of time they took. This is helpful to identify slow tests.
set -e
# Make sure the surefire folder exists
if [ ! -d "target/surefire-reports" ]; then
echo "The folder 'target/surefire-reports' doesn't exists. Please run tests before using this script."
exit 1
fi
# Go through the surefire test reports and sort tests by time taken. Source for this snippet:
#
grep -h testcase target/surefire-reports/TEST-*.xml |
awk -F '"' '{print "#" "() - " "ms" }' |
sort -rn -k 3