添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

通常,测试报告的生成依赖于测试框架的配套工具或是第三方插件。

对于JUnit,你可以使用JUnit自身的测试报告功能或者集成如Surefire Report Plugin(Maven项目中)来生成报告。

我的项目使用的是maven+junit搭建,怎么配置Surefire Report Plugin?

在Maven项目中,配置Surefire Report Plugin实际上非常直接,因为通常你只需要在你的 pom.xml 文件中添加Surefire Plugin的配置部分即可,而无需单独声明Surefire Report Plugin。这是因为Surefire Plugin在执行JUnit测试时,默认就会生成测试报告,并存放在 target/surefire-reports 目录下。以下是基本的配置示例:

<project>
  <build>
    <plugins>
      <!-- Maven Surefire Plugin for running JUnit tests -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.0.0-M5</version> <!-- Use the latest version available -->
        <configuration>
          <!-- Configure properties of the Surefire Plugin -->
          <!-- For example, to run tests in parallel -->
          <!--<parallel>methods</parallel>-->
          <!--<threadCount>10</threadCount>-->
          <!-- Exclude or include specific tests -->
          <!--<includes>
            <include>**/*Test.java</include>
          </includes>-->
          <!--<excludes>
            <exclude>**/*IT.java</exclude>
          </excludes>-->
          <!-- Other configurations as needed -->
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

mvn package 或者在ide里面 package

[INFO] Scanning for projects...
[INFO] 
......
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running MyJavaFXAppTest
1234567890 is exists
1234567890 is exists
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 16.713 s - in MyJavaFXAppTest
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ StockManagementSystem ---
[INFO] Building jar: D:\soft\src\StockManagementSystem\target\StockManagementSystem-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  20.300 s
[INFO] Finished at: 2024-05-16T17:22:37+08:00
[INFO] ------------------------------------------------------------------------

生成结果如下:

打开surefire-reports文件夹

2024-05-16T17-22-19_163.dumpstream内容

# Created at 2024-05-16T17:22:19.465
Boot Manifest-JAR contains absolute paths in classpath 'D:\ProgramData\.m2\repository\org\apache\maven\surefire\surefire-booter\3.0.0-M5\surefire-booter-3.0.0-M5.jar'
Hint: <argLine>-Djdk.net.URLClassPath.disableClassPathURLCheck=true</argLine>

 MyJavaAppTest.txt

-------------------------------------------------------------------------------
Test set: MyJavaAppTest
-------------------------------------------------------------------------------
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 16.713 s - in MyJavaAppTest

TEST-MyJavaAppTest.xml

<?xml version="1.0" encoding="UTF-8"?>
<testsuite xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://maven.apache.org/surefire/maven-surefire-plugin/xsd/surefire-test-report-3.0.xsd" version="3.0" name="MyJavaAppTest" time="16.713" tests="2" errors="0" skipped="0" failures="0">
  <properties>
    <property name="sun.desktop" value="windows"/>
    <property name="awt.toolkit" value="sun.awt.windows.WToolkit"/>
    <property name="file.encoding.pkg" value="sun.io"/>
    <property name="java.specification.version" value="1.8"/>
    <property name="javafx.runtime.version" value="8.0.131-b11"/>
    <property name="sun.cpu.isalist" value="amd64"/>
    <property name="sun.jnu.encoding" value="GBK"/>
    <property name="java.class.path" value="......"/>
    <property name="java.vm.vendor" value="Oracle Corporation"/>
    <property name="sun.arch.data.model" value="64"/>
    <property name="user.variant" value=""/>
    <property name="java.vendor.url" value="http://java.oracle.com/"/>
    <property name="user.timezone" value="Asia/Shanghai"/>
    <property name="java.vm.specification.version" value="1.8"/>
    <property name="os.name" value="Windows 7"/>
    <property name="javafx.version" value="8.0.131"/>
    <property name="user.country" value="CN"/>
    <property name="sun.java.launcher" value="SUN_STANDARD"/>
    <property name="sun.boot.library.path" value="D:\soft\jdk1.8.0_131\jre\bin"/>
    <property name="sun.java.command" value="......"/>
    <property name="surefire.test.class.path" value="......"/>
    <property name="sun.cpu.endian" value="little"/>
    <property name="user.home" value="C:\Users\Administrator"/>
    <property name="user.language" value="zh"/>
    <property name="java.specification.vendor" value="Oracle Corporation"/>
    <property name="java.home" value="D:\soft\jdk1.8.0_131\jre"/>
    <property name="basedir" value="D:\soft\src\LibraryManagementSystem"/>
    <property name="style.color" value="always"/>
    <property name="file.separator" value="\"/>
    <property name="line.separator" value="&#10;"/>
    <property name="java.vm.specification.vendor" value="Oracle Corporation"/>
    <property name="java.specification.name" value="Java Platform API Specification"/>
    <property name="java.awt.graphicsenv" value="sun.awt.Win32GraphicsEnvironment"/>
    <property name="surefire.real.class.path" value="......"/>
    <property name="sun.boot.class.path" value="......"/>
    <property name="user.script" value=""/>
    <property name="sun.management.compiler" value="HotSpot 64-Bit Tiered Compilers"/>
    <property name="java.runtime.version" value="1.8.0_131-b11"/>
    <property name="user.name" value="Administrator"/>
    <property name="path.separator" value=";"/>
    <property name="os.version" value="6.1"/>
    <property name="java.endorsed.dirs" value="D:\soft\jdk1.8.0_131\jre\lib\endorsed"/>
    <property name="java.runtime.name" value="Java(TM) SE Runtime Environment"/>
    <property name="file.encoding" value="GBK"/>
    <property name="java.vm.name" value="Java HotSpot(TM) 64-Bit Server VM"/>
    <property name="localRepository" value="D:\ProgramData\.m2\repository"/>
    <property name="java.vendor.url.bug" value="http://bugreport.sun.com/bugreport/"/>
    <property name="java.io.tmpdir" value="C:\Users\ADMINI~1\AppData\Local\Temp\"/>
    <property name="java.version" value="1.8.0_131"/>
    <property name="user.dir" value="D:\soft\src\LibraryManagementSystem"/>
    <property name="os.arch" value="amd64"/>
    <property name="java.vm.specification.name" value="Java Virtual Machine Specification"/>
    <property name="java.awt.printerjob" value="sun.awt.windows.WPrinterJob"/>
    <property name="sun.os.patch.level" value="Service Pack 1"/>
    <property name="java.library.path" value="......"/>
    <property name="java.vm.info" value="mixed mode"/>
    <property name="java.vendor" value="Oracle Corporation"/>
    <property name="java.vm.version" value="25.131-b11"/>
    <property name="java.ext.dirs" value="D:\soft\jdk1.8.0_131\jre\lib\ext;C:\Windows\Sun\Java\lib\ext"/>
    <property name="sun.io.unicode.encoding" value="UnicodeLittle"/>
    <property name="java.class.version" value="52.0"/>
  </properties>
  <testcase name="BM_Add_001(FxRobot)" classname="MyJavaAppTest" time="9.146">
    <system-out><![CDATA[test
1234567890 is exists
]]></system-out>
  </testcase>
  <testcase name="BM_Add_002(FxRobot)" classname="MyJavaAppTest" time="7.545">
    <system-out><![CDATA[test
1234567890 is exists
]]></system-out>
  </testcase>
</testsuite>

不过这看起来很不友好,我想应该有方法生成html界面,还真被我找到了。

生成HTML报告:虽然Surefire Plugin本身不直接生成HTML报告,但你可以使用maven-surefire-report-plugin来生成HTML格式的报告。执行以下Maven命令来生成HTML报告:

mvn surefire-report:report
D:\soft\src\StockManagementSystem>mvn surefire-report:report
[INFO] Scanning for projects...
[INFO]
......
[INFO]
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running MyJavaAppTest
1234567890 is exists
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 16.87 s -
 in MyJavaAppTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO]
[INFO] <<< maven-surefire-report-plugin:3.2.5:report (default-cli) < [surefire]t
est @ StockManagementSystem <<<
[INFO]
[INFO]
[INFO] --- maven-surefire-report-plugin:3.2.5:report (default-cli) @ StockManagementSystem ---
[INFO] Rendering content with org.apache.maven.skins:maven-default-skin:jar:1.3
skin.
[WARNING] Unable to locate Test Source XRef to link to - DISABLED
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  20.002 s
[INFO] Finished at: 2024-05-16T17:41:49+08:00
[INFO] ------------------------------------------------------------------------

这将在target/site/surefire-report.html生成一个HTML报告,你可以用浏览器打开这个文件来浏览更易读的测试结果。

集成至CI/CD工具:如果你的项目集成到了Jenkins、GitLab CI/CD或其他持续集成工具中,这些工具通常可以直接解析Surefire的XML报告,并提供图形化的测试结果概览。

1.  -------------------------------------------------------   2.   T E S T S   3.  -------------------------------------------------------   4.  Running com <groupId>io.qameta.allure</groupId> <artifactId>allure-maven</artifactId> <version>2.9</version> </plugin> mvn clean test -运行测试 您可以使用以下命令之一生成报告: mvn allure:serve 报告将生成到temp文件夹中。 具有结果的Web服务器将启动。 mvn allure:report 报告将在以下目录中生成: target/site/allure-maven/index.html 您可以像以下配置诱人版本: <plugin> <groupId>io.qameta.allure</gr 在新建maven包pom文件中引入三个测试报告 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http:/...
自动化测试时,Maven工程中单元测试生成测试报告,使用TestNg单元测试框架时,测试报告默认生成在target\surefire-reports目录下。例如,pom.xml文件中部分配置: <dependencies> <dependency> <groupId>org.testng</groupId>
本文主要简述了extent-report在maven项目中的使用,以下内容将默认项目是maven项目,项目中已存在可以执行的测试方法,且本文配置xml文件作为执行测试方法的执行入口,主要用于保存记忆,留待日后查阅~~~~~~ -----------------------------------以下是正文----------------------------- 第一步:先在ma...
eclipse项目里javascript总是验证,且出现最多的error是 "Description Resource Path Location Type Syntax error on toke 12926 MySQL Workbench Failed to Connect to MySQL at 127.0.0.1:3306 with user root Bad handshake 12323 maven-surefire-plugin 在maven-central仓库里是依赖,不是插件; <dependency> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-report-plugin</artifactId> <version>3.5.0</version> </dependency> 解决python -m pip install --upgrade pip更新失败 这个太秀了,支持博主 Python的22个常用模块 CodeWhisperer: 这篇文章列举了Python的22个常用模块,涵盖了操作系统接口、系统功能、数据处理到深度学习等多个领域,对初学者和开发者都非常实用。注意到了重复列出的sklearn和scikit-learn实际上是同一个库,可能是个小疏忽。整体来说,这份列表全面且实用,是学习和参考的好资源。 MySQL常用语句 CSDN-Ada助手: MySQL入门 技能树或许可以帮到你:https://edu.csdn.net/skill/mysql?utm_source=AI_act_mysql