What is the difference between 'mvn verify' vs 'mvn test'? - maven

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?

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.

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

Related

Maven test and then site or other way around?

Currently our pipeline does and this creates a war file and puts in into the server
mvn clean test package
We are now trying to add surefire reports for those tests, everything works well when all tests pass
mvn clean test site package
But if some test fails then site does not get called and we have no report.
Although I know that site has tests inside as well so we tried but even if there was failing tests the site command did not make it to be failing and build succeeded.
mvn clean site package
What is the best approach to get tests and surefire report.
With How to build a jar using maven, ignoring test results?
and https://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html#testFailureIgnore:
<testFailureIgnore>(element) boolean(type) -((ever) since)
Set this to "true" to ignore a failure during testing. Its use is NOT RECOMMENDED, but quite convenient on occasion.
Default value is: false.
User property is: maven.test.failure.ignore.
Using:
mvn -Dmaven.test.failure.ignore=true clean package site
Will:
clean
package (including tests + test results, unless skipped)
not fail the build, when tests fail
and generate the project report (site)

Maven run command for specific test suite

i am trying to build maven command to run specific test:
i want to be able to execute this:
mvn test
mvn integration
mvn specificdata
so each test will go into a folder and run the suite
src/test
src/integration
src/specificdata
mvn test works with the test folder but when i run
mvn specificdata i get
[ERROR] Unknown life-cycle phase "specificdata".
same for integration
how can i make mvn run these tests independently?
This cannot be done in the way you describe it.
test is a phase and as such, part of the standard lifecycle. Calling mvn test does not only run tests, but executes the phases before test as well.
The standard lifecycle also offers phases for integration tests, esp. integration-test. Integration tests are usually put into src/test as well and distinguished by a naming convention. But beware: Calling mvn integration-test will call all previous phases (including test, compileetc.) as well.
https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html

How to configure build step for maven goal in teamcity to run unit test

I use junit in my java project (developed using intellij idea) for unit test, and I want to configure build step in team city to run my unit tests only. I also use maven to build my project. It works when I set goals for maven to "clean compile" but I dont know how to configure build step to run unit tests.
Also in command line. when i run "maven test" it runs unit tests correctly and shows the failures.
I can't comment on TeamCity, but I'll try to help anyway out of my maven knowledge.
So first of all mvn clean compile will never run your unit tests, because maven has a concept of lifecycle and testing phase is coming after compile phase.
When you run mvn test it will run all the face up to (including) the test phase so the unit tests will run as a part of maven default lifecycle.
Now you're asking about a "build step for running unit tests" from which I conclude that you need a separate step. In maven phase is nothing more than running a series of plugins. In maven plugin has goals, so you can run a plugin responsible for running unit tests directly.
In maven this plugin is called "surefire" and a goal is called "test", so you can run:
mvn surefire:test
Given the classes (production code and tests) are compiled, you will see that this only runs your unit tests. So this probably has to be configured in Team City.

Configure cucumber in TeamCity to run unit tests only using tags (Maven)

I have both unit and acceptance tests in a maven project (spring boot). I would like to run unit tests only when my build runs in TeamCity. I am using cucumber.
When i run the tests via command line, everything works as expected (only unit tests are run)
c:\apache-maven-3.3.9\bin\mvn package -Dcucumber.options="--tags #unit"
However, in teamcity, all tests are being run (unit and acceptance). It seems teamcity ignores my cucumber.options
In addition, when I double click on the 'test' lifecycle in Intellij, all tests are run as well (not just unit tests) So my guess is that TeamCity is doing exactly what the 'test' lifecycle does.
How can i get around this problem (in TeamCity)?. I have tried using a 'Command line' step, which works, however, i lose all the tests reporting as well as test coverage reports.
i have solved the Intellij problem by creating (or changing) a configuration:
Try writing it this way in TeamCity:
"-Dcucumber.options= --tags #unit"

Do not run integration tests when running mvn clean install

I want the following to happen:
when I run mvn clean install , I want unit tests alone to run (no integration tests)
When I do mvn integration test , I want integration test alone to run (no unit tests)
when I do mvn test unit test alone should run.
I tried few things with Maven Surefire plugin and Maven failsafe plugin but could not
achieve this. What I have tried is: added Surefire and Failsafe plugins, tried separating the unit tests with annotations, as well separating unit and integration tests at package level, and keeping specific naming convention for unit and integration test.
What I am able to achieve is run unit tests alone on mvn clean install, but when I run Maven integration test I am not able to stop unit tests from running. Any idea how I should go about it?
The problem you have that you don't understand the Maven life-cycle which has the following phases (only excerpts):
clean
...
compile
...
test
..
pre-integration-test
integration-test
post-integration-test
..
install
This means in other words your requirement to do mvn clean install without running the integration tests is not satisfiable by the defaults (conventions).
I would suggest to create a profile where you put the maven-failsafe-plugin into so you can control if integration-tests will run or not. This results into a thing like this:
mvn -Prun-its install
to run the integration tests you can use the following:
mvn verify
which will include running the unit tests but this can be suppressed by using:
mvn -DskipTests=true verify
Using mvn test will run only the unit tests no integration tests cause it's earlier in life cycle.

Resources