通过Maven surefire-report在汇总格式的单个文件中的单元测试类中的测试执行时间 [英] Execution time of tests in unit test class via maven surefire-report in a single file in summarized format

查看:268
本文介绍了通过Maven surefire-report在汇总格式的单个文件中的单元测试类中的测试执行时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能让我知道如何通过maven-surefire在单个文件中获得单元测试类中每个单元测试所花费的时间?我已经看到target/surefire-report包含每个测试的文件.基本上,我正在寻找一个汇总了所有执行时间的文件.如果可能的话,还可以按每个测试的执行时间对结果进行排序.

Can anyone let me know how can I get the time taken by each of the unit tests in a unit test class in a single file via maven-surefire? I have seen my target/surefire-report it has files for each test. Basically I am looking for a single file with all the execution times summarized. If possible also sort the result by execution time of each test.

我正在使用Maven 3.5& MacOSX 10.12.6上的surefire-plugin 2.4.2.

I am using maven 3.5 & surefire-plugin 2.4.2 on MacOSX 10.12.6.

推荐答案

maven-surefire-plugin当前不允许您执行此操作.它将所有结果写入单独的文件中.如果您感觉是这样,则可以在其问题跟踪器中创建功能请求.缺少的功能.

The maven-surefire-plugin currently doesn't let you do this. It writes all the results in separate files. You could create a feature-request in its issue tracker, if you feel like this is a missing feature.

但是,您可以使用一些Linux命令将输出转换为所需的输出.以下是一些命令,可将单独的XML文件转换为看起来像您想要的文件:

However you can use some Linux commands to convert the output to what you need. Here are some commands that turn the separate XML files into a single file that looks like what you want:

grep testcase target/surefire-reports/TEST-*.xml |
  sed 's/.* name="\(.*\)" classname="\(.*\)" time="\(.*\)".*/\2#\1() - \3ms/g' |
  sort -n -k 3 > output.txt


更新:数值排序存在分数变化的问题 数字.使用下面的awk版本来解决此问题.


Update: Numeric sorting has problems with varying number of fraction digits. Use awk version below to solve this.

使用awk可以做的更短,而且不那么隐秘:

The same thing could be done with awk a bit shorter and less cryptic:

grep -h testcase target/surefire-reports/TEST-*.xml |
  awk -F '"' '{printf("%s#%s() - %.3fms\n", $4, $2, $6); }' |
  sort -n -k 3 > output.txt

在生成surefire报告后,您必须从maven项目的顶级目录中执行这些命令.

You have to execute these commands from the toplevel directory of your maven project after the surefire-reports were generated.

如果您有多模块项目,请改用此项目:

If you have multi-module project, use this instead:

find . -name TEST-*.xml -exec grep -h testcase {} \; |
  awk -F '"' '{printf("%s#%s() - %.3fms\n", $4, $2, $6); }' |
  sort -n -k 3 > output.txt

结果文件为output.txt,并包含以下格式的行:

The resulting file is output.txt and contains lines of the following format:

<classname>#<methodname>() - <time>ms

结果按消耗的时间排序.

The result is sorted by consumed time.

这篇关于通过Maven surefire-report在汇总格式的单个文件中的单元测试类中的测试执行时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆