176

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.

Jeanne Boyarsky
  • 12,156
  • 2
  • 49
  • 59
user373201
  • 10,945
  • 34
  • 112
  • 168

7 Answers7

418

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.

RonU
  • 5,525
  • 3
  • 16
  • 13
  • 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? – Manuel Jordan Aug 28 '21 at 03:30
  • `-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`. – RonU Aug 31 '21 at 05:56
  • When that is done, is there a way to continue from this point? Without compiling again? Just running the tests next? – Chris Pillen Oct 16 '21 at 08:15
  • 1
    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 test` – RonU Oct 20 '21 at 04:36
51

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

mvn test-compile compile
blackgreen
  • 34,072
  • 23
  • 111
  • 129
orange77
  • 933
  • 8
  • 12
37

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.
Martins
  • 1,231
  • 2
  • 14
  • 17
26

you can try to use parameter -DskipTests

References:

Stephan
  • 41,764
  • 65
  • 238
  • 329
lweller
  • 11,077
  • 3
  • 34
  • 38
  • 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 following – user373201 Jan 22 '11 at 15:57
  • [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 sources – user373201 Jan 22 '11 at 15:58
  • 1
    never mind, even though the logs say that, it still seems to compile test files. – user373201 Jan 22 '11 at 16:31
  • 38
    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 Eclipse – Yashima Jun 26 '12 at 12:32
  • 3
    @Yashima you're right this aswer is not so clever... please accept answer bellow, so a can delete this one – lweller Jul 02 '12 at 15:12
  • @Yashima You are talking about the `skip` option, aren't you? See this [answer](http://stackoverflow.com/a/28740251/363573). – Stephan Feb 26 '15 at 11:09
  • @lweller 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. – TekTimmy Jun 26 '15 at 08:30
  • for some reason, tests still run if -DskipTests is set. – adapt-dev Feb 12 '16 at 21:32
  • you were supposed to delete the answer ( ͡° ͜ʖ ͡°) – Line Feb 18 '19 at 12:30
  • 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=true` – Manuel Jordan Aug 28 '21 at 03:40
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.

Stephan
  • 41,764
  • 65
  • 238
  • 329
  • 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. – tishma Dec 01 '20 at 13:12
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.

schnatterer
  • 7,525
  • 7
  • 61
  • 80
0

If you settings.xml file you can also use

<maven.test.skip>true</maven.test.skip>
menapole
  • 1,166
  • 1
  • 11
  • 22