Maven: disabling test phase but not integration test phase - maven

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.

Related

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.

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.

How can I atomically run mvn tests (without rebuilding source code)?

I want to run a maven project lifecycle starting, and ending, with the unit tests.
How can I skip recompilation and re-resolution of dependencies and only run the test phase?
If you start maven by calling a phase it will execute all lifecycle phases up to the one you are calling. For example, when calling
mvn test
all the phases before the test lifecycle phase will be execute too: the project will be validated, sources and resources will be generated and processed, sources will be compiled, the same will happen to test sources and resources and finally unit tests will be run.
But you can also call the plugin goal that is bound to a lifecycle phase. In the case of the test phase the bound goal is surefire's test mojo. So you could call
mvn surefire:test
and no other lifecycle phase will be executed.
You can find the goals bound to each phase depending on the package type here.
You can run :
mvn surefire:test
Build your own lifecycle, or run the tests with something besides Maven (Ant, Gradle, your IDE, command-line JUnit runner, ...). That's the kind of restriction you live with when you're using Maven.

Can I run post processing on my maven integration test results if there are test failures?

I want to extend a maven2 POM to run some post processing on my integration test results. I added a java:exec plugin and bound it to the post-integration-test phase.
All is fine when the tests pass but if I get any test failures then the post processing isn't executed.
Question: Is there an option to force a task, regardless of the results of the integration test phase?
Our POM was using the Surefire plugin to execute the integration tests but it looks like that was a mistake. The Failsafe plugin is intended to address this issue. From the documentation:
If you use the Surefire Plugin for running tests, then when you have a test failure, the build will stop at the integration-test phase and your integration test environment will not have been torn down correctly. The Failsafe Plugin is used during the integration-test and verify phases of the build lifecycle to execute the integration tests of an application. The Failsafe Plugin will not fail the build during the integration-test phase thus enabling the post-integration-test phase to execute.
http://maven.apache.org/plugins/maven-failsafe-plugin/

Resources