TestNG Tutorial 1 – Basic usage
This tutorial introduces the basic annotation supported in TestNG.
import java.util.*; import org.testng.Assert; import org.testng.annotations.*; public class TestNGTest1 { private Collection collection; @BeforeClass public void oneTimeSetUp() { // one-time initialization code System.out.println("@BeforeClass - oneTimeSetUp"); } @AfterClass public void oneTimeTearDown() { // one-time cleanup code System.out.println("@AfterClass - oneTimeTearDown"); } @BeforeMethod public void setUp() { collection = new ArrayList(); System.out.println("@BeforeMethod - setUp"); } @AfterMethod public void tearDown() { collection.clear(); System.out.println("@AfterMethod - tearDown"); } @Test public void testEmptyCollection() { Assert.assertEquals(collection.isEmpty(),true); System.out.println("@Test - testEmptyCollection"); } @Test public void testOneItemCollection() { collection.add("itemA"); Assert.assertEquals(collection.size(),1); System.out.println("@Test - testOneItemCollection"); } }
Result
@BeforeClass - oneTimeSetUp @BeforeMethod - setUp @Test - testEmptyCollection @AfterMethod - tearDown @BeforeMethod - setUp @Test - testOneItemCollection @AfterMethod - tearDown @AfterClass - oneTimeTearDown PASSED: testEmptyCollection PASSED: testOneItemCollection

Hi,
I have 3 test
@BeforeTest
@AfterTest
@Test – to Login
@Test – to create Employee
@Test – to logout.
When i execute this second test(Create Employee) is getting executed first and then login and test to create employee and logout is throwing an error.
please let me know what has to be done..
As far as I know JUnit, tests are executed in random order except for the setup and teardown methods. Therefore put login into setup (@BeforeTest) and logout into teardown (@AfterTest)!
Hello mkyong,
I have one query.
I have TestNG Framework integrated into the eclipse.
I wanted to execute a java package(contains multiple java files) and obtain the induvidual results in the html report (Default_test.html)
My question is that i have the testng.xml file configured with the name of the package to be executed, but i dont have the idea of how to write a corresponding java file that calls this package, excecutes it and generate the results(In default_test.html) of all the java files present in that package.
Below is the sample code snippet of testng.xml file to configure a package.
Could you please help me out in this?
Thanks in advance.
Thanks & Regards,
Majid M A
Majid,
you can create dependency between tests:
http://www.mkyong.com/unittest/testng-tutorial-7-dependency-test/
How to start your class ?
Advice to use the Eclipse IDE + TestNG Eclipse plugin for testing.