Better testing with Testng

Dec 23, 2011 · 3 minute read · Comments
tech


Most of the programmers busy with writing code and forget the importance of having tested the implementations then and there.When you start writing tests you should select the best suitable framework for your job or else you will make things worse. There are few different tests such as
Out of these tests JUnit is adequate to perform Unit tests but for integration tests it has some limitations. If you have some complex dependencies between modules or states such as a sever needed to be started with deployed artiface before all the tests execute or establish a database connection with some preconditions you will find hard time if you choose JUnit .One key important thing you need to keep in mind is “JUnit instantiate class before it runs each and every test method”.
There are lots of comparisons between JUnit and Testng [1][2].When you go through them you will realize how Testng becomes your trusted companion for tests such as integration tests.Testng provides you annotations to control things better and a very cleaner way.Not only that you have custom testng.xml file which enables to customize things to accomplish lots of things as desirable.
If you wish to migrate to Testng there are lots of resources available to learn about Testng.But i advice do not try to convert JUnit tests directly but rather modify them to inherit elegance of Testng :) .
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.1.1</version>
<scope>test</scope>
</dependency>

@BeforeSuite: The annotated method will be run before all tests in this suite have run.
@AfterSuite: The annotated method will be run after all tests in this suite have run.
@BeforeTest: The annotated method will be run before any test method belonging to the classes inside the <test> tag is run.
@AfterTest: The annotated method will be run after all the test methods belonging to the classes inside the <test> tag have run.
@BeforeGroups: The list of groups that this configuration method will run before. This method is guaranteed to run shortly before the first test method that belongs to any of these groups is invoked.
@AfterGroups: The list of groups that this configuration method will run after. This method is guaranteed to run shortly after the last test method that belongs to any of these groups is invoked.
@BeforeClass: The annotated method will be run before the first test method in the current class is invoked.
@AfterClass: The annotated method will be run after all the test methods in the current class have been run.
@BeforeMethod: The annotated method will be run before each test method.
@AfterMethod: The annotated method will be run after each test method.

@Test(expectedExceptions = ExpectedException.class)
public void expectedExceptionTester() {
throwAnException();
}
The above method should throw the expected exception otherwise it should fail.
[1] http://www.mkyong.com/unittest/junit-4-vs-testng-comparison/
[2] http://www.ibm.com/developerworks/java/library/j-cq08296/
[3] http://testng.org/doc/index.html

comments powered by Disqus