2012-06-17 229 views
7

運行時mvn test maven不會運行所有的測試類。 當我通過添加-Dtest = PropertyTests顯式提供一個類時,測試將會運行。Maven不會運行測試

這裏是我的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> 

爲什麼行家不會自動運行測試?我錯過了什麼嗎?

UPDATE

實施例試驗(類在src /測試/ JAVA/COM /示例/ 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()); 
    } 
} 

Maven的輸出上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] ------------------------------------------------------------------------ 

回答

12

你需要添加maven surefire插件來運行測試。該配置可以在這裏找到: http://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html

UPDATE:

下面是我一直使用同規格/ 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:

的命名規則是測試,所以更改PropertyTests到PropertyTest。

+0

添加surefire插件後,它仍然不會運行測試(測試運行:0)。查看代碼更新的問題。 – dtrunk

+0

您的測試是否在src/test/java中?你的測試是什麼樣子的?你從maven獲得什麼輸出? – Noah

+2

我想你可能需要從'PropertyTests'中刪除's'。 – Noah