天天看點

【轉】maven 自動化web應用內建測試

web應用內建測試的時候,各位還需要啟動web容器,然後打開浏覽器,輸入ulr,然後看到浏覽器的輸出嗎?

下面我們用maven做到自動化!

我們利用maven的生命周期和jetty插件來實作。

下面描述下做的自動化web內建測試實作的原理。

1,在生命周期pre-integration-test啟動jetty容器

3,在post-integration-test shutdow jetty容器。

在pom.xml中加入代碼如下:

【轉】maven 自動化web應用內建測試

<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>   

                        <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>   

                                <id>start-it-jetty</id>   

                                <phase>pre-integration-test</phase>   

                                    <goal>run</goal>   

                                    <daemon>true</daemon>   

                                <id>stop-it-jetty</id>   

                                <phase>post-integration-test</phase>   

                                    <goal>stop</goal>   

                </plugins>   

            </build>   

        </profile>   

    </profiles>  

 然後就可以編寫測試用例了

 步驟如下:

1,定義一個以此命名的****it的測試類(integration test縮寫), 在裡面華麗的寫好你的測試邏輯。

再此不舉例了,主要一個思路可以用httpclint來實作裡面的測試代碼。 

2,然後 執行 mvn clean post-integration-test -pittest

好了 就可以看到我們測試用例是否通過。

建議:以上的代碼可以加入到父類的pom中,以後繼承此父pom後,隻需要按以上2步,就可以做到web應用測試自動化了。