Maven自动跳过测试

35

Maven为什么默认跳过所有测试?我有一个pom.xml文件,其中有几个配置文件,但无法通过它们中的任何一个运行我的测试。其中一个配置文件如下:

<profile>
        <id>jsf-test</id>
        <dependencies>
            <dependency>
                <groupId>org.jboss.as</groupId>
                <artifactId>jboss-as-arquillian-container-remote</artifactId>
                <version>${jboss.version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>com.jsf.tests</groupId>
                <artifactId>jsf-app</artifactId>
                <version>${jsf-app.version}</version>
                <type>war</type>
            </dependency>
        </dependencies>
        <build>
            <plugins>                
                <plugin>
                    <artifactId>maven-dependency-plugin</artifactId>
                    <version>2.6</version>
                    <executions>
                        <execution>
                            <id>copy-jsf-app</id>
                            <phase>validate</phase>
                            <goals>
                                <goal>copy</goal>
                            </goals>
                            <configuration>
                                <artifactItems>
                                    <artifactItem>
                                        <groupId>com.jsf.tests</groupId>
                                        <artifactId>jsf-app</artifactId>
                                        <version>${jsf-app.version}</version>
                                        <type>war</type>
                                        <destFileName>jsfapp.war</destFileName>
                                        <outputDirectory>target</outputDirectory>
                                    </artifactItem>
                                </artifactItems>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>

                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>${maven-surefire.version}</version>
                    <configuration>
                        <skipTests>false</skipTests> <!-- desperate trial -->
                        <properties>
                            <property>
                                <name>listener</name>
                                <value>${testng.listeners}</value>
                            </property>
                        </properties>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
如果我运行mvn verify -Pjsf-test命令,则项目将被编译,jsf-app构件将正确复制到目标目录中,并且测试将被跳过。使用mvn verify -Dtest=TestCalculator命令会得到相同的结果。我正在使用ArquillianTestNG执行实际的测试,但我不确定这是否与此问题有关。 编辑 在调试模式下运行将提供(相关部分)
[DEBUG]   (s) reportFormat = brief
[DEBUG]   (s) reportsDirectory = /home/pmensik/Work/workspace/epp-test/cdi-arquillian-    test/target/surefire-reports
[DEBUG]   (f) reuseForks = true
[DEBUG]   (s) runOrder = filesystem
[DEBUG]   (s) skip = true
[DEBUG]   (s) skipTests = false
[DEBUG]   (s) systemPropertyVariables = {jsfPortlet=true}
[DEBUG]   (s) testClassesDirectory = /home/pmensik/Work/workspace/epp-test/cdi-arquillian-test/target/test-classes
[DEBUG]   (s) testFailureIgnore = false
[DEBUG]   (s) testNGArtifactName = org.testng:testng
[DEBUG]   (s) testSourceDirectory = /home/pmensik/Work/workspace/epp-test    /cdi-arquillian-test/src/test/java
[DEBUG]   (s) trimStackTrace = true
[DEBUG]   (s) useFile = true
[DEBUG]   (s) useManifestOnlyJar = true
[DEBUG]   (s) useSystemClassLoader = true
[DEBUG]   (s) useUnlimitedThreads = false
[DEBUG]   (s) workingDirectory = /home/pmensik/Work/workspace/epp-test/cdi-arquillian-test
[DEBUG]   (s) project = MavenProject: org.jboss.gatein.test:cdi-portlet-test:6.1-ER01 @ /home/pmensik/Work/workspace/epp-test/cdi-arquillian-test/pom.xml
[DEBUG]   (s) session = org.apache.maven.execution.MavenSession@3c3483ec
[DEBUG] -- end configuration --
[INFO] Tests are skipped.

我的最简单的测试看起来像这样

public class Test {

    @Drone
    protected WebDriver driver;

    @Deployment(testable = false)
    public static WebArchive createTestArchive() {
        return ShrinkWrap.createFromZipFile(WebArchive.class, new File("target/CDIPortlet.war"));
    }

    @Test
    public void testCase{
        //...
    }

}

你确定你没有以某种方式运行 -DskipTests 吗?mvn clean verifymvn clean install 做了什么? - vikingsteve
1
尝试使用“-X”运行以获取调试输出,然后查看surefire插件的配置。我还怀疑是插件继承问题。surefire插件是否在父POM中配置? - user944849
你的测试在哪里定义的? - John Ament
@vikingsteve - 得到完全相同的结果 - Petr Mensik
@user944849 - 请看我的编辑。是的,我正在继承一个非常简单的 POM,其中只有在 pluginManagement 中定义了 Surefire 插件并跳过了配置,没有其他内容。 - Petr Mensik
@PetrMensik 好的,maven-surefire-plugin 的默认配置是否有包含或排除模式?请查看所有父级pom。如果有的话,在您的配置文件 <configuration> 中添加 <excludes> <exclude>none</exclude> </excludes> 可能会有所帮助。 - vikingsteve
1个回答

41

调试输出显示如下:

[DEBUG]   (s) skip = true

该选项不仅跳过运行测试,还会跳过编译测试。如果你好奇,检查父POM(由此POM直接引用的任何公司POM或Arquillian引入的超级POM)以查看此标志在哪里设置。

解决方法是添加:

<skip>false</skip>

将此模块中的surefire插件配置添加,或者进行修改。

-Dmaven.test.skip=false

到您的命令行。

参考


8
<skipTests>true</skipTests> 的意思是编译测试代码,但不运行测试代码。 - Nikita Bosik
4
<skip>true</skip> -- 这表示测试甚至没有编译。 - Nikita Bosik
1
这对我来说是有效的,但我不知道为什么它有效。因为我的任何pom.xml文件都没有将其设置为false。 - C.J.
我认为在POM中有<skip>true</skip>的情况下,在命令中添加-Dmaven.test.skip=false会被忽略。只是一点小提示。无论如何,直到我将其更改为<skip>false</skip>之前,它都没有起作用。 - Nom1fan
1
@Nom1fan - 正确。如果值像那样被硬编码,属性中的值将无法再使用。如果您可以访问POM,则删除硬编码并使用属性是正确的方法。 - user944849

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接