0

You can skip executing the tests via command line by the following command:

mvn install -DskipTests

You can also skip executing of a specific unit test like that:

mvn -Dtest=\!com.example.ExampleTest#testName install

I know that you can also use the maven.test.skip property to skip compiling the tests

mvn install -Dmaven.test.skip=true

Is there a way to skip compiling of a specific unit test from command line? In other words, I do not want a particular test to be included in a target folder (without deleting the test code, of course).

glytching
  • 44,936
  • 9
  • 114
  • 120
Kote
  • 683
  • 1
  • 9
  • 27
  • You could just comment out the Test annotation. `// @Test`. – Thorbjørn Ravn Andersen Mar 19 '18 at 17:11
  • I am implementing a script, which means that I want to get it done by command line. It is straightforward to do it manually – Kote Mar 19 '18 at 17:27
  • I do not understand your explanation. If you want to disable just one test of many, have it in its own class then. – Thorbjørn Ravn Andersen Mar 19 '18 at 20:31
  • I understand what you mean. The problem is that I want to make this script as much programmatic as possible and exclude the manual work. I'm doing some research about mutation testing. Imagine that I run tests 100+ times on different sources, I cannot create own class for each case manually 100+ times... or comment out a line as you said. I want a command to be able to do that – Kote Mar 19 '18 at 20:51
  • I am using Pitest and unfortunately it does not have support to exclude one specific test when executing tests (like maven does with -Dtest param). It seems that only option for me is to add this feature to PIT tool – Kote Mar 19 '18 at 20:54
  • You may want to look into parameterized testing. This allows running a single test on each entry of a large dataset and moves the configuration out of maven and into your testing framework. – Thorbjørn Ravn Andersen Mar 19 '18 at 21:12
  • If your current test framework does not support that, reconsider your choice of test framework. – Thorbjørn Ravn Andersen Mar 21 '18 at 16:31

1 Answers1

1

To exclude a test class from compilation by Maven you can add an testExclude to your maven-compiler-plugin declaration.

For example:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.7.0</version>
    <configuration>
      <!-- use testExcludes to exclude a class from the src/test tree -->
      <testExcludes>
        <testExclude>/path/to/your/class</testExclude>
      </testExcludes>
    </configuration>
</plugin>

Update based on this:

Kote> I'm looking to exclude a single/multiple unit test, not the whole class

glytching> You want the class to be compiled and run by Surefire but you want the compiled form to exclude an individual test (i.e. a method) within that class?

Kote> Yes

You can ...

  • Tell Maven not to execute any tests by using -DskipTests.
  • Tell Maven not to execute a specific test case by using -Dtest=\!com.example.ExampleTest.
  • Tell Maven not to execute a specific test within a specific test case by using -Dtest=\!com.example.ExampleTest#testName.
  • Tell Maven not to compile any classes in the test tree by using -Dmaven.test.skip.
  • Tell Maven not to compile a specific class in the test tree by using maven-compile-plugin::testExcludes.

But you cannot tell Maven not to compile a specific method within a class.

It sounds like you need a conditional compilation solution. Or perhaps you should edit the class before compilation. Either way, there's no built-in support for this in Maven (or in Java).

Community
  • 1
  • 1
glytching
  • 44,936
  • 9
  • 114
  • 120
  • Thank you for the answer. But I'm looking to exclude a single/multiple unit test, not the whole class and also execute it from the command line. Sorry for confusion, I edited the question. – Kote Mar 19 '18 at 16:39
  • You want the class to be compiled and run by Surefire but you want the compiled form to exclude an individual test (i.e. a method) within that class? – glytching Mar 19 '18 at 16:42
  • Yes, I know it sounds weird, but if you're interested I can explain why I want to do that. But before that do you think it is possible? – Kote Mar 19 '18 at 16:45