TestNG – 8 || Creation and execution of Test Suites.

Test Suites are a combination of one or more classes (class files) and programs. Test Suite is basically an XML file, used to group and organize the test cases from different class files.

Till now we have seen that we can run Test Cases written in any class file directly using Right Click on that Class file. But in real scenarios, we have to write hundreds of files to create any framework. In that case, we have to use Test Suites to manage and organize these Test Cases for execution. Will learn the same in further blogs.

In this chapter will see the basic structure of XML, execution process and result analysis using only one Class file.

Simple Suite Example
Simple Suite Example

Test Suites XML Structure

Steps to Create TestNG XML File

  1. Select Project > Right Click on Project
  2. Click New > Other
  3. Search for TestNG and expand it
  4. Click on TestNG class > Next
  5. Select the Source(src) folder of the project.
  6. Click to select Package under the src folder “testSuitePackage“.
  7. Give any name in the ‘Class Name’ field “LoginPage“.
  8. Type XML file name in the ‘XML Suite file’ field “Simple_TestSuite.xml

LoginPage.java

package testSuitePackage;
import org.testng.annotations.Test;

/**
 * @author ashok.kumar
 */
public class LoginPage {
	
	@Test
	public void launchURL()
	{
		System.out.println("URL Launched Successfully.");
	}
	
	@Test(dependsOnMethods="launchURL")
	public void login()
	{
		System.out.println("User Logged-In Successfully.");
	}
	
	@Test(dependsOnMethods="login")
	public void changeProfile()
	{
		System.out.println("User Profile changed Successfully.");
	}
	
	@Test(dependsOnMethods="login")
	public void deactivateSubscription()
	{
		System.out.println("Subscription deactivated successfully.");
	}
	
	@Test(dependsOnMethods="login")
	public void logout()
	{
		System.out.println("User Logged-out Successfully.");
	}
}

Simple_TestSuite.xml

<?xml version="1.0" encoding="UTF-8"?>
<suite name="SimpleSuiteExample" parallel="false">
 <test name="AlphaTestCases">
    <classes>
      <class name="testSuitePackage.LoginPage"/>
    </classes>
  </test> 
</suite>

All the rules which we have learned about priority, alphabetical order, beforeMethod, before class will remain the same for each Test set.

Executing the Multiple Test cases

  1. Right click on the XML file “Simple_TestSuite.xml
  2. Select on RunAs
  3. Click on TestNG Suite

Result Analysis wrt XML file

The above test suite will execute all the test-cases (methods) coded in the LoginPage.java file. The output result of the program can be seen using the console of the Eclipse.

To view the HTML result after execution, refresh the test-output folder in the Project Explorer navigation. Observe that a new folder with the suite nameSimpleSuiteExample” is created as like

test-output folder

It includes the result of each test, which we have defined as test name “AlphaTestCases” and so on. The result in the index.html and emailable-report.html would be analyzed as we have discussed in the second Chapter.

Tags in Test Suites

In the above example, all the methods get executed, but what if we want to perform a regression/smoke and want to run only specific Test Cases from the entire program. We can achieve the same with few modifications in our existing Test Suite XML File.

<?xml version="1.0" encoding="UTF-8"?>
<suite name="SimpleSuiteExample" parallel="false">

<!--  <test name="AlphaTestCases">
    <classes>
      <class name="testSuitePackage.LoginPage"/>
    </classes>
  </test>  -->
  
  <test name="RegressionTestCases">
    <classes>
      <class name="testSuitePackage.LoginPage"/>
      <methods>
      <include name="login"/>
      <include name="launchURL"/>
      <include name="logout"/>
      </methods>
    </classes>
  </test>

</suite> <!-- Suite -->

See above example, we have added new section <test name=”RegressionTestCases”> in the xml file. This new section contains <include> tags to determine which methods need to be executed from the entire class file.

  • <?xml>: XML standard tag which needs to be use by default.
  • <suite name=”String”>: Defines the name of the Suite.
  • <test name=”String”>: Define the Test Case Name in a Test Suite.
  • <classes>: Contains list of all class files for execution. It can have multiple class tags.
  • <class name=”packageName.className”>: Contains reference of one class file including respective package name.
  • <methods>: If we don’t use this tag all the methods having @Test annotation will be executed. But in case we have to run only few methods from a class we could define the same under this tag.
  • <include name=”methodName”>: This tag defines the name of each method used in a class.

Make sure to comment AlphaTestCases section otherwise both the tests would run. Now run the test suite xml file and will observe that only the methods mentioned under <include> tag executed.

Result Analysis with Two Tests

If we run the above Test Suite XML file without commenting any Test, we will observe below changes in the test-output folder. Now there would be two HTML files present, containing the result for each Test.

test-output folder

The emailable-report.html will look like below sample.

emailable-report.html

Related Links:

Java Basics:

Java File Handling:

OOPs Concept:

Java Question And Answer:

Java Programs:

Excel:


130total visits,1visits today

Leave a Reply

Your email address will not be published. Required fields are marked *