Showing posts with label Best Explanation of TestNG. Show all posts
Showing posts with label Best Explanation of TestNG. Show all posts

Monday, April 20, 2020

TestNG Tutorial Advanced Topics - Part 5

Tutorial Agenda

1> Exclude-Include:- TestNG provides the feature of enabling and disabling the test cases. We can disable a set of test cases from getting executed. 

For example, we have scenario where a serious bug occurs in a feature due to certain tests, so we need to disable the test cases from being executed.

You can disable or exclude the test cases by using the enable attribute to the @Test annotation and assign False value to the enable attribute.

Syntax: @Test(enabled=false)

We can achieve this thing in two way as below:

1> We can add value in the @Test annotation as below:

Example:


Result:


2> We can add Exclude tag into the testng.xml file:

Example:



Result:









😀 
TIME TO IMPLEMENT the Same IN YOUR ECLIPSE NOW😀 

2> DependsOnMethods: dependsOnMethods attribute on a test method [test1 e.g.] specifies all the test methods [test2, test3,..] this test method depends on. 

It means test1 will start execution only after all the tests it depends on executed successfully. 

If any of the tests specified via dependsOnMethods attribute failed, then test1 will be skipped.

alwaysRun attribute on a test method to true forces the execution of this test even if the tests it depends on were failed.

Example: In below class we have four test method and last two depends on first two method.

1> testAdd(): Everything is fine with code, This will PASS

2> testDivide(): Intentionally we given wrong logic to divide number by 0, hence this will FAIL.

3> testProcessRealNumbers(): This method depends on both above methods to pass.

dependsOnMethods={"testAdd", "testDivide"}

So this will get SKIPPED as second method going to FAIL.

4> testProcessEvenNumbers(): This method again depends on both above methods to pass, also we have added ALWAYS RUN as True means this will pass the test method even one of the method from dependsOn is fail.

Sample Code:

Result as described above:


😀 TIME TO IMPLEMENT the Same IN YOUR ECLIPSE NOW😀 

3> TestNG Listeners: 

TestNG provides the @Listeners annotation which listens to every event that occurs in a selenium code. 

Listeners are activated either before the test or after the test case. 

It is an interface that modifies the TestNG behavior. For example, when you are running a test case either through selenium or appium and suddenly a test case fails. 

We need a screenshot of the test case that has been failed, to achieve such scenario, TestNG provides a mechanism, i.e., Listeners. When the test case failure occurs, then it is redirected to the new block written for the screenshot.

Listeners are implemented by the ITestListener interface. 

An ITestListener interface has the following methods:



Below is the description of all listeners methods:

onTestStart(): An onTestStart() is invoked only when any test method gets started.
onTestSuccess(): Method is executed on the success of a test method.
onTestFailure(): Method is invoked when test method fails.
onTestSkipped(): Method run only when any test method has been skipped.
onTestFailedButWithinSuccessPercentage():Method is invoked each time when the test method fails but within success percentage.
onStart(): Method is executed on the start of any test method.
onFinish(): Method is invoked when any test case finishes its execution.

Let's Implement this:

First, We need to create one Listener class which contains all methods definition and print line information.

Sample Definition should be as below:


Below is the definition of all method on above class.


Now, Create actual test case which contains TEST method here.
Need to call that listener as shown below:

@Listeners(All_Practicals.Listerner_ITestListener.class)


Once you run the test then it will add some extra line into the log as below.



😀 TIME TO IMPLEMENT the Same IN YOUR ECLIPSE NOW😀

Reference Site:https://www.javatpoint.com/

TestNG Tutorial Advanced Topics - Part 4

TestNG Parameters are the arguments that we pass to the test methods. 

Parameterisation is always helpful in any application to store the key values into the file and call it whenever required across the application, there are two ways through which we can pass the parameters to the test methods:


  • TestNG Parameters
  • TestNG DataProviders


In this topic, we will learn about the TestNG Parameters. We will learn about the parameterisation in the xml file.

Suppose we want to set the global variables such url settings, username, password or API Keys, there are some values which are constant in all the test cases, in such case we use the TestNG Parameters.

1> TestNG @Parameters – test parameters with testng.xml

You already know about testng.xml File. If not then Click Here.

Now, we need to store username and password in testng.xml file and need to pass it to class called 'Parameterized_Keyword'.

So we have added two parameter tags here in testng.xml.




Now, Below way we can call these two parameters using @Parameters annotation in Parameterized_Keyword.Java class.





Note: testng.xml parameter name should match with name we are passing into class file under @Parameter annotation to call it.

😀 TIME TO IMPLEMENT the Same IN YOUR ECLIPSE NOW 😀

2> TestNG @DataProvider – dataProvider in the class itself

A test method that uses DataProvider will be executed a multiple number of times based on the data provided by the DataProvider

The test method will be executed using the same instance of the test class to which the test method belongs.

Marks a method as supplying data for a test method. The annotated method must return an Object[ ][ ], where each Object[ ] can be assigned the parameter list of the test method. 

The @Test method that wants to receive data from this DataProvider needs to use a dataProvider name equals to the name of this annotation.

Lets understand this by example practically.

We have one TestNG Class, Which contains below @DataProvider having name=SearchProvider.



It contains two dimensional Object array to pass the element values . Hence return values show the same.

This will pass the value to @Test method one by one.

Now, we have one @Test method in the same TestNG class. We pass the dataProvider using the same name as we have given in @DataProvider to pass the values one by one.



Below is the result when we run this code. Console & TestNG Result.




😀 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)



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)