Friday, 4 April 2014

Making Unit and Integration Tests coexist - Maven/TestNG

Recently I had to make unit and integration tests coexist in the same maven project (pom), up-till then integration tests resided in a separate project (nice and easy).  However ideally these should co-exists in same project to make versioning/branching possible.

This small post I will share the steps taken to make this possible.

Technologies used here are:

- Maven
- TestNG

Goals: 

- mvn test should only run unit tests
- mvn integration-test should only run integration tests so that they do not cause issues building the project and are not included in the maven lifecycle for mvn install

Steps:


  • Make 2 different testng.xml files called:
    • ITtestng.xml (for integration tests)
      <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
      <suite name="TPCC REST api Integration Tests" verbose="1" >
      <test name="Rest API - Json schema validation Tests" >
      <classes>
      <class name="com.clearqa.test.RestApiITCase" />
      </classes>
      </test>
      </suite>
      view raw ITtestng.xml hosted with ❤ by GitHub
    • UTtestng.xml (for unit tests)
      <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
      <suite name="TPCC Unit Tests" verbose="1" >
      <test name="Dummy Tests" >
      <classes>
      <class name="com.clearqa.test.DummyTest" />
      </classes>
      </test>
      </suite>
      view raw UTtestng.xml hosted with ❤ by GitHub
  • Include maven-failsafe-plugin in your pom.xml to run just the integration test cases. Please not maven-failsafe-plugin is designed to run integration tests while the Surefire plugins is desinged to run unit test more info on this can be found here.
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.17</version>
    <configuration>
    <suiteXmlFiles>
    <suiteXmlFile>target/test-classes/ITtestng.xml</suiteXmlFile>
    </suiteXmlFiles>
    </configuration>
    <executions>
    <execution>
    <id>failsafe-integration-tests</id>
    <phase>integration-test</phase>
    <goals>
    <goal>integration-test</goal>
    </goals>
    </execution>
    </executions>
    </plugin>
  • That is it! 
    • Now your mvn test command will run only the unit tests. Also mvn install will not run integration tests and cause the build to fail
    • mvn integration-test will run both unit and integration tests.


No comments:

Post a Comment