Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to seperate unit and integration tests in testng + maven using annotations?

The maven fail-safe plugin needs to be able to tell the difference between unit tests and integration tests. It seems that when using JUnit one way to separate tests is to use JUnit @Categories annotation. This blog post show how to do this using junit http://www.agile-engineering.net/2012/04/unit-and-integration-tests-with-maven.html

@Category(IntegrationTest.class)
public class ExampleIntegrationTest{

 @Test
 public void longRunningServiceTest() throws Exception {

 }
}

How can I accomplish the same same thing with TestNG and Maven failsafe plugin. I want to use annotations on the test classes to mark them as integration tests.

like image 819
ams Avatar asked Oct 15 '13 09:10

ams


People also ask

What annotation is used on an integration test method to run it in a transaction?

@Transaction annotation for integration testing in Spring Boot.

How do I run a specific integration test in Maven?

The simplest way to run integration tests is to use the Maven failsafe plugin. By default, the Maven surefire plugin executes unit tests during the test phase, while the failsafe plugin runs integration tests in the integration-test phase.

How does maven integrate and run JUnit test?

We can run our unit tests with Maven by using the command: mvn clean test. When we run this command at command prompt, we should see that the Maven Surefire Plugin runs our unit tests. We can now create a Maven project that compiles and runs unit tests which use JUnit 5.


3 Answers

This can be added to the test.

@IfProfileValue(name="test-profile", value="IntegrationTest")
public class PendingChangesITCase extends AbstractControllerIntegrationTest {
    ...
}

To select the tests to be executed just add the value to the profile for executing the integration tests.

<properties>
    <test-profile>IntegrationTest</test-profile>
</properties>

If the maven profile selected does not have the property value, it will not execute the integration tests.

like image 119
jordiburgos Avatar answered Nov 01 '22 15:11

jordiburgos


We use maven-surefire-plugin for unit tests and maven-failsafe-plugin for integration tests. They both integrate nicely with Sonar.

like image 33
Shinta Smith Avatar answered Nov 01 '22 15:11

Shinta Smith


It looks like i've arrived late to this party but for future googlers, I got it to work by doing the following:

Annotate the relevant test classes with a group name of your choice:

@Test(groups='my-integration-tests')
public class ExampleIntegrationTest {
  @Test
  public void someTest() throws Exception {

 }
}

Tell the surefire plugin (which runs the normal unit test phase) to ignore your integration tests:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <configuration>
    <excludedGroups>my-integration-tests</excludedGroups>
  </configuration>
</plugin>

And tell the failsafe plugin (which runs the integration test) to only care about your groups.

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-failsafe-plugin</artifactId>
  <version>2.20</version>
  <executions>
    <execution>
      <goals>
        <goal>integration-test</goal>
        <goal>verify</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <includes>**/*.java</includes>
    <groups>my-integration-tests</groups>
  </configuration>
</plugin>
like image 20
andrew-g-za Avatar answered Nov 01 '22 17:11

andrew-g-za