Search code examples
xmlscalamavenscalatestscala-maven-plugin

Scalatest from Maven: How to tag and filter out an entire Suite?


I have a Maven project and I'm using the scalatest-maven-plugin to configure scalatest. I'm using scalatest 3.0.0 however I can't manage to tag and filter out an entire Suite.

As reference, I have used the blog Tag a whole ScalaTest suite (update for Java 8) but this doesn't seem to work from Maven.

I created a new Skip tag defined as follows:

package tags;

import java.lang.annotation.*;

@org.scalatest.TagAnnotation
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface Skip {}

Then I tag my test Suite like this:

@tags.Skip
class AcceptanceTest extends FeatureSpec { ... 

I then configure my scalatest-maven-plugin like this:

<plugin>
    <groupId>org.scalatest</groupId>
    <artifactId>scalatest-maven-plugin</artifactId>
    <configuration>
        <tagsToExclude>tags.Skip</tagsToExclude>
    </configuration>
    <executions>
        <execution>
            <id>test</id>
            <goals>
                <goal>test</goal>
            </goals>
        </execution>
    </executions>
</plugin>    

Then running mvn clean install -X I see (which correctly passes the -l Tag exclusion CLI argument to Scalatest):

[DEBUG] Forking ScalaTest via: cmd.exe /X /C "java -Dbasedir=mydir 
        org.scalatest.tools.Runner -R -l tags.Skip ...

but the AcceptanceTest Suite gets nevertheless executed. I have also tried tagging the Suite like this without success:

class AcceptanceTest extends Tag("tags.Skip") with FeatureSpecLike { ...      

Solution

  • To separate execution of integration tests I used maven profiles:

        <profiles>
        <profile>
            <id>default-test</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>net.alchim31.maven</groupId>
                        <artifactId>scala-maven-plugin</artifactId>
                    </plugin>
                    <plugin>
                        <groupId>org.scalatest</groupId>
                        <artifactId>scalatest-maven-plugin</artifactId>
                        <version>1.0</version>
                        <configuration>
                            <reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory>
                            <junitxml>.</junitxml>
                            <filereports>WDF TestSuite.txt</filereports>
                            <tagsToExclude>org.example.testkit.annotations.IntegrationTestSuite</tagsToExclude>
                        </configuration>
                        <executions>
                            <execution>
                                <id>test</id>
                                <goals>
                                    <goal>test</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>integration-test</id>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>net.alchim31.maven</groupId>
                        <artifactId>scala-maven-plugin</artifactId>
                    </plugin>
                    <plugin>
                        <groupId>org.scalatest</groupId>
                        <artifactId>scalatest-maven-plugin</artifactId>
                        <version>1.0</version>
                        <configuration>
                            <reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory>
                            <junitxml>.</junitxml>
                            <filereports>WDF TestSuite.txt</filereports>
                            <tagsToInclude>org.example.testkit.annotations.IntegrationTestSuite</tagsToInclude>
                        </configuration>
                        <executions>
                            <execution>
                                <id>test</id>
                                <goals>
                                    <goal>test</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
    

    Then I marked integration tests accordingly

    @IntegrationTestSuite
    class ExampleTest extends PropSpec with MockitoSugar with BeforeAndAfterAll with EndpointTestHelper {
    

    to run unit tests I run

    mvn test
    

    for integration tests

    mvn test -Pintegration-test
    

    UPD:

    package org.example.testkit.annotations;
    
    import org.scalatest.TagAnnotation;
    
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    @TagAnnotation
    @Retention(RetentionPolicy.RUNTIME)
    @Target({ElementType.METHOD, ElementType.TYPE})
    public @interface IntegrationTestSuite {}
    

    We use scala 2.11, scalatest 2.2.6, maven 2, java 8.