Maven is skipping unit tests - maven

I am facing an issue while running test using command line.
mvn test -DskipTests=false -DskipITs=false is only running some unit tests and skipping the rest. Are there any tricks or other options to force maven to take into account all the related tests.

Related

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

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

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.

Maven: disabling test phase but not integration test phase

I'm working with a maven project, and are trying to run only the integration tests, while skipping unit tests.
I'm running the integration tests using failsafe plugin, and the plugin execution is bound to the integration-test phase.
Is there any way I can run only the integration tests, while skipping the unit tests (which are run via surefire).
My googling led to a page that shows me how to do the opposite.
If you want to skip the test you have two choices:
mvn -Dskip=true ...
which will not compile the tests nor execute them (logical ;-)). The other option is just to skip the execution of the tests but doesn't skip the compilation of the tests
mvn -DskipTests=true ...
But you can't skip the test lifecycle phase in Maven.

Resources