0

I have looked at a few other SO questions like this and this. But those questions are pretty dated and I'm curious if there exists a new solution.

Here's what my setup looks like:

Category interface:

public interface FastTest{}

Category suite:

@RunWith(Categories.class)
@IncludeCategory(FastTest.class)
public class FastSuite{}

Sample test:

@Category(FastTest.class)
public class FastTests{

    @Test public void assertOneFastTest(){}

    @Test public void assertTwoFastTest(){}
}

Using maven, let's say I want to only run all my FastTest tests. Ideally, I would use the command

mvn test -Dtest.category=FastTest

or

mvn test -Dtest.suite=FastSuite

But I have not been able to get this working. Does anyone have any suggestions aside from using ClasspathSuite? Thanks.

Community
  • 1
  • 1
jamesfzhang
  • 4,403
  • 8
  • 32
  • 43

2 Answers2

2

You can do this from the surefire plugin, using the configuration for groups, but you need to specify the junit47 provider as well. The following works for me:

<plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.11</version>
    <dependencies>
        <dependency>
            <groupId>org.apache.maven.surefire</groupId>
            <artifactId>surefire-junit47</artifactId>
            <version>2.11</version>
        </dependency>
    </dependencies>
    <executions>
        <execution>
            <id>default-test</id>
            <phase>test</phase>
            <goals>
                <goal>test</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <groups>com.xxx.foo.CategoryClass</groups>
    </configuration>
</plugin>

I think this was introduced fairly recently, so may not work in earlier versions of the plugin, pre 2.11. You need to specify the provider, otherwise it doesn't work. The groups should be the fully qualified name of the Category class. You can also specify an excludeGroups as well if needs be.

For more information, see Maven Surefire Plugin surefire:test.

Matthew Farwell
  • 60,889
  • 18
  • 128
  • 171
  • MANY thanks for the hint about the junit provider !!! To the contrary of what's documented, it should have been selected automatically. But that's visibly not the case. – Jan Goyvaerts Feb 09 '12 at 11:47
  • @JanGoyvaerts re: the junit provider, I've already raised an issue about that. It should be fixed in 2.12.1, but I need to supply a patch :-) – Matthew Farwell Feb 09 '12 at 11:56
  • I made it finally work by requesting explicitly the junit47 provider. I'm afraid failsafe has the same problem. – Jan Goyvaerts Feb 09 '12 at 13:31
  • Just in case ... the ticket i raised at Surefire https://jira.codehaus.org/browse/SUREFIRE-832. But I think there's a stability problem somewhere with the provider too. The system almost grinds to a halt when using it for Surefire and Failsafe. – Jan Goyvaerts Feb 09 '12 at 16:19
0

Did a little more research and found no options so I ended up using ClasspathSuite. It turns out that it's not any different except that you cannot specify @Category at a class level, you have to annotate every method you want to categorize

Now, each category suite looks like this:

@RunWith(Categories.class)
@Categories.IncludeCategory(FastTest.class)
@Suite.SuiteClasses(AllTests.class)
public class FastSuite{}

You have to modify AllTests to look like this:

@RunWith(ClasspathSuite.class)
public class AllTests{}

For methods, it looks like this:

public class FastTests{

    @Categories(FastTest.class)
    public void assertOneFastTest(){}

    @Categories(FastTest.class)
    public void assertTwoFastTest(){}
}

Using maven, you can do the following command:

mvn test -Dtest=FastSuite -Dt3-chrome-path=%CHROME_DRIVER_HOME -Dwebdriver.chrome.driver=%CHROME_DRIVER_HOME
jamesfzhang
  • 4,403
  • 8
  • 32
  • 43
  • It is not directly related, but this is a way to [compute 'Tests by Category' counter](https://stackoverflow.com/a/53778167/10524205). – Bsquare ℬℬ Jan 18 '19 at 12:47