Maven不会运行测试 [英] Maven won't run tests

查看:82
本文介绍了Maven不会运行测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行mvn test时,maven不会运行所有测试类. 当我通过添加-Dtest = PropertyTests显式提供一个类时,将运行测试.

When running mvn test maven won't run all Test Classes. When I explicitly provide a class by adding -Dtest=PropertyTests the tests will be run.

这是我的pom.xml:

Here's my pom.xml:

<?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不会自动运行测试?我错过了什么吗?

Why maven won't run the tests automatically? Did I missed something?

更新

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

Example test (Class is in 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输出:

Maven output on mvn test:

$ 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 surefire插件才能运行测试.可以在这里找到配置: http://maven.apache.org/surefire/maven -surefire-plugin/examples/inclusion-exclusion.html

You need to add the maven surefire plugin to run the tests. The configuration can be found here: http://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html

更新:

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

Here's a configuration that I've been using with 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>

更新2:

命名约定为Test,因此将PropertyTests更改为PropertyTest.

The naming convention is Test, so change PropertyTests to PropertyTest.

这篇关于Maven不会运行测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆