首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Maven的Scalatest :如何标记和过滤出整个套件?

Maven的Scalatest :如何标记和过滤出整个套件?
EN

Stack Overflow用户
提问于 2016-10-12 08:10:34
回答 2查看 2.9K关注 0票数 2

我有一个Maven项目,我使用最有规模的插件来配置scalatest。我使用的是最有规模的3.0.0,但是我无法标记和过滤掉整个套件。

作为参考,我使用了博客标记整个ScalaTest套件(更新为Java 8),但这似乎不适用于Maven。

我创建了一个新的Skip标记,定义如下:

代码语言:javascript
复制
package tags;

import java.lang.annotation.*;

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

然后我给我的测试套件贴上这样的标签:

代码语言:javascript
复制
@tags.Skip
class AcceptanceTest extends FeatureSpec { ... 

然后,我将我的最大规模的maven插件配置如下:

代码语言:javascript
复制
<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>    

然后运行mvn clean install -X (它正确地将-l标记排除CLI参数传递给Scalatest):

代码语言:javascript
复制
[DEBUG] Forking ScalaTest via: cmd.exe /X /C "java -Dbasedir=mydir 
        org.scalatest.tools.Runner -R -l tags.Skip ...

但是AcceptanceTest套件仍然会被执行。我也试着把这套套房贴上这样的标签,但没有成功:

代码语言:javascript
复制
class AcceptanceTest extends Tag("tags.Skip") with FeatureSpecLike { ...      
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-10-12 09:05:32

为了分离集成测试的执行,我使用了maven配置文件:

代码语言:javascript
复制
    <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>

然后,我相应地标记了集成测试。

代码语言:javascript
复制
@IntegrationTestSuite
class ExampleTest extends PropSpec with MockitoSugar with BeforeAndAfterAll with EndpointTestHelper {

要运行单元测试,我要运行

代码语言:javascript
复制
mvn test

用于集成测试

代码语言:javascript
复制
mvn test -Pintegration-test

UPD:

代码语言:javascript
复制
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 {}

我们使用Scala2.11,scalatest 2.2.6,maven 2,java 8。

票数 4
EN

Stack Overflow用户

发布于 2019-11-14 08:56:03

您可以传递动态需要的标记,而无需创建新的配置文件:

代码语言:javascript
复制
<plugin>
    <groupId>org.scalatest</groupId>
    <artifactId>scalatest-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>test</id>
            <goals>
                <goal>test</goal>
            </goals>
            <configuration>
                <tagsToInclude>${tagsToInclude}</tagsToInclude>
                <tagsToExclude>${tagsToExclude}</tagsToExclude>
            </configuration>
        </execution>
    </executions>
</plugin>  

一些标签定义示例:

代码语言:javascript
复制
package com.foo.tags;

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 Skip {}

最后,您可以通过以下方式动态地从CLI传递标记:

代码语言:javascript
复制
mvn -DtagsToExclude=com.foo.tags.Skip test
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39993622

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档