TestNG Showcasing – Suites

Although supported in JUnit 4, in the earlier JUnit 3 it was only TestNG that made the Suites hierarchy possible.

To gather the specifics of Suites we would need a config xml file that for our case would look like:

<!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd" >
<suite name="Suites test">
  <test name="testingSuites">
    <classes>
       <class name="com.dimitrisli.testng.suite.TestNGSuiteClass2" />
       <class name="com.dimitrisli.testng.suite.TestNGSuiteClass1" />
    </classes>
  </test>
</suite>

Since we are using Maven as our build management tool, it’s good to know that the Surefire plugin is TestNG-enabled needing the following structure to specify the TestNG related config file:

<plugin>
  			<groupId>org.apache.maven.plugins</groupId>
  			<artifactId>maven-surefire-plugin</artifactId>
  			<version>2.11</version>
  			<configuration>
  				<suiteXmlFiles>
  					<suiteXmlFile>testng.xml</suiteXmlFile>
  				</suiteXmlFiles>
  				</configuration>
  		</plugin>

For our example we have two test Classes:

package com.dimitrisli.testng.suite;

import org.testng.Assert;
import org.testng.annotations.Test;

public class TestNGSuiteClass1 {

	@Test
	public void method1(){
		System.out.println("From TestNGSuiteClass1 test case");
		Assert.assertTrue(true);
	}
}

and

package com.dimitrisli.testng.suite;

import org.testng.Assert;
import org.testng.annotations.Test;

public class TestNGSuiteClass2 {

	@Test
	public void method1(){
		System.out.println("From TestNGSuiteClass2 test case");
		Assert.assertTrue(true);
	}
}

After executing mvn package we notice the following in the output:

Running TestSuite
From TestNGSuiteClass2 test case
From TestNGSuiteClass1 test case

We have just intervened in the Maven build lifecycle enhancing the test phase to consider our TestNG suite and to run the unit tests accordingly.

You can find this code in this Github repository

Advertisements

TestNG Showcase – Exceptions Testing

Similar to JUnit 4 testing, TestNG is providing functionality to check exceptions are thrown when we are expecting them to be thrown.

A simple example:


package com.dimitrisli.testng.exceptionTesting;

import org.testng.annotations.Test;


public class ExceptionsTesting {

	@Test(expectedExceptions = NullPointerException.class)
	public void method(){
		throw new NullPointerException();
	}

	@Test(expectedExceptions = IllegalArgumentException.class)
	public void method2(){
		throw new IllegalArgumentException();
	}
}

The overall code concerning the TestNG showcasing can be found in this Github repository.

TestNG Showcase – Performance testing

Similar to JUnit 4, TestNG is providing means for performance measurements.

An example:

package com.dimitrisli.testng.performance;

import org.testng.Assert;
import org.testng.annotations.Test;



public class PerformanceTesting {

	@Test(timeOut=100)
	public void method(){
		Assert.assertTrue(true);
	}
}

You can find the overall code of the TestNG showcasing in this Github repository.

TestNG Showcase – Before and After

Similar to JUnit 4, TestNG is providing similar Before and After functionalities with the addition of @BeforeSuite and @AfterSuite. Also instead of @Before and @After as it is named in JUnit (before and after each unit test) it is now named @BeforeMethod and @AfterMethod providing though identical functionality.

An example:

package com.dimitrisli.testng.beforeAfter;

import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;


public class TestNGBeforeAfterTesting {

	@BeforeSuite
	public static void initialiseSuite(){
		System.out.println("init suite");
	}

	@BeforeClass
	public static void initialiseClass(){
		System.out.println("init class");
	}
	
	@BeforeMethod
	public void initialiseTest(){
		System.out.println("init test");
	}
	
	@Test
	public void test1(){
		System.out.println("inside test1");
		Assert.assertTrue(true);
	}

	@Test
	public void test2(){
		System.out.println("inside test2");
		Assert.assertTrue(true);
	}
	
	@AfterMethod
	public void teardownTest(){
		System.out.println("teardown test");
	}
	
	@AfterClass
	public static void teardownClass(){
		System.out.println("teardown class");
	}
	
	@AfterSuite
	public static void teardownSuite(){
		System.out.println("teardown suite");
	}
}

that is yielding the following output:

init suite
init class
init test
inside test1
teardown test
init test
inside test2
teardown test
teardown class
PASSED: test1
PASSED: test2

===============================================
    Default test
    Tests run: 2, Failures: 0, Skips: 0
===============================================

teardown suite

===============================================
Default suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================

You can find the code in this Github repository

Setup SBT in Mac OS X

Simple Build Tool (SBT) is a building tool for Scala with a Maven feel into it.

First download the latest sbt-launch.jar, put the jar in the /usr/local/bin dir:

$sudo curl http://typesafe.artifactoryonline.com/typesafe/ivy-releases/org.scala-tools.sbt/sbt-launch/CURRENT_VERSION/sbt-launch.jar --O sbt-launch-CURRENT_VERSION.jar
$sudo cp sbt-launch-CURRENT_VERSION.jar /usr/local/bin

Final step is to create file with the command to load up the jar:

$sudo mkdir /usr/local/bin/sbt
$sudo chmod +x /usr/local/bin/sbt 
$sudo vim /usr/local/bin/sbt

Including the following command:

java -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=512m -Xmx512M -Xss2M -jar `dirname $0`/sbt-launch-CURRENT_VERSION.jar "$@"