Maven - How to compile tests without running them ?
Asked Answered
X

7

190

Is there a way in Maven to compile the tests without running them ? I want to use the IDE to run specific tests and not all of them.

Xylophagous answered 22/1, 2011 at 15:22 Comment(1)
You should probably either edit the question or change the accepted answer.Diplomate
W
28

you can try to use parameter -DskipTests

References:

Willywillynilly answered 22/1, 2011 at 15:26 Comment(10)
In netbeans, that is what i was doing. I see the following mvn -Dmaven.test.skip=true -Dnetbeans.execution=true clean install then i see the followingXylophagous
[compiler:testCompile] Not compiling test sources [surefire:test] Tests are skipped. Atleast using netbeans looks like if tests are skipped it does not compile test sourcesXylophagous
never mind, even though the logs say that, it still seems to compile test files.Xylophagous
this leads to skipping tests completely: execution as well as compilation, see the correct answer below: test-compile. I use it with mvn clean compile test-compile in EclipseLeeannaleeanne
@Leeannaleeanne you're right this aswer is not so clever... please accept answer bellow, so a can delete this oneWillywillynilly
@Leeannaleeanne You are talking about the skip option, aren't you? See this answer.Peanuts
@Willywillynilly As far as I know this is the best solution for the question! Until now I used 'maven.test.skip=true' which produces missing test dependencies in cases where tests are not necessary.Misapply
for some reason, tests still run if -DskipTests is set.Peel
you were supposed to delete the answer ( ͡° ͜ʖ ͡°)Swordsman
According with the documentation -DskipTests compiles the test classes but does not execute them (or the test classes are skipped for execution) - to avoid compilation and execution should be used: -Dmaven.test.skip=trueDiversity
S
431

How about the test-compile lifecycle phase? It doesn't require any test skipping, because it occurs before the test phase. I.e.,

$ mvn test-compile

And done.

Introduction to the Build Lifecycle explains further.

Sloth answered 25/1, 2011 at 16:21 Comment(4)
Just curious, what is the advantage of test-compile over -DskipTests? - according with the documentation -DskipTests compiles the test classes but does not execute them (or the test classes are skipped for execution) - so according with It doesn't require any test skipping - should I assume that is expensive?Diversity
-DskipTests is not a Maven lifecycle goal; it's just an option to set. You could run lots of goals with -DskipTests. The OP asked how to compile them without running them, so this satisfies that. I don't see any reason to think skipping tests would be expensive, but it does seem useless to run the test goal and use -DskipTests.Sloth
When that is done, is there a way to continue from this point? Without compiling again? Just running the tests next?Searcy
If you later execute e.g., mvn test, Maven will execute all of its lifecycle phases again, but as long as you haven't run clean, then your build artifacts will still be present and should be up-to-date, making the process much faster. Try it for yourself by running mvn test-compile; mvn test vs mvn test-compile; mvn clean testSloth
I
57

To just compile the tests and code, without running them, just do:

mvn test-compile
Invaluable answered 30/12, 2015 at 10:41 Comment(1)
I think test-compile includes compileAntilogism
P
39

When executing a goal that will include the testing phase (such as package), you can do two things:

  • Use the command mvn -DskipTests=true package. This will compile all tests but not run them.
  • Or mvn -Dmaven.test.skip=true package. This will not compile or run the test branch.
Propylite answered 19/5, 2013 at 9:25 Comment(0)
W
28

you can try to use parameter -DskipTests

References:

Willywillynilly answered 22/1, 2011 at 15:26 Comment(10)
In netbeans, that is what i was doing. I see the following mvn -Dmaven.test.skip=true -Dnetbeans.execution=true clean install then i see the followingXylophagous
[compiler:testCompile] Not compiling test sources [surefire:test] Tests are skipped. Atleast using netbeans looks like if tests are skipped it does not compile test sourcesXylophagous
never mind, even though the logs say that, it still seems to compile test files.Xylophagous
this leads to skipping tests completely: execution as well as compilation, see the correct answer below: test-compile. I use it with mvn clean compile test-compile in EclipseLeeannaleeanne
@Leeannaleeanne you're right this aswer is not so clever... please accept answer bellow, so a can delete this oneWillywillynilly
@Leeannaleeanne You are talking about the skip option, aren't you? See this answer.Peanuts
@Willywillynilly As far as I know this is the best solution for the question! Until now I used 'maven.test.skip=true' which produces missing test dependencies in cases where tests are not necessary.Misapply
for some reason, tests still run if -DskipTests is set.Peel
you were supposed to delete the answer ( ͡° ͜ʖ ͡°)Swordsman
According with the documentation -DskipTests compiles the test classes but does not execute them (or the test classes are skipped for execution) - to avoid compilation and execution should be used: -Dmaven.test.skip=trueDiversity
P
9

Alternatively, you can use maven.test.skip.exec option.

mvn -Dmaven.test.skip.exec=true

Maven will compile the tests without running them. I use this option in all my projects regularly.

Peanuts answered 26/2, 2015 at 10:42 Comment(1)
I was looking into having two pipeline steps: 1. build 2. test Using this option in step 1 then mvn surefire:test in step 2 seems to minimize duplication of things being run.Graminivorous
A
2

In case you really want to only compile the tests (skip all other phases like compile), this will do

mvn org.apache.maven.plugins:maven-compiler-plugin:3.1:testCompile

See the plugin bindings of the default lifecycle.

Ataghan answered 29/11, 2014 at 19:48 Comment(0)
E
0

If you settings.xml file you can also use

<maven.test.skip>true</maven.test.skip>
Endaendall answered 25/1, 2011 at 15:18 Comment(4)
You should never do that. If you need it, set it on the command line, but never permanently.Ibbetson
Never said you should or shouldn't do it. Just providing knowledge of the option.Endaendall
This causes maven not to compile the tests, either.Peisch
@Sean Patrick Floyd: why not? this is NOT permanent, that would be to set it in the pom.xml.Approbate

© 2022 - 2024 — McMap. All rights reserved.