如何运行Maven插件的集成测试? [英] How do I run a maven plugin's integration tests?
问题描述
我已经使用 maven-archetype-plugin
原型生成了一个maven插件项目.
I've generated a maven plugin project using the maven-archetype-plugin
archetype.
我将生成的集成测试 src/it/simple-it/verify.groovy
更改为失败:
I've altered the generated integration test src/it/simple-it/verify.groovy
to fail:
assert false
然后我调用:
$ mvn全新安装调用程序:集成测试调用程序:验证
我的断言
测试通过:
[INFO] Installing /home/peter/ownCloud/Personal/eclipse-workspace/my-plugin/target/my-plugin-1.0-SNAPSHOT.jar to /home/peter/.m2/repository/org/example/my-plugin/1.0-SNAPSHOT/my-plugin-1.0-SNAPSHOT.jar
[INFO] Installing /home/peter/ownCloud/Personal/eclipse-workspace/my-plugin/pom.xml to /home/peter/.m2/repository/org/example/my-plugin/1.0-SNAPSHOT/my-plugin-1.0-SNAPSHOT.pom
[INFO]
[INFO] --- maven-invoker-plugin:3.1.0:integration-test (default-cli) @ my-plugin ---
[INFO] Building: simple-it/pom.xml
[INFO] simple-it/pom.xml ................................ SUCCESS (13.4 s)
[INFO]
[INFO] --- maven-invoker-plugin:3.1.0:verify (default-cli) @ my-plugin ---
[INFO] -------------------------------------------------
[INFO] Build Summary:
[INFO] Passed: 1, Failed: 0, Errors: 0, Skipped: 0
[INFO] -------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 34.975 s
[INFO] Finished at: 2019-03-03T08:23:34-04:00
[INFO] Final Memory: 38M/441M
[INFO] ------------------------------------------------------------------------
很明显,该测试实际上并未被调用.我想念什么?
Clearly the test wasn't actually invoked. What am I missing?
为演示该问题,我创建了一个简单的shell脚本:
To demonstrate the issue, I've created a simple shell script:
#!/bin/bash
rm -rf my-plugin
mvn archetype:generate -DgroupId=org.example -DartifactId=my-plugin -DarchetypeArtifactId=maven-archetype-plugin -DarchetypeVersion=1.4 -DinteractiveMode=false
cd my-plugin
echo assert false > src/it/simple-it/verify.groovy
mvn clean install invoker:integration-test invoker:verify
我相信,当达到invoker:verify目标时,该脚本应该在最后一行失败,并且maven执行 verify.groovy
,这实际上是一个 assert false
I believe that this script should fail on the last line, when the invoker:verify goal is reached, and maven executes verify.groovy
which is literally an assert false
.
推荐答案
原型生成器正在为集成测试创建一个单独的配置文件,该配置文件默认情况下处于禁用状态.您将需要通过运行以下命令来启用此配置文件:
The archetype generator is creating a separate profile for integration tests which is disabled by default. You will need to enable this profile by running:
mvn clean verify -Prun-its
然后您的测试将失败.
您可以通过添加以下内容来修改POM以默认情况下启用此配置文件:
You can modify your POM to enable this profile by default by adding the following:
<activation>
<activeByDefault>true</activeByDefault>
</activation>
完整的配置文件块将是:
The full profiles block would then be:
<profiles>
<profile>
<id>run-its</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-invoker-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<debug>true</debug>
<cloneProjectsTo>${project.build.directory}/it</cloneProjectsTo>
<pomIncludes>
<pomInclude>*/pom.xml</pomInclude>
</pomIncludes>
<postBuildHookScript>verify</postBuildHookScript>
<localRepositoryPath>${project.build.directory}/local-repo</localRepositoryPath>
<settingsFile>src/it/settings.xml</settingsFile>
<goals>
<goal>clean</goal>
<goal>test-compile</goal>
</goals>
</configuration>
<executions>
<execution>
<id>integration-test</id>
<goals>
<goal>install</goal>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
此时,您只需要运行:
mvn clean verify
,由于默认情况下现在运行IT测试,因此它也将失败.
and it will also fail since the IT tests are now run by default.
有关Maven构建配置文件的更多信息,请查看此处.
For more information about Maven build profiles have a look Here.
这篇关于如何运行Maven插件的集成测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!