Is there a way using surefire to exclude tests at a test method level - not a class level?
Asked Answered
M

3

6

The Apache Maven Surefire site has the following example syntax to exclude tests from running:

<configuration>
  <excludes>
    <exclude>**/TestCircle.java</exclude>
    <exclude>**/TestSquare.java</exclude>
  </excludes>
</configuration>

This excludes classes, but if the test you want to exclude was in a class with a bunch of other tests - then every test in the class will be excluded, regardless of whether you only wanted to exclude the one test or not.

This doesn't seem very granular. I'd like to exclude just one test from a class that has multiple tests.

My question is: Is there a way using surefire to exclude tests at a test method level - not a class level?

Meteoroid answered 21/10, 2015 at 2:33 Comment(0)
J
2

It's 2023 and the answer is: You can.

With with property excludesFile, you can refer to a file which contains exclude patterns, and as docs say,

Since 3.0.0-M6, method filtering support is provided in the exclusions file as well, example

file excludedTests.txt:

pkg.SomeTest#testMethod

So for example in the <configuration> section you need to add

<configuration>
  <excludesFile>excludedTests.txt</excludesFile>
</configuration>

and during execution, the testMethod is not invoked.

Judicator answered 10/7, 2023 at 12:15 Comment(0)
M
2

Use JUnit >= 4.8 and Categories => http://maven.apache.org/surefire/maven-surefire-plugin/examples/junit.html

1. Add : public interface SlowTests{} 

2.  public class AppTest {
      @Test
      @Category(com.mycompany.SlowTests.class)
      public void testSlow1() {
        System.out.println("testSlow1");
      }

      @Test
      @Category(com.mycompany.SlowTests.class)
      public void testSlow2() {
        System.out.println("testSlow2");
      }

      @Test 
      public void test3() {
        System.out.println("test3");
      }

        @Test 
      public void test4() {
        System.out.println("test4");
      }
    }

3.
 <plugin>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>2.11</version>
      <configuration>
        <groups>com.mycompany.SlowTests</groups>
      </configuration>
    </plugin>
Myalgia answered 21/10, 2015 at 8:20 Comment(0)
J
2

It's 2023 and the answer is: You can.

With with property excludesFile, you can refer to a file which contains exclude patterns, and as docs say,

Since 3.0.0-M6, method filtering support is provided in the exclusions file as well, example

file excludedTests.txt:

pkg.SomeTest#testMethod

So for example in the <configuration> section you need to add

<configuration>
  <excludesFile>excludedTests.txt</excludesFile>
</configuration>

and during execution, the testMethod is not invoked.

Judicator answered 10/7, 2023 at 12:15 Comment(0)
F
1

I don't think there is an option for that - not even a hidden one. JUnit itself gets a list of files it should scan for test execution. See JUnitCore.

The surefire plugin allows to distinguish tests in groups. See also this answer on Stackoverflow and this Blog.

That might be an option?

Frustule answered 21/10, 2015 at 7:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.