英文:
How to run all unit-tests from a test jar?
问题
我运行了以下命令来生成一个测试Jar包:mvn jar:test-jar
。
执行所有单元测试的mvn
命令是什么?
英文:
I ran the following command to produce a test-jar: mvn jar:test-jar
.
What's the mvn
command to execute all unit-tests?
答案1
得分: 1
在创建包含测试类的JAR文件时,您可能希望重用这些类。例如,一些常见的测试固件、抽象基类等等。
换句话说,当您运行 mvn jar:test-jar
时,您会创建一个新的构件,您可以将其作为依赖添加到其他Maven项目或模块中:
<dependencies>
<dependency>
<groupId>groupId</groupId>
<artifactId>artifactId</artifactId>
<classifier>tests</classifier> <!-- 注意分类器 -->
<type>test-jar</type> <!-- 注意类型 -->
<version>version</version>
<scope>test</scope>
</dependency>
</dependencies>
注意: 这种方法不是首选方式,因为 test-jar 会丢失传递的测试范围依赖
。
因此,回到最初的问题:您不是创建 test-jar
来运行测试,而是创建它以在项目或模块之间重用测试类(通过添加依赖)。
要运行测试,只需使用标准的Maven命令:
mvn clean test
英文:
When you're creating a jar containing test-classes, you would probably want to reuse those classes. For example, some common test fixtures or abstract base classes, and so on.
In other words, when you run mvn jar:test-jar
, you create new artifact, which you can add as dependency in other maven project or module:
<dependencies>
<dependency>
<groupId>groupId</groupId>
<artifactId>artifactId</artifactId>
<classifier>tests</classifier> <!-- note the classifier -->
<type>test-jar</type> <!-- note the type -->
<version>version</version>
<scope>test</scope>
</dependency>
</dependencies>
Note: that such approach is not the preferred way, since test-jar
will loose transitive test-scoped
dependencies
So, returning to the original question: you don't create test-jar
to run tests, you create it to reuse test classes between projects or modules (by means of adding dependency
).
To run tests you'll simply use standard Maven command:
mvn clean test
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论