web應(yīng)用集成測(cè)試的時(shí)候,各位還需要啟動(dòng)web容器,然后打開瀏覽器,輸入ulr,然后看到瀏覽器的輸出嗎?
下面我們用maven做到自動(dòng)化!
我們利用maven的生命周期和jetty插件來實(shí)現(xiàn)。
下面描述下做的自動(dòng)化web集成測(cè)試實(shí)現(xiàn)的原理。
1,在生命周期pre-integration-test啟動(dòng)jetty容器
2,在生命周期integration-test中測(cè)試我們寫的***IT.java類
3,在post-integration-test shutdow jetty容器。
在pom.xml中加入代碼如下:
<profiles>
<profile>
<id>ittest</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<id>run-integration-test</id>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<includes>
<include>**/*IT.java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.26</version>
<configuration>
<contextPath>/</contextPath>
<stopPort>9966</stopPort>
<stopKey>stop-jetty-for-it</stopKey>
<connectors>
<connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
<port>6211</port>
</connector>
</connectors>
</configuration>
<executions>
<execution>
<id>start-it-jetty</id>
<phase>pre-integration-test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<daemon>true</daemon>
</configuration>
</execution>
<execution>
<id>stop-it-jetty</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
然后就可以編寫測(cè)試用例了
步驟如下:
1,定義一個(gè)以此命名的****IT的測(cè)試類(integration test縮寫), 在里面華麗的寫好你的測(cè)試邏輯。
再此不舉例了,主要一個(gè)思路可以用httpclint來實(shí)現(xiàn)里面的測(cè)試代碼。
2,然后 執(zhí)行 mvn clean post-integration-test -Pittest
好了 就可以看到我們測(cè)試用例是否通過。
建議:以上的代碼可以加入到父類的pom中,以后繼承此父pom后,只需要按以上2步,就可以做到web應(yīng)用測(cè)試自動(dòng)化了。