2015-01-11 188 views
1

我想用Selenium測試我的Web應用程序,將測試類與Maven集成。當我給命令mvn集成測試時,測試類被識別,但測試沒有被執行。最終,構建是成功的。在mvn集成測試中沒有運行硒測試

 Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.228 sec 

Results : 

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

[INFO] ------------------------------------------------------------------------ 
[INFO] BUILD SUCCESS 
[INFO] ------------------------------------------------------------------------ 
[INFO] Total time: 13.915s 
[INFO] Finished at: Sun Jan 11 20:15:59 IST 2015 
[INFO] Final Memory: 43M/303M 
[INFO] ------------------------------------------ 

這裏是我的pom.xml文件

<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/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>Music</groupId> 
    <artifactId>Music</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>war</packaging> 
    <properties> 
     <spring.version>3.0.5.RELEASE</spring.version> 
     <spring.security.version>3.2.3.RELEASE</spring.security.version> 
    </properties> 
    <dependencies> 
     <dependency> 
      <groupId>mysql</groupId> 
      <artifactId>mysql-connector-java</artifactId> 
      <version>5.1.24</version> 
      <type>jar</type> 
      <scope>compile</scope> 
     </dependency> 
     <dependency> 
      <groupId>javax</groupId> 
      <artifactId>javaee-web-api</artifactId> 
      <version>7.0</version> 
      <scope>provided</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.tika</groupId> 
      <artifactId>tika-app</artifactId> 
      <version>1.5</version> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.tika</groupId> 
      <artifactId>tika-parsers</artifactId> 
      <version>1.5</version> 
     </dependency> 
     <dependency> 
      <groupId>commons-fileupload</groupId> 
      <artifactId>commons-fileupload</artifactId> 
      <version>1.3</version> 
     </dependency> 
     <dependency> 
      <groupId>javax.transaction</groupId> 
      <artifactId>jta</artifactId> 
      <version>1.1</version> 
     </dependency> 
     <dependency> 
      <groupId>org.json</groupId> 
      <artifactId>json</artifactId> 
      <version>20140107</version> 
     </dependency> 
     <dependency> 
      <groupId>org.jasypt</groupId> 
      <artifactId>jasypt</artifactId> 
      <version>1.9.0</version> 
     </dependency> 
     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>4.11</version> 
     </dependency> 
     <dependency> 
      <groupId>org.dbunit</groupId> 
      <artifactId>dbunit</artifactId> 
      <version>2.4.9</version> 
     </dependency> 
     <dependency> 
      <groupId>org.slf4j</groupId> 
      <artifactId>slf4j-simple</artifactId> 
      <version>1.6.1</version> 
     </dependency> 
     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-core</artifactId> 
      <version>3.6.3.Final</version> 
     </dependency> 
     <dependency> 
      <groupId>javassist</groupId> 
      <artifactId>javassist</artifactId> 
      <version>3.12.1.GA</version> 
     </dependency> 
     <dependency> 
      <groupId>org.jmock</groupId> 
      <artifactId>jmock-junit4</artifactId> 
      <version>2.6.0</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-core</artifactId> 
      <version>${spring.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-context</artifactId> 
      <version>${spring.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-jdbc</artifactId> 
      <version>${spring.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-orm</artifactId> 
      <version>${spring.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-context-support</artifactId> 
      <version>${spring.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-webmvc</artifactId> 
      <version>${spring.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-web</artifactId> 
      <version>${spring.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.security</groupId> 
      <artifactId>spring-security-web</artifactId> 
      <version>${spring.version}</version> 
     </dependency> 

     <dependency> 
      <groupId>org.springframework.security</groupId> 
      <artifactId>spring-security-config</artifactId> 
      <version>${spring.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.security</groupId> 
      <artifactId>spring-security-taglibs</artifactId> 
      <version>${spring.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.security</groupId> 
      <artifactId>spring-security-core</artifactId> 
      <version>${spring.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>commons-dbcp</groupId> 
      <artifactId>commons-dbcp</artifactId> 
      <version>1.4</version> 
     </dependency> 
     <dependency> 
      <groupId>jstl</groupId> 
      <artifactId>jstl</artifactId> 
      <version>1.2</version> 
     </dependency> 
     <dependency> 
      <groupId>org.seleniumhq.selenium</groupId> 
      <artifactId>selenium-server</artifactId> 
      <version>2.25.0</version> 
     </dependency> 
     <dependency> 
      <groupId>org.testng</groupId> 
      <artifactId>testng</artifactId> 
      <version>5.10</version> 
      <classifier>jdk15</classifier> 
      <scope>test</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.seleniumhq.selenium.client-drivers</groupId> 
      <artifactId>selenium-java-client-driver</artifactId> 
      <version>1.0.1</version> 
      <scope>test</scope> 
     </dependency> 
    </dependencies> 
    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-war-plugin</artifactId> 
       <version>2.4</version> 
      </plugin> 
      <plugin> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>3.0</version> 
       <configuration> 
        <source>1.7</source> 
        <target>1.7</target> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-surefire-plugin</artifactId> 
       <configuration> 
        <skip>true</skip> 
        <systemProperties> 
         <property> 
          <name>user</name> 
          <value>tobrien</value> 
         </property> 
         <property> 
          <name>name</name> 
          <value>Tim O'Brien</value> 
         </property> 
        </systemProperties> 
       </configuration> 
       <executions> 
        <execution> 
         <phase>integration-test</phase> 
         <goals> 
          <goal>test</goal> 
         </goals> 
         <configuration> 
          <skip>false</skip> 
         </configuration> 
        </execution> 
       </executions> 
      </plugin> 
      <plugin> 
       <groupId>org.codehaus.mojo</groupId> 
       <artifactId>selenium-maven-plugin</artifactId> 
       <executions> 
        <execution> 
         <phase>pre-integration-test</phase> 
         <goals> 
          <goal>start-server</goal> 
         </goals> 
         <configuration> 
          <background>true</background> 
         </configuration> 
        </execution> 
       </executions> 
      </plugin> 
     </plugins> 
    </build> 
</project> 

測試代碼:

public class FunctionalTest { 
private Selenium selenium; 

     @BeforeClass 
     public void startSelenium() { 
     this.selenium = new DefaultSelenium("localhost", 8080, "*chrome", 
      "http://www.twitter.com"); 
     this.selenium.start(); 
     } 
@Test 
     @Parameters({ "user", "name" }) 
     public void testTwitter(String user, String name) { 
     try { 
      selenium.open("http://www.twitter.com/" + user); 
      selenium.waitForPageToLoad("3000"); 
      assert selenium.isTextPresent(name); 

     } catch (SeleniumException e) { 
      throw e; 
     } 
     } 
@AfterClass(alwaysRun = true) 
     public void stopSelenium() { 
     this.selenium.stop(); 
     } 
} 
+0

你可以添加一些測試代碼嗎? – xyz

+0

爲什麼你有'skip'設置? –

+0

我只是遵循http://books.sonatype.com/mcookbook/reference/ch08s02.html網站中給出的pom.xml文件 – kumuda

回答

0

你必須在Maven的萬無一失插件的配置增加了跳躍測試標籤

  <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-surefire-plugin</artifactId> 
      <configuration> 
       <skip>true</skip> 
      ... 
     </plugin> 

此配置將跳過maven運行下面所有階段的測試「預集成測試階段」。嘗試並將此值設置爲false或完全刪除它。

0

默認情況下,maven-surefire-plugin僅在測試階段運行。您需要通過添加額外的執行來啓用它,該執行在集成測試階段運行。這裏是一個最好的做法,在正常的測試被命名爲* Test.java測試和集成測試被命名爲* IT.java:

<!-- Integration tests --> 
<!-- Exclude *IT.java at test phase --> 
<!-- Include *IT.java at integration-test phase, but exclude normal tests --> 
<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-surefire-plugin</artifactId> 
    <version>2.7.1</version> 
    <configuration> 
    <excludes> 
     <exclude>**/*IT.java</exclude> 
    </excludes> 
    </configuration> 
    <executions> 
    <execution> 
     <id>integration-test</id> 
     <goals> 
     <goal>test</goal> 
     </goals> 
     <phase>integration-test</phase> 
     <configuration> 
     <excludes> 
      <exclude>**/*Test.java</exclude> 
     </excludes> 
     <includes> 
      <include>**/*IT.java</include> 
     </includes> 
     </configuration> 
    </execution> 
    </executions> 
</plugin> 

添加到Maven的萬無一失,插件的配置排除測試,其一端與「* IT.java」,以避免在測試階段執行集成測試。然後爲正在集成測試階段運行的集成測試添加執行。要覆蓋用於測試階段執行的共享插件配置,請在集成測試執行中指定一個配置。此配置包括集成測試(「* IT.java」)並排除正常測試(「* Test.java」),因爲在集成測試階段只能運行集成測試。正常測試在測試階段仍將運行一次,但不會再進入集成測試階段。