Bỏ qua test, unit test khi build, run maven

Bỏ qua test, unit test khi build, run maven.

Khi build, install, package… trong maven nó sẽ tự động chạy các script test, điều này gây tốn thời gian hoặc làm cho lệnh của bạn bị failed nếu các script test bị failed.

Bỏ qua test, unit test khi build, run maven

Cách 1: khi chạy maven trên dòng lệnh (command line)

Thêm option -DskipTests: option sẽ vẫn thực hiện compile các dòng code test nhưng không thực thi chúng

mvn install -DskipTests

Thêm option -Dmaven.test.skip=true: option này sẽ bỏ qua việc compile và thực thi các câu lệnh test

mvn install -Dmaven.test.skip=true

Cách 2: thiết lập trong file pom.xml

Để bỏ qua chạy test, ta thiết lập field skipTests bằng true<skipTests>true</skipTests>

<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.0.0-M4</version>
        <configuration>
          <skipTests>true</skipTests>
        </configuration>
      </plugin>
    </plugins>
  </build>
  [...]
</project>

Nếu bạn muốn mặc định bỏ qua chạy test nhưng vẫn có thể enable khi chạy trên command line thì làm như sau:

thiết lập skipTests trong file pom.xml

<project>
  [...]
  <properties>
    <skipTests>true</skipTests>
  </properties>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.0.0-M4</version>
        <configuration>
          <skipTests>${skipTests}</skipTests>
        </configuration>
      </plugin>
    </plugins>
  </build>
  [...]
</project>

khi muốn enable chạy test thì thêm option -DskipTests=false

mvn install -DskipTests=false

 

 

 

 

Okay, Done! (xem thêm: https://stackjava.com/category/maven)

References: https://maven.apache.org/surefire/maven-surefire-plugin/examples/skipping-tests.html

stackjava.com