简体   繁体   English

如何从测试 jar 运行所有单元测试?

[英]How to run all unit-tests from a test jar?

I ran the following command to produce a test-jar: mvn jar:test-jar .我运行以下命令来生成一个 test-jar: mvn jar:test-jar

What's the mvn command to execute all unit-tests?执行所有单元测试的mvn命令是什么?

When you're creating a jar containing test-classes, you would probably want to reuse those classes.当您创建一个包含测试类的 jar 时,您可能希望重用这些类。 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:换句话说,当您运行mvn jar:test-jar ,您会创建新的工件,您可以将其作为依赖项添加到其他 Maven 项目或模块中:

  <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注意:这种方法不是首选方法,因为test-jar会丢失可传递的test-scoped依赖项

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 ).因此,回到最初的问题:您不创建test-jar来运行测试,而是创建它以在项目或模块之间重用测试类(通过添加dependency )。

To run tests you'll simply use standard Maven command:要运行测试,您只需使用标准的 Maven 命令:

mvn clean test

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM