132

Actuality when i run tests they fails but i need to run them to get some .class files which are very important for my jar.

By default when test results fails , the jar is not build , could i add a setting in pom.xml which ignore that, so I can build the jar ignoring results from tests ?

I read something about "Maven Surefire Plugin" but I don't know how to use it...

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
user398920
  • 1,421
  • 2
  • 12
  • 10
  • This question asks about continuing with the packaging of the current Maven project/module even if tests fail. It is therefore not a duplicate of https://stackoverflow.com/questions/4174696/making-maven-run-all-tests-even-when-some-fail because that is about continuing to build other modules in a multi-module reactor build. While possible answers have some overlap, the context is totally different. – Falko Menge Sep 08 '20 at 06:41

6 Answers6

213

Please refer to surefire:test for details, but the most useful properties are:

-Dmaven.test.failure.ignore=true (or -DtestFailureIgnore=true) - will ignore any failures occurred during test execution

-Dmaven.test.error.ignore=true ( deprecated ) - will ignore any errors occurred during test execution

-DskipTests - would compile the test classes but skip test execution entirely

-Dmaven.test.skip=true - would not even compile the tests

I believe that in your case where you want to compile test classes but not fail the build due to any tests errors and still create the jar.

You should use the first option to ignore any test failures which you can still review once the build has finished.

Zilvinas
  • 5,518
  • 3
  • 26
  • 26
  • 1
    Anyone know why "-Dmaven.test.error.ignore=true" is not being used anymore? I finding the need to not ignore actual failures, but ignore errors (which may not actually cause the test to "fail") in some cases. Is there any way to distinguish the two now? – PAULUS Oct 25 '13 at 21:20
  • You could use an older maven surefire plugin version to achieve this – Zilvinas May 17 '14 at 07:49
  • Build fails when I use -DtestFailureIgnore=true, whereas -DtestFailureIgnore=true works. I use maven-3.2.1 – divideByZero Nov 30 '15 at 09:10
  • 1
    @divideByZero too much copy paste, you just wrote the same thing twice. So which way does it (not) work? – Zilvinas Nov 30 '15 at 20:55
  • @Zilvinas maven.test.failure.ignore worked, but testFailureIgnore did not work for me with Maven-2.2.1 – Kingsly Dec 28 '15 at 19:49
  • 1
    @Kingsly as specified in http://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html the `testFailureIgnore` is a user property, I believe you could set it inside your `pom.xml`. In general, they both should do the same thing. And it is not your `maven` version that matters, but the _plugin_ version – Zilvinas Jan 08 '16 at 20:45
49

mvn -Dmaven.test.skip=true package skips the surefire test mojo.

to ignore test failures and keep maven from stopping you can add this to the section of the pom.xml:

 <plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-surefire-plugin</artifactId>
   <configuration>
     <testFailureIgnore>true</testFailureIgnore>
   </configuration>
 </plugin>
fasseg
  • 17,504
  • 8
  • 62
  • 73
  • Thanks a lot for your post, i added that part and it worked , any way, my problem was not solved because , the .class files which result after running tests now are missing... and i need those... somehow... even if test results fails – user398920 Jul 29 '10 at 18:15
  • i added a new part to the answer regarding surefire plugin config for ignoring test failures...hope that helps.. – fasseg Jul 29 '10 at 19:16
  • 4
    `mvn -Dmaven.test.skip=true package skips the testing phase`. **No it doesn't!**. You can't skip a phase by using a system property. What it does is set a flag that skips execution of the surefire:test mojo and the compiler:test-compile mojo (possibly others) : http://maven.apache.org/plugins/maven-surefire-plugin/test-mojo.html#skip http://maven.apache.org/plugins/maven-compiler-plugin/testCompile-mojo.html#skip – Sean Patrick Floyd Jul 30 '10 at 10:53
  • 5
    That's not nitpicking, that's correctness. And all these answers suggesting to skip tests are incorrect anyway, they missed the point: the question is not about skipping tests, it's about ignoring tests failures. – Pascal Thivent Jul 30 '10 at 11:08
30

The solution is:

mvn -fn clean install

execute mvn --help for advanced options

Here's the excerpt for -fn

 -fn,--fail-never         NEVER fail the build, regardless
                          of project result
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • 1
    this is not the best solution, as it will stop building the project that failed, meaning that whatever goals where set to be done after test execution ( package, install, deploy & etc ) will not be executed for that project. So the "full build" will run till the end, but that project will not be installed to repo and the previous version of the jar will be used where it is defined as a dependency – Zilvinas Jul 26 '13 at 06:19
  • @Zilvinas so what *is* the best solution? – Sean Patrick Floyd Jul 26 '13 at 07:51
  • see the answer below: http://stackoverflow.com/a/16690564/344477 – Zilvinas Sep 24 '13 at 07:37
20
<properties>
<maven.test.skip>true</maven.test.skip>
<maven.test.failure.ignore>true</maven.test.failure.ignore>
</properties>

http://jira.codehaus.org/browse/SUREFIRE-319

Or from command line

http://maven.apache.org/maven-1.x/plugins/test/properties.html

maven.test.error.ignore Yes Set this to true to ignore errors during testing. Its use is NOT RECOMMENDED, but quite convenient on occasion

dablak
  • 1,376
  • 1
  • 11
  • 21
I82Much
  • 26,901
  • 13
  • 88
  • 119
  • Thanks a lot for your post, i added that part and it worked , any way, my problem was not solved because , the .class files which result after running tests now are missing... and i need those... somehow... even if test results fails – user398920 Jul 29 '10 at 18:13
  • sorry i was wrong, removing "true" part i get the right results – user398920 Jul 29 '10 at 18:22
  • 1
    for me it appears that -Dmaven.test.failure.ignore=true works from the command line but -Dmaven.test.error.ignore=true does nothing. – rogerdpack Jan 19 '12 at 21:18
  • @rogerdpack: failures and errors are different in some testing frameworks; from your message, I guess you were getting failures and not errors. – Blaisorblade Jul 19 '12 at 17:56
  • I would not recommend setting this property in the pom.xml, but rather only run it when really required by adding this directly from command line – Zilvinas May 22 '13 at 11:14
  • true works for me, while org.apache.maven.plugins maven-surefire-plugin true not. Thanks – PenguinEngineer Mar 28 '17 at 07:50
4

Use -DskipTests=true instead of -Dmaven.test.skip=true in order to skip tests but compile them.

Using -Dmaven.test.failure.ignore=true will also work but is not very nice.

Jens
  • 67,715
  • 15
  • 98
  • 113
Julien Carsique
  • 3,915
  • 3
  • 22
  • 28
  • 1
    If `test-jar` type, generated from test Class is used as dependency in other module whole compilation will fail if we use '-Dmaven.test.skip=true'. While -DskipTests=true compiles and generates jar but doesn't run test. :) – Shiva Jun 27 '17 at 07:40
2

Use the maven option -Dmaven.test.skip=true

E.g. mvn package -Dmaven.test.skip=true

Jordan Allan
  • 4,408
  • 7
  • 32
  • 35
  • Thanks a lot for your post, i added that part and it worked , any way, my problem was not solved because , the .class files which result after running tests now are missing... and i need those... somehow... even if test results fails – user398920 Jul 29 '10 at 18:14
  • 5
    This will skip the tests, the question is more about ignoring failures. – Pascal Thivent Jul 29 '10 at 21:24