【问题标题】:Maven won't run testsMaven 不会运行测试
【发布时间】:2012-06-19 19:27:56
【问题描述】:

当运行mvn test 时,maven 不会运行所有的测试类。 当我通过添加 -Dtest=PropertyTests 明确提供类时,将运行测试。

这是我的 pom.xml:

<configuration>
    <includes>
        <include>**/*Spec.*</include>
        <include>**/*Test.*</include>
    </includes>
</configuration>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>example</artifactId>
    <version>1.0</version>

    <properties>
        <java-version>1.6</java-version>
        <org.slf4j-version>1.6.6</org.slf4j-version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>

        <!-- Logging -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${org.slf4j-version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>${org.slf4j-version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${org.slf4j-version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.16</version>
            <scope>runtime</scope>
        </dependency>

        <!-- Test -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>

        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${java-version}</source>
                    <target>${java-version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

为什么 maven 不会自动运行测试?我错过了什么吗?

示例测试(类在 src/test/java/com/example/PropertyTests.java):

public final class PropertyTests
{
    @Test
    public void testGetters()
    {
        Property property = new Property( "foo", "bar" );

        Assert.assertEquals( "foo", property.getKey() );
        Assert.assertEquals( "bar", property.getValue() );
    }
}

mvn test 上的 Maven 输出:

$ mvn test
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building Unnamed - com.example:example:jar:1.0
[INFO]    task-segment: [test]
[INFO] ------------------------------------------------------------------------
[INFO] [resources:resources {execution: default-resources}]
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] [compiler:compile {execution: default-compile}]
[INFO] Compiling 13 source files to /home/danny/workspace/example/target/classes
[INFO] [resources:testResources {execution: default-testResources}]
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] [compiler:testCompile {execution: default-testCompile}]
[INFO] Compiling 3 source files to /home/danny/workspace/example/target/test-classes
[INFO] [surefire:test {execution: default-test}]
[INFO] Surefire report directory: /home/danny/workspace/example/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 10 seconds
[INFO] Finished at: Sun Jun 17 18:09:45 CEST 2012
[INFO] Final Memory: 17M/42M
[INFO] ------------------------------------------------------------------------

【问题讨论】:

    标签: maven maven-2 junit junit4 maven-surefire-plugin


    【解决方案1】:

    您需要添加 maven surefire 插件来运行测试。配置可以在here找到。

    这是我一直在使用 specs/junit 的配置。

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.8.1</version>
        <configuration>
            <includes>
                <include>**/*Spec.*</include>
                <include>**/*Test.*</include>
            </includes>
        </configuration>
    </plugin>
    

    命名约定是Test,所以把PropertyTests改成PropertyTest

    【讨论】:

    • 添加surefire插件后,它仍然不会运行测试(测试运行:0)。请参阅更新的代码问题。
    • 你的测试在 src/test/java 中吗?你的测试是什么样的?你从 maven 得到什么输出?
    • 我认为您可能需要从“PropertyTests”中删除“s”。
    • 不幸的是,这对我不起作用。我尝试了每一种可能性......我不知道为什么......我正在做一个大项目,特别是在整个项目的一个模块中。我有 3 个测试类 FirstTest、SecondTest、ThirdTest(在代码中是相同的)并且我收到 3 条不同的消息。第一个 BUILD SUCCESS 测试运行 3。第二个 BUILD SUCCESS 测试运行 0。第三个 BUILD FAILURE。我很想解决这个问题
    • 问题是错误的导入。我使用了org.junit.Testorg.testng.annotations.Test 的意图,所以我反复收到错误。无论如何,谢谢你;-)
    猜你喜欢
    • 2012-04-06
    • 1970-01-01
    • 1970-01-01
    • 2014-08-24
    • 2021-10-19
    • 2014-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多