Executing the Testcase using TestNG.xml file


Lets execute your testcases using testng.xml. This is really helpful when you would like to execute only desired testcases,browser compatibility / parallel testing.

Create a java class file name Testng.java


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

public class Testng {
// this is test case 1
@Test
public void testCase1() {
System.out.println(“in test case 1”);
}

// this is test case 2
@Test
public void testCase2() {
System.out.println(“in test case 2”);
}

@BeforeMethod
public void beforeMethod() {
System.out.println(“in beforeMethod”);
}

@AfterMethod
public void afterMethod() {
System.out.println(“in afterMethod”);
}

@BeforeClass
public void beforeClass() {
System.out.println(“in beforeClass”);
}

@AfterClass
public void afterClass() {
System.out.println(“in afterClass”);
}

@BeforeTest
public void beforeTest() {
System.out.println(“in beforeTest”);
}

@AfterTest
public void afterTest() {
System.out.println(“in afterTest”);
}

@BeforeSuite
public void beforeSuite() {
System.out.println(“in beforeSuite”);
}

@AfterSuite
public void afterSuite() {
System.out.println(“in afterSuite”);
}

}

Next, let’s create the file testng.xml. Right click on the project, New–> File, Create ‘testng.xml’.Click on finish.

You will see testng.xml under your project.
Now paste this code in that file

<suite name="Suite1">
  <test name="test1">
    <classes>
       <class name="Testng"/>
    </classes>
  </test>
</suite>

Now Run the xml file.You will see the report generated in the test-output folder.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s