分离和只运行单元测试的原因是:
- 需要在 selenium 测试之前运行单元测试 - 以简化故障推理。
- 这些类型的测试需要在 Maven 插件配置中进行不同的设置——并行测试运行、排序、系统属性、JVM 选项等。
- Selenium 测试是端到端的,需要大量的配置和部署工作、重置应用程序状态(清除数据库、启动和停止应用服务器)——这些工作在所有开发和暂存环境中都不容易完成
Maven 有一组用于分离集成测试步骤的阶段,还有一些配置文件允许在一个地方收集 selenium 测试配置。
这是我们使用 selenium 进行端到端测试的示例(解压缩并运行脚本以从头开始设置数据库、启动应用服务器、运行测试、停止应用服务器、验证和报告测试结果):
<profile>
<id>selenium</id>
<dependencies>
<dependency>
<groupId>com.thenewmotion</groupId>
<artifactId>msp-solveconnector</artifactId>
<version>${project.version}</version>
<classifier>sql-install</classifier>
<type>zip</type>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack-dependencies</id>
<phase>package</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<includeGroupIds>com.thenewmotion</includeGroupIds>
<includeArtifactIds>msp-solveconnector</includeArtifactIds>
<includeClassifiers>sql-install</includeClassifiers>
<includeTypes>zip</includeTypes>
<includes>**/*.*</includes>
<outputDirectory>${project.build.directory}</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sql-maven-plugin</artifactId>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.16</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>create-db</id>
<phase>pre-integration-test</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<driver>com.mysql.jdbc.Driver</driver>
<url>jdbc:mysql://localhost</url>
<username>${msp.db.user}</username>
<password>${msp.db.password}</password>
<orderFile>ascending</orderFile>
<fileset>
<basedir>${project.build.directory}/db</basedir>
<includes>
<include>1_drop_database.sql</include>
<include>2_create_database.sql</include>
<include>3_create_tables.sql</include>
<include>4_create_views.sql</include>
<include>5_create_foreign_keys.sql</include>
<include>6_data.sql</include>
</includes>
</fileset>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<configuration>
<stopKey>foo</stopKey>
<stopPort>8088</stopPort>
<connectors>
<connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
<port>${msp.port}</port>
<maxIdleTime>60000</maxIdleTime>
</connector>
</connectors>
</configuration>
<executions>
<execution>
<id>start-jetty</id>
<phase>pre-integration-test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<daemon>true</daemon>
</configuration>
</execution>
<execution>
<id>stop-jetty</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.12</version>
<configuration>
<systemPropertyVariables>
<msp.user>${msp.user}</msp.user>
<msp.password>${msp.password}</msp.password>
<msp.baseUrl>${msp.baseUrl}</msp.baseUrl>
<webdriver.type>${webdriver.type}</webdriver.type>
<webdriver.chrome.driver>${webdriver.chrome.driver}</webdriver.chrome.driver>
</systemPropertyVariables>
<includes>
<include>**/*FT.class</include>
</includes>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sql-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
</plugin>
</plugins>
</build>
</profile>