What is the difference between 'mvn verify' vs 'mvn test'?
Asked Answered
A

2

71

I'm bit confused with mvn verify phase. I've created a Spring Boot project (a simple project, without any explicit configurations added). I've created a few JUnit unit tests which are run with both the mvn verify and mvn test commands.

There isn't any difference observed in the mvn verify and mvn test command output.

What does mvn verify do different than mvn test?

Also some posts on Stack Overflow mentions that mvn verify runs the integration tests. If this is the case then I have few questions.

  • How does Maven identify a specific test as a unit test or integration test?
  • If mvn verify is supposed to run only the integration tests, then why are unit tests executed with it?
Alcoholic answered 30/3, 2021 at 18:5 Comment(2)
@JFabianMeier I agree, but the documentation is very confusing to me in terms of distinguishing what different verify does apart from executing additional phases than test. Also checks on results of integration tests to ensure quality criteria is not very clear, what checks? why only integration tests? where is quality criteria configured?Alcoholic
Here you can see what happens in the different phases for a JAR: maven.apache.org/guides/introduction/… Of course, one can bind additional goals to phases.Kare
S
94

First of all, when you run a Maven goal, it will run any previous goal. The order of basic phases is:

  • Validate
  • Compile
  • Test
  • Package
  • Verify
  • Install
  • Deploy

If you run Test, Maven will execute validate, compile and test. Based on this, the first point is that verify includes test.

Based on official documentation:

  • TEST - test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed

  • VERIFY - run any checks on results of integration tests to ensure quality criteria are met

To run unit tests, the Surefire plugin is recommended. And Failsafe for integration tests.

The verify command executes each default lifecycle phase in order (validate, compile, package, etc.), before executing verify. In most cases the effect is the same as package. However, in case there are integration tests, these will be executed as well. And during the verify phase some additional checks can be done, e.g. if your code is written according to the predefined checkstyle rules.

Conclusion: if you want to run your integration tests and check it, use verify. If you only want to run unit tests, use test.

My personal advice: if in doubt, use verify.

Swordtail answered 30/3, 2021 at 18:20 Comment(4)
to ensure quality criteria - where is this criteria configured?Alcoholic
It depends on your configuration and plugins that are you using. Usually, you configure rules for integration tests with failsafe plugin. Integration tests only will be executed with verify (with mvn test only unit), and then, you can configure for example to break the execution if one test fail, or you can configure to complete all build without break even if there are tests failures. There are a lot of maven plugins and you can attach a concrete action to any phase. The "rules" is marked on all of your pom configuration. From execute a command or even up a docker container....Swordtail
That kind of configuration is so important in your CI environment. Continuing with the same example, if your build break when a IT test fail, your Merge Request will mark that execution failed. If not, the people will see it "in green".Swordtail
Can't the checkStyle rules can be checked in validate phase? Why we are doing it in the verify phase?Latent
K
2

How does Maven identify a specific test as a unit test or integration test?

Integrations Test always takes a name like IT.java or IT.java or *ITCase.java

Kalb answered 21/5, 2022 at 11:14 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Rusell

© 2022 - 2024 — McMap. All rights reserved.