Showing posts with label Best Tutorials on TestNG. Show all posts
Showing posts with label Best Tutorials on TestNG. Show all posts

Monday, April 20, 2020

TestNG Tutorial Advanced Topics - Part 1

We are now going to learn more advance stuff about the TestNG in this tutorials.

In first tutorial of TestNG (Introduction of TestNG), you get enough knowledge how to install, configure and different annotations.


Default Project Folder Structure With TestNG Library





Let's see the difference on JAVA class and TestNG class.


Below is difference in Option when you try to run the Java Class and TestNG Class.


TestNG Class: Similar to Java class but dont contains Main Method and contains different annotations to run as testng class.



Note: We never use Main Method in TestNG Class.


Java Class Run





TestNG Class Run






How TestNG Change the Reporting in Selenium: Main Benefits of TestNG

Without TestNG: In Selenium without TestNG we have to add required PRINT LINE statements to verify that we are on right track or not or we need to print same information back and forth to excel or other files.


Without TestNG - When we run the Selenium Script, we can see output in Console.





With TestNG - We can see the complete format of how many test case run, pass, failed and skipped.




Also one separate folder 'test-output' get generated every time we run TestNG class and it has different kind of reports. 




Mostly used format is 'Index.html' which display below structure in browser.


TestNG Tutorial Advanced Topics - Part 2

What is TextNG.XML?

In simple term testng.xml is heart of testng class. It contains all key information about the project. If you want to make any customization on your program to run you can do it from here.

It is very useful to organize the priorities of many thing while running your program. Like Groups, Include, Exclude, Parallel, Parameters etc.

Benefits of Testng.XML file:


  • It allows to pass parameters to the test cases
  • Allows to add group dependencies
  • Allows to add priorities to the test cases
  • Allows to configure parallel execution of test cases
  • Allows to parameter the test cases
  • TestNG listeners can be implemented at Suite level
  • To Integrate TestNG framework with third party tools like Jenkins testng.xml is required
  • Testng.xml file allows to include or exclude the execution of test methods and test groups

How to Generate Textng.xml file for the Project:

Step 1 : Select your project folder in which you have all the packages.

Step 2 : Right click on selected project folder.

Step 3 : Mouse hover on TestNG option from the given list which is located at the bottom.

Step 4 : Select "Convert to TestNG" option.



Step 5 : A modal will appear with the name of "Generate testng.xml" 

You can give name, select parallel mode and thread count if you want to add them by default while creating file.




Sample Format of TextNG.XML






TestNG Tutorial Advanced Topics - Part 3

In this tutorial we are going to see different keywords we use in TestNG to structured the Test and its defined methods.

1> Priority Keyword: If you want the methods to be executed in a different order, use the parameter "priority". 

Parameters are keywords that modify the annotation's function.

Parameters require you to assign a value to them. You do.this by placing a "=" next to them, and then followed by the value.

TestNG will execute the @Test annotation with the lowest priority value up to the largest. There is no need for your priority values to be consecutive.

You can run a single or multiple test cases in your TestNG code.

If test priority is not defined while, running multiple test cases, TestNG assigns all @Test a priority as zero(0).

Syntax: 



Example:  


  • Normal report without setting up the priorities where Method 1 run first and then Method 2. 

  • Now we assign the priority to change the execution mode of the class methods.

  • Execution order get changed when we setup the priority for them to run in specific order.


😀 TIME TO IMPLEMENT the Same IN YOUR ECLIPSE NOW 😀

2> Parallel Testing in TestNG: TestNG parallel execution of tests, classes and suites with examples. Learn how to run testng tests and suites in parallel or single test in multiple threads.

Parallelism or multi-threading in software terms is defined as the ability of the software, operating system, or program to execute multiple parts or sub-components of another program simultaneously.

In testng.xml, if we set 'parallel' attribute on the tag to 'tests', testNG will run all the ‘@Test’ methods in tag in the same thread, but each tag will be in a separate thread.

If we want to run methods/classes in separate threads, we need to set 'parallel' attribute on the tag to 'methods' / 'classes'

The attribute thread-count allows you to specify how many threads should be allocated for this execution.

Syntax: 


Example: Below command will run all methods parallel with maximum 5 session at once for below mentioned class.




You can defined the Methods, Classes and Tests as per your requirement in Parallel Mode:




😀 TIME TO IMPLEMENT the Same IN YOUR ECLIPSE NOW 😀


3> Groups Keyword: Grouping test methods is one of the most important features of TestNG. 

In TestNG users can group multiple test methods into a named group. You can also execute a particular set of test methods belonging to a group or multiple groups.

You can achieve the same on two ways:

1> Groups on Methods Level: You can define the grouping at Method level, so you can see the grouped result.



2> Groups on Class Level: You can define the grouping at Class level, so in case you did not define the group at method level it will take by default from class.



Once you grouped them together and run the class, it will display the output in Index.HTML report file as below with grouping.




😀 TIME TO IMPLEMENT the Same IN YOUR ECLIPSE NOW 😀
4> invocationCount - threadPoolSize

invocationCount determined how many times TestNG should run this test method.

threadPoolSize attribute tells TestNG to create a thread pool to run the test method via multiple threads. With thread pool, it will greatly decrease the running time of the test method.

Example: To run below method with 10 times and thread pool 2 means create two pools every time. Hence system will load both instance 5 times and record the time.




We can increase and reduce the threadPoolSize to see the variation in the record time.




😀 TIME TO IMPLEMENT the Same IN YOUR ECLIPSE NOW 😀

~~~~ Other TestNG Related Tutorial Links~~~~

TestNG Tutorial - Introduction Part

TestNG Tutorial Advanced Topics - Part 1 (Configuration of TestNG)

TestNG Tutorial Advanced Topics - Part 2 (TestNG.XML File)

TestNG Tutorial Advanced Topics - Part 3 (Priority, Groups, Parallel Mode, invocationCount, ThreadPool)

TestNG Tutorial Advanced Topics - Part 4 (Parameterization, Data Provider)

TestNG Tutorial Advanced Topics - Part 5 (Exclude,Listeners,DependsOnMethod)