13

I would like to skip a single test (e.g. com.example.MyTest) when building a project with Maven from the command line.

I'm aware of similar questions like this one, but they all require either modification of source code or pom.xml. I would like to do without modifications. How can I exclude a test using command line options only?

What I've tried so far after reading some documentation is

mvn clean install -Dtest="*,!com.example.MyTest"

but the test is still not skipped. I'm using surefire plugin version 2.19 and JUnit 4.11.

Community
  • 1
  • 1
Mifeet
  • 12,949
  • 5
  • 60
  • 108

1 Answers1

16

It turns out that (at least in Surefire 2.19), the test pattern doesn't work with fully qualified class names. So the right solution is

mvn clean install -Dtest="*,!MyTest"

i.e. without the package path.

In Surefire 2.19.1, it should be possible to use fully qualified names. In versions older than 2.19, neither seems to work.

Mifeet
  • 12,949
  • 5
  • 60
  • 108
  • 1
    It didn't work for me :( This is how I'm using it: ` test -Dlog4j.configuration=log4j-dm.xml -Dtest="*,!Testname1Test,!Testname2Test,!Testname3Test,!Testname4Test" `. – vianna77 Jul 04 '16 at 15:59
  • What version of surefire are you using? – Mifeet Jul 07 '16 at 14:33
  • Version: [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) – vianna77 Jul 07 '16 at 18:24
  • I guess that your version of Surefire doesn't support excluding tests like this. Try upgrading to 2.19. – Mifeet Jul 08 '16 at 07:46
  • 1
    When I start Jenkins, it says the version is maven-surefire-plugin:2.12.4. I already updated the pom version my app, but I don't know how to do that in Jenkins. I'm still searching how to do that. – vianna77 Jul 08 '16 at 10:33
  • I had to escape the "!" (use "\!" instead) otherwise I get a bash "event not found" error, see https://serverfault.com/questions/208265/what-is-bash-event-not-found – Frédéric Camblor Sep 04 '20 at 08:59