Selenium Webdriver Appium Complete TutorialSelenium Webdriver Appium Complete Tutorial
Automation Testing
  • Tools
    • Selenium
      • Selenium Java Tutorial
      • Selenium C# Tutorial
    • Appium
      • Appium Java Tutorial
      • Appium C#Tutorial
    • Katalon
  • Trainings
  • TestNG
  • Reports
    • Extent Reports
      • Extent Reports – Java
      • Extent Reports – Java -Version3
      • Extent Reports – C#
    • Vigo Reports
    • AT Excel Report
  • Excel
    • Apache POI – Java
    • Excel With C#
  • Interview Questions
    • Selenium Interview Questions
    • Java Interview Questions
    • C# Interview Questions
  • Demo Site
  • Practice Site
  • More…
    • AutoIt
    • Sikuli
    • Robot Class
    • File Upload
    • ScreenShot
      • AShot
      • ShutterBug
  • About
December 14, 2016

Adding Methods to testng.xml file





Adding methods to testng.xml file, will explain you about creating and configuring TestNG test suite with methods which are developed/created in the java project. A Class may contain one or more methods in it. With this testng.xml configuration file we can include all the methods belongs to a particular class to the test suite.

Below are The Steps for Adding Methods to testng.xml file:

1.Create a java project with multiple methods in each class.

1 1. Project with Multiple Methods

In the above project, created a class called “FirstClassInFirstPackage” with three methods called “firstTestCase”, “secondTestCase” and “thirdTestCase”.

2.Below is the sample code for the above test class and methods.

package com.test.firstpackage;

import org.testng.annotations.Test;

public class FirstClassInFirstPackage
{
    
  @Test
  public void firstTestCase()
  {
      System.out.println("First TestNG test in First Package");
  }
 
  @Test
  public void secondTestCase()
  {
      System.out.println("Second TestNG test in First Package");
  }
 
  @Test
  public void thirdTestCase()
  {
      System.out.println("Third TestNG test in First Package");
  }
}

3.Now create a testng.xml file (here the xml file name is “testng-methods.xml”) with below code/configuration content.  To work with methods in the testng.xml file, we have to use include or exclude tags in the class tag.

Include meas which methods to execute and exclude means which methods NOT to execute from a particular class.

If we want to execute all the methods from a particular class then we can directly use the entire class to execute from the configuration file. Which we have seen the Adding Classes to testng.xml file.

But particularly, when we want to include or exclude some of the test methods then we can go for methods tag and should use the include or exclude tags to execute particular test methods.


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test name="ClassTest">
    <classes>
        <class name="com.test.firstpackage.FirstClassInFirstPackage">
            <methods>
            <include name="firstTestCase"></include>
            <include name="secondTestCase"></include>
            <exclude name="thirdTestCase"></exclude>
            </methods>
        </class>           
    </classes>
  </test>
</suite>

4.Now execute the “testng-methods.xml” file from eclipse as TestNG suite. Then you can see the following output.

2. TestResult with include and exclude tags

In the above output, we can not see the “thirdTestCase” in the test result as this is excluded from the configuration file.



5.We have multiple test methods in a test class and only want to execute single test method from the class, then we can include only that test method in the configuration file and no need to mention the exclude for the other test methods.

Below is the sample code :


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test name="ClassTest">
    <classes>
        <class name="com.test.firstpackage.FirstClassInFirstPackage">
            <methods>
            <include name="firstTestCase"></include>            
            </methods>
        </class>           
    </classes>
  </test>
</suite>

6.This is the output for the above code:

3. TestResult with only one include

In the above output, we can see only one test method executed and remaining are exclude from the execution.

7.Now we want to exclude only one method and remaining should include in the execution. Then we just mention one exclude tag with the particular method. This should include other methods indirectly.

Below is the sample code :


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test name="ClassTest">
    <classes>
        <class name="com.test.firstpackage.FirstClassInFirstPackage">
            <methods>
            <exclude name="firstTestCase"></exclude>            
            </methods>
        </class>           
    </classes>
  </test>
</suite>

8.This is the output for the above code:

4. TestResult with only one exclude

In the above output, we can see all test method executed except one which we mentioned in the configuration file.

This way we can configure the methods in the testng xml configuration file to execute the scripts as methods and can analyse the test results.



Share this post: on Twitter on Facebook

Adding Classes to testng.xml file TestNG Annotations

Related Posts

Excel to DataProvider

TestNG

Read data from Excel to DataProvider in Selenium

Running TestNG programmatically

TestNG

Running TestNG Tests Programmatically

EXECUTING ONLY FAILED TESTS

TestNG

Executing Only Failed Tests in TestNG

Capture Screenshot for Failed Tests

TestNG

Capture Screenshot for Failed Tests in TestNG

Preserve Order in TestNG

TestNG

Preserve Order in TestNG

PRIORITIZING TESTS

TestNG

Prioritizing Tests in TestNg

EXECUTE MULTIPLE XML FILES

TestNG

Execute Multiple XML files in TestNG

CUSTOM REPORTER IN TESTNG (1)

TestNG

Custom Reporter in TestNG

CUSTOM LOGGER IN TESTNG

TestNG

Custom Logger in TestNG

ASSERTIONS

TestNG

TestNG Assertions

Newsletter

Recent Posts

  • TAKING WEB ELEMENT SCREENSHOT IN SELENIUMHow to Capture WebElement Screenshot in Selenium Webdriver using selenium 4
    December 15, 2019
  • How To SWAP Two Numbers in Java Without using Temp VariableHow to SWAP Two Numbers in Java Without Temp variable or Without Third variable
    December 8, 2019
  • How to Swap Two Numbers in Java with Temp VariableHow to SWAP Two Numbers in Java using Temp Variable
    December 1, 2019
  • How to Read Properties file in JavaHow to Read Data From Properties File in Java
    November 27, 2019
  • Compare two arrays in java with out inbuilt functionsHow to Compare Two Arrays in Java without built-in functions
    November 16, 2019
© Selenium Webdriver Appium Complete Tutorial 2025