您可以通过在主项目目录下执行 Maven 的 Jtest 目标来执行分析和测试。除非您的项目从未构建过且需要确保外部项目依赖项可用,或者您正在测试一个多模块项目,否则在执行分析前,无需构建被测项目。我们建议在运行分析前编译多模块项目,从而让 Jtest 能够使用来自本地资源库的工件,减少测试和分析代码所需的时间。
作为聚合器,Maven 的 Jtest 插件在构建期间只能执行一次。
对代码执行静态分析的步骤:
执行 jtest:jtest 目标。您的命令行可能与以下内容类似:
mvn jtest:jtest |
Maven 的 Jtest 插件将在 .json 文件中收集必要的构建数据,并根据您提供的配置分析您的代码(请参阅配置 Jtest 执行)。
查看分析结果(请参阅查看结果)。
默认情况下会从分析中排除测试源。若要分析测试代码,需启用 |
如果您的构建通过影响分析范围的插件进行扩展(例如,通过在 generate-resources 阶段追加或修改资源文件),您可以执行所有这些阶段,以便 Jtest 收集完整的项目数据,或者只需在 test-compile 阶段之前执行 jtest:jtest,如下所示:
mvn test-compile jtest:jtest |
您可以通过使用 jtest:jtest 和 jtest:agent 目标,以及专用的 Unit Tests
内建测试配置,将单元测试结果包含在 Jtest 报告中:
使用 Maven 执行 jtest:agent 和 jtest:jtest 目标。确保在运行 jtest:agent
目标前编译测试和类。 您的命令行可能与以下内容类似:
mvn clean test-compile jtest:agent test jtest:jtest -Djtest.config="builtin://Unit Tests" |
默认情况下,Jtest 会收集执行测试的覆盖率数据。若要禁用覆盖率收集功能,则启用 jtest.coverage.skip
选项(详细信息,请参阅 Maven 的 Jtest 目标参考)。
在未编译类的情况下执行 jtest:agent
目标可能会导致覆盖率为空。如果您的构建使用 Tycho 插件,或其他脱离默认生命周期的插件,则应调用专用(非默认)的编译器,确保类正确编译并收集覆盖率信息。您的命令行可能与以下内容类似:
mvn clean tycho-compiler:compile jtest:agent verify jtest:jtest -Djtest.config="builtin://Unit Tests" |
如果您通过调用 org.junit.Assume(JUnit 4)或 org.junit.jupiter.api.Assumptions(JUnit 5)类的方法来跳过某个测试,Jtest 会收集调用方法前执行的代码行的覆盖率。如果使用 JUnit 4 运行器执行测试,覆盖率信息不会与覆盖率报告中的任何测试相关联。
Jtest 收集 Maven 并行构建的测试覆盖率,使用 -T 选项启用。详细信息,请参阅 https://cwiki.apache.org/confluence/display/MAVEN/Parallel+builds+in+Maven+3。例如:
mvn -T 1C clean test-compile jtest:agent test jtest:jtest -Djtest.config="builtin://Unit Tests" |
Jtest 支持为 Surefire 并行测试执行收集覆盖率,但收集的覆盖率可能无法与执行的每个测试正确匹配。
jtest:agent
目标jtest:agent 目标基于构建模块生成 Jtest 代理设置,并在 initialize 阶段将 javaagent 虚拟机参数设置到由 agentPropertyNames 参数指定的属性中。
默认情况下,它会尝试通过特定的属性将 javaagent VmArg
注入 maven-surefire-plugin
和 eclipse-test-plugin
。
与 Maven 测试插件结合使用 jtest:agent
目标的前提条件:
forkCount
或 forkMode
参数)argLine
参数)jtest:agent
目标会尝试通过设置 Maven 测试插件(maven-surefire-plugin 和 tycho-surefire-plugin)中 VmArgs
参数的属性,自动进行配置(默认通过 argLine 和 tycho.testArgLine 属性完成)。您可以通过以下方式之一自定义这些参数的默认值:
通过默认属性。jtest:agent
目标将使用 javaagent VmArg
扩展属性:
<project> <!-- ... --> <properties> <!-- argLine property which will be extended by jtest:agent goal ---> <argLine>-Xmx=1024m -XX:MaxPermSize=256m</argLine> </properties> <!-- ... --> <build> <plugins><!-- or pluginManagement --> <plugin> <artifactId>maven-surefire-plugin</artifactId> <configuration> <forkCount>1</forkCount> <!-- do not configure argLine parameter here --> </configuration> </plugin> </plugin> </build> </project> |
通过注入自定义属性的参数值:
<project> <!-- ... --> <build> <plugins> <plugin> <groupId>com.parasoft.jtest</groupId> <artifactId>jtest-maven-plugin</artifactId> <version>2020.1.0</version> <configuration> <!-- jtest:agent goal will set Jtest Java agent VmArg into properties below --> <agentPropertyNames>jtest.argLine</agentPropertyNames> <!-- optional coverage block with parameters identical as in offline coverage --> <coverage> <!-- ... --> </coverage> </configuration> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <configuration> <argLine>${jtest.argLine} -Xmx=1024m -XX:MaxPermSize=256m</argLine> </configuration> </plugin> </plugin> </build> </project> |
Jtest 支持为 Tycho 构建执行 JUnit 4 测试和收集测试覆盖率。但是,由于 OSGi 的限制,使用 tycho-surefire-plugin 执行的测试需要扩展 tycho-surefire 配置:
将 [INSTALL_DIR]/integration/coverage 中 Jtest 提供的 runtime.jar 文件部署到以下位置之一:
- Maven 资源库。您的命令行可能与以下内容类似:
mvn deploy:deploy-file -Dfile=C:\parasoft\jtest\integration\coverage\runtime.jar -DrepositoryId=REPOSITORY_ID -Durl=REPOSITORY_URL -DartifactId=runtime-jtest -DgroupId=com.parasoft.jtest -Dversion=2020.1.0 |
- 本地 Maven 资源库。您的命令行可能与以下内容类似:
mvn install:install-file -Dfile=<C:\parasoft\jtest\integration\coverage\runtime.jar -DgroupId=com.parasoft.jtest -DartifactId=runtime-jtest -Dversion=2020.1.0 -Dpackaging=jar |
通过将部署的工件添加为框架扩展来扩展您的 tycho-surefire 配置。配置可能与以下内容类似:
<plugin> <groupId>org.eclipse.tycho</groupId> <artifactId>tycho-surefire-plugin</artifactId> <version>${tycho-version}</version> <configuration> <frameworkExtensions> <frameworkExtension> <groupId>com.parasoft.jtest</groupId> <artifactId>runtime-jtest</artifactId> <version>2020.1.0</version> </frameworkExtension> </frameworkExtensions> ... </plugin> |
或者,如果您只执行测试而不收集覆盖率信息,可以通过在 jtestcli.properties
配置文件添加以下选项,Jtest 将使用已弃用的 XML 处理机制获取测试结果:
jtest.unittest.xml.results.processing.enabled=true |
这样,Jtest 报告中将包含 Tycho 构建的测试结果。不包含覆盖率信息。
请注意,启用已弃用的 XML 处理机制可能会影响测试执行速度。
Jtest 的覆盖率代理允许在对运行的应用程序执行手动或自动测试期间收集覆盖率数据。有关使用 Jtest 收集应用程序覆盖率的信息,请参阅应用程序覆盖率。
简介您可以通过测试影响分析来扩展 Maven 的 Jtest 插件的功能。利用测试影响分析可以只关注和重新运行受变更影响的测试,从而不必要耗费额外的时间和精力执行大量未受影响的测试。对项目执行测试影响分析需要:
前提条件
|
<build> <plugins> <plugin> <groupId>com.parasoft.jtest.tia</groupId> <artifactId>tia-maven-plugin</artifactId> <version>2020.1.0</version> </plugin> </plugins> </build> |
您可以通过配置 tia-maven-plugin
,在 POM 文件或命令行中自定义项目的测试影响分析。至少需要提供 Jtest 在执行过程中生成的以下文件的路径:
coverage.xml
report.xml
如果有一个或多个测试位于与被测源代码不同的项目中,则必须提供 coverage.xml 文件的绝对路径,以确保重新执行所有受影响的测试。
有关完整的可用选项列表,请参阅 Maven 的 Jtest 目标参考。
如果在 POM 文件中配置插件,可以将 tia-maven-plugin
属性添加到:
插件声明
<plugin> <groupId>com.parasoft.jtest.tia</groupId> <artifactId>tia-maven-plugin</artifactId> <version>2020.1.0</version> <configuration> <referenceCoverageFile>target/jtest/coverage.xml</referenceCoverageFile> <referenceReportFile>target/jtest/report.xml</referenceReportFile> <runFailedTests>false</runFailedTests> <runModifiedTests>true</runModifiedTests> <jtestHome>${jtest.home}</jtestHome> <settings>jtestcli.properties</settings> </configuration> </plugin> |
插件执行
<plugin> <groupId>com.parasoft.jtest.tia</groupId> <artifactId>tia-maven-plugin</artifactId> <version>2020.1.0</version> <executions> <execution> <goals> <goal>affected-tests</goal> </goals> <configuration> <referenceCoverageFile>target/jtest/coverage.xml</referenceCoverageFile> <referenceReportFile>target/jtest/report.xml</referenceReportFile> <runFailedTests>false</runFailedTests> <runModifiedTests>true</runModifiedTests> <jtestHome>${jtest.home}</jtestHome> <settings>jtestcli.properties</settings> </configuration> </execution> </executions> </plugin> |
Maven 属性
<properties> <jtest.referenceCoverageFile>target/jtest/coverage.xml</jtest.referenceCoverageFile> <jtest.referenceReportFile>target/jtest/report.xml</jtest.referenceReportFile> <jtest.runFailedTests>false</jtest.runFailedTests> <jtest.runModifiedTests>true</jtest.runModifiedTests> <jtest.home>${env.JTEST_HOME}</jtest.home> <jtest.settings>jtestcli.properties</jtest.settings> </properties> |
如果在命令行中自定义测试影响分析,则使用 -D
选项传递 tia-maven-plugin
属性。该属性必须包含“jtest”前缀(详细信息,请参阅 Maven 的 Jtest 目标参考)。您的命令行可能与以下内容类似:
mvn tia:affected-tests test -Djtest.referenceCoverageFile=target/jtest/coverage.xml -Djtest.referenceReportFile=target/jtest/report.xml -Djtest.runFailedTests=false -Djtest.runModifiedTests=true -Djtest.home=$JTEST_HOME -Djtest.settings=jtestcli.properties |
affected-tests
目标您可以在命令行配置执行 affected-tests
目标或在 POM 文件中配置该目标。
当 tia-maven-plugin
包含在 Maven 构建文件中时(请参阅与测试影响分析插件集成),可以在命令行执行 tia:affected-tests
目标。确保在 test
目标前执行:
mvn tia:affected-tests test |
您可以在构建插件或配置文件的构建插件中配置执行 affected-tests
目标。
如果 affected-tests
目标包含在构建插件中:
<build> <plugins> <plugin> <groupId>com.parasoft.jtest.tia</groupId> <artifactId>tia-maven-plugin</artifactId> <version>2020.1.0</version> <executions> <execution> <goals> <goal>affected-tests</goal> </goals> </execution> </executions> </plugin> </plugins> </build> |
它将使用 Maven test
目标自动执行:
mvn test |
如果 affected-tests
目标包含在配置文件的构建插件中:
<profile> <id>tia</id> <build> <plugins> <plugin> <groupId>com.parasoft.jtest.tia</groupId> <artifactId>tia-maven-plugin</artifactId> <version>2020.1.0</version> <executions> <execution> <goals> <goal>affected-tests</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </profile> |
则必须使用配置文件执行 Maven test
目标:
mvn test -P tia |
当测试套件中至少有一个测试受到代码变更影响时,测试影响分析会重新运行整个测试套件。需注意,这可能会导致一些与受影响的测试包含在同一测试套件中的未受影响测试重新运行。
默认情况下,Maven surefire 插件无法重新运行嵌套测试。为确保在使用 Maven 运行测试影响分析时重新执行嵌套测试,需将 <include></include> 和 <exclude></exclude> 元素添加到 Maven surefire 插件配置中,例如:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.2</version> </plugin> </plugins> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <testFailureIgnore>true</testFailureIgnore> <includes> <include></include> </includes> <excludes> <exclude></exclude> </excludes> </configuration> </plugin> </plugins> </pluginManagement> </build> |
测试影响分析依赖于 Surefire 的默认包含/排除机制,以及当前 POM 文件中配置的自定义包含和排除设置。在极少数情况下,测试影响分析可能无法识别包含或排除模式。这可能导致无法重新运行与模式匹配的受影响测试和/或无法重新运行未受代码变更影响的测试。
要解决该问题,可以在命令行中使用 -Dparasoft.testFilter
选项指定文件模式。您的命令行可能与以下内容类似:
mvn tia:affected-tests test -Djtest.referenceCoverageFile=target/jtest/coverage.xml -Djtest.referenceReportFile=target/jtest/report.xml -Djtest.settings=jtestcli.properties -Djtest.testFilter="**/*Test.java, !**/ProblemTest.java" |
需注意,Jtest 测试影响分析不支持 Surefire 用于执行单个测试的 -Dtest
内置选项。使用 -Dtest
选项会覆盖 POM 文件和命令行中为测试影响分析配置的包含和排除设置。