Version 1 by Erik Drolshammer
on Nov 10, 2008 14:11.

compared with
Key
This line was removed.
This word was removed. This word was added.
This line was added.

Changes (30)

View Page History
The golden rule is that _mvn clean install_ should *ALWAYS* return Build Successful. This means that the test suite which is run by default cannot depend on any external resources like database, JMS, Files, WebServices, etc. This does not mean that you cannot have tests that use _in-memory_, embedded alternatives! For now let's assume that we have two types of tests: simple unit tests that only test business and doesn't need any external environment and tests that need external services. Lets look at a concrete example to make things more concrete.
{info}
The golden rule is that _mvn clean install_ should *ALWAYS* return _Build Successful_.
This means that the test suite which is run by default cannot depend on any external resources like database, JMS, Files, WebServices, etc.
{info}

h4. Example
This does not mean that you cannot have tests that use _in-memory_, embedded alternatives! For now let's assume that we have two types of tests: simple unit tests that only test business and doesn't need any external environment and tests that need external services. Lets look at a concrete example to make things more concrete.

|| Service || Group Name Suggestion ||
| Oracle database | db-oracle|
| BEA JMS Broker | jms-bea|
| Webservice1 | webservice1 |
| Webservice2 | webservice2 |
h2. How to set this up


# Annotate each test that depend on any of these services with the appropriate groups. (Multiple groups for a test is OK.)
# Create two testng configuration files.
** testng.xml - run all fast tests that doesn't require any environment.
*** {code}
<groups>
<run>
<exclude name="disabled"/>
<exclude name="jms-bea"/>
<exclude name="db-oracle"/>
<exclude name="webservice1"/>
<exclude name="webservice2"/>
<exclude name="slow"/>
</run>
</groups>
{code}
** testng-test-all-environments.xml - run slow tests and tests that depend environment
*** {code}
<groups>
<run>
<exclude name="disabled"/>
<include name="jms-bea"/>
<include name="db-oracle"/>
<include name="webservice1"/>
<include name="webservice2"/>
</run>
</groups>
{code}
# Update pom.xml with the following surefire configuration
** use testng.xml by default.
# Annotate the tests in question
{code}
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<environmentVariables>
<path>${basedir}/target;${java.library.path}</path>
</environmentVariables>
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
@Test(groups = "external-api")
public void testFetchDataFromExternalAPI() {}
{code}
** Add a profile to pom.xml to run the tests that require environment. These can then be activated by running _mvn test -P test-all-environments_.
# Configure surefire (in pom.xml) to exclude these groups of tests
{code}
<properties>
<!--Override this property in a maven profile if you want to run all tests. -->
<excluded.test.groups>external-api</excluded.test.groups>
</properties>

<!-- To run all tests: mvn clean install -Pall-tests -->
<profiles>
<profile>
<profile> <id>all-tests</id>
<properties>
<excluded.test.groups/>
</properties>
</profile>
</profiles>

<build>
<plugins>
<plugin>
<id>test-all-environments</id> <groupId>org.apache.maven.plugins</groupId>
<build> <artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<plugins> <excludedGroups>${excluded.test.groups}</excludedGroups>
<plugin> </configuration>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<environmentVariables>
<path>${basedir}/target;${java.library.path}</path>
</environmentVariables>
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/testng-all-environments.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
...
<dependencies>
<dependency>
</plugins> <groupId>org.testng</groupId>
</build> <artifactId>testng</artifactId>
</profile> <version>6.14.2</version>
<scope>test</scope>
</profiles> </dependency>
</dependencies>
{code}