Весенние тесты интеграции Sevenium в Maven - запуск Tomcat перед Selenium

Я учусь запускать тесты интеграции Selenium в Maven. Я настроил Selenium и плагин Tomcat Maven с помощью Cargo. Это мой файл 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>com.mycompany</groupId>
    <artifactId>SeleniumTest</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>SeleniumTest</name>

    <properties>
        <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.5</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>tomcat-maven-plugin</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc-struts</artifactId>
            <version>2.5.6</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.14</version>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.8</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.codehaus.cargo</groupId>
            <artifactId>cargo-maven2-plugin</artifactId>
            <version>1.3.3</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>selenium-maven-plugin</artifactId>
            <version>2.3</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-firefox-driver</artifactId>
            <version>2.31.0</version>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>6.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>SeleniumTest</artifactId>
            <version>${project.version}</version>
            <type>war</type>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <testSourceDirectory>src/main/java/test/integrationTests</testSourceDirectory>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <compilerArguments>
                        <endorseddirs>${endorsed.dir}</endorseddirs>
                    </compilerArguments>
                    <suiteXmlFiles>
                        <suiteXmlFile>src/main/java/test/integrationTests/testng.xml</suiteXmlFile>
                    </suiteXmlFiles>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.1.1</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.1</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${endorsed.dir}</outputDirectory>
                            <silent>true</silent>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>javax</groupId>
                                    <artifactId>javaee-endorsed-api</artifactId>
                                    <version>6.0</version>
                                    <type>jar</type>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.14</version>
                <executions>
                    <execution>
                        <id>default-test</id>                                
                        <configuration>
                            <skipTests>true</skipTests>
                        </configuration>
                    </execution>

                    <execution>
                        <id>surefire-it</id>
                        <phase>integration-test</phase>
                        <goals>
                            <goal>test</goal>
                        </goals>
                        <configuration>
                            <skipTests>false</skipTests>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.cargo</groupId>
                <artifactId>cargo-maven2-plugin</artifactId>
                <version>1.3.3</version>
                <configuration>
                    <wait>true</wait> 
                    <container>
                        <containerId>tomcat7x</containerId>
                        <type>installed</type>
                        <home>D:\Program Files\Apache Software Foundation\Apache Tomcat 7.0.27</home>
                    </container>
                    <configuration>
                        <type>existing</type>
                        <home>D:\Program Files\Apache Software Foundation\Apache Tomcat 7.0.27</home>
                    </configuration>

                    <deployables>
                        <deployable>
                            <pingURL>http://localhost:8080/SeleniumTest</pingURL>
                            <pingTimeout>300000</pingTimeout>
                        </deployable>
                    </deployables>
                    <executions>
                        <execution>
                            <id>start-container</id> 
                            <phase>pre-integration-test</phase>
                            <goals>
                                <goal>start</goal>
                            </goals>
                        </execution>
                        <execution>
                            <id>stop-container</id>
                            <phase>post-integration-test</phase>
                            <goals>
                                <goal>stop</goal>
                            </goals>
                        </execution>
                    </executions>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

Это журналы Netbeans, когда я говорю Build:

------------------------------------------------------------------------
Building SeleniumTest 1.0-SNAPSHOT
------------------------------------------------------------------------

[dependency:copy]

[resources:resources]
[debug] execute contextualize
Using 'UTF-8' encoding to copy filtered resources.
Copying 0 resource

[compiler:compile]
Nothing to compile - all classes are up to date

[resources:testResources]
[debug] execute contextualize
Using 'UTF-8' encoding to copy filtered resources.
skip non existing resourceDirectory C:\Users\Colenso\Documents\NetBeansProjects\SeleniumTest\src\test\resources

[compiler:testCompile]
Compiling 1 source file to C:\Users\Colenso\Documents\NetBeansProjects\SeleniumTest\target\test-classes

[compiler:testCompile]
Compiling 1 source file to C:\Users\Colenso\Documents\NetBeansProjects\SeleniumTest\target\test-classes

[surefire:test]
Tests are skipped.

[war:war]
Packaging webapp
Assembling webapp [SeleniumTest] in [C:\Users\Colenso\Documents\NetBeansProjects\SeleniumTest\target\SeleniumTest-1.0-SNAPSHOT]
Processing war project
Copying webapp resources [C:\Users\Colenso\Documents\NetBeansProjects\SeleniumTest\src\main\webapp]
Webapp assembled in [454 msecs]
Building war: C:\Users\Colenso\Documents\NetBeansProjects\SeleniumTest\target\SeleniumTest-1.0-SNAPSHOT.war

[surefire:test]
Surefire report directory: C:\Users\Colenso\Documents\NetBeansProjects\SeleniumTest\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running test.integrationTests.SimpleTest
Configuring TestNG with: TestNG652Configurator

Как видите, он только собирает файл.war и не запускает мой контейнер Tomcat и не развертывает на нем файл.war. Если я скажу " Запустить в NetBeans", я увижу, что Tomcat запускается и мое приложение работает. Однако интеграционные тесты не проводятся.

1 ответ

Решение

Для запуска интеграционных тестов вы должны указать maven запустить integration-test фаза. Либо ты бежишь mvn integration-test или же mvc install (install работает, потому что он пришел после integration-test в фазах мавенов)

@ См. Maven Build Lifecycle

Кстати: вы должны взглянуть на maven-failsave-плагин. Это тестовый плагин, подобный верному, но предназначенный для запуска интеграционных тестов. - Таким образом, у вас может быть два плагина (верный для модульных тестов и отказоустойчивый для интеграционных тестов), так что вам не нужно так много переконфигурировать верный.

Другие вопросы по тегам