TestNG is a testing framework for the Java programming language created by Cédric Beust and inspired by JUnit and NUnit. The design goal of TestNG is to cover a wider range of test categories: unit, functional, end-to-end, integration, etc., with more powerful and easy-to-use functionalities. It is an open source automated testing framework; where NG of TestNG means Next Generation. TestNG is similar to JUnit but it is much more powerful than JUnit but still it’s inspired by JUnit. It is designed to be better than JUnit, especially when testing integrated classes. .
TestNG is a testing framework designed to simplify a broad range of testing needs, from unit testing (testing a class in isolation of the others) to integration testing (testing entire systems made of several classes, several packages and even several external frameworks, such as application servers).
Writing a test is typically a three-step process:
  • Write the business logic of your test and insert TestNG annotations in your code.
  • Add the information about your test (e.g. the class name, the groups you wish to run, etc...) in a testng.xml file or in build.xml.
  • Run TestNG.
What Is TestNG?
TestNG is open source framework which is inspired from NUnit and JUnit with introducing few new features and functionality compared to NUnit and JUnit to make it easy to use and more powerful. We can use TestNg with selenium Webdriver software testing tool to configure and run test cases very easily, easy to understand, read and manage test cases, and to generate HTML or XSLT test reports.
What are the annotations available in TestNG?
Here is the list of annotations that TestNG supports.
@BeforeSuite: This method will be run only once before all tests in this suite have run.
@AfterSuite: This method will be run only once after all tests in this suite have run.
@BeforeClass: This method will be run only once before the first test method in the current class is invoked.
@AfterClass: This method will be run only once after all the test methods in the current class have run.
@BeforeTest: This method will be run before any test method belonging to the classes inside the <test> tag is run.
@AfterTest: This method will be run after all the test methods belonging to the classes inside the <test> tag have run.
@BeforeGroups: The list of groups that this configuration method will run before. This method is guaranteed to run shortly before the first test method that belongs to any of these groups is invoked.
@AfterGroups: The list of groups that this configuration method will run after. This method is guaranteed to run shortly after the last test method that belongs to any of these groups is invoked.
@BeforeMethod: This method will be run before each test method.
@AfterMethod: This method will be run after each test method.
@Data Provider: 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.
@Factory: Marks a method as a factory that returns objects that will be used by TestNG as Test classes. The method must return Object [ ].
@Listeners: Defines listeners on a test class.
@Parameters: Describes how to pass parameters to a @Test method.
@Test: Marks a class or a method as a part of the test.
Benefits of using Annotations:
  • It identifies the methods it is interested in by looking up annotations. Hence method names are not restricted to any pattern or format.
  • We can pass additional parameters to annotations.
  • Annotations are strongly typed, so the compiler will flag any mistakes right away.
  • Test classes no longer need to extend anything (such as Test Case, for JUnit 3).
What are the advantages of TestNG?
  • Annotations helps to set program/function priority easy
  • Allow to generate HTML report of execution
  • TestNG provides parallel execution of test methods
  • It generate logs
  • Three kinds of report generated
  • Order of execution can be changed
  • It has support for parameterizing test cases using @Parameters annotation
  • An xml file can be generated to execute the entire test suite. In that xml file we can   rearrange our execution order and we can also skip the execution of particular test case.
  • It allows data driven testing using @DataProvider annotation.
What is Parameterization in TestNG?
One of the important features of TestNG is parameterization. This feature allows user to pass parameter values to test methods as arguments. This is supported by using the @Parameters annotation. Parameterization plays very important role in automation testing. It is required when we don’t want to hard code the value in our code and it is not a good practice as well. Then parameterization comes into picture, so we will not have to hard code any value and it will be easy to handle the new data also.
Parameterization in testing is achieved using two methods:
Using testing.xml file
Using DataProviders

What is Dataproviders in TestNG?
A Data Provider is a method annotated with @DataProvider. This annotation has only one string attribute: its name. If the name is not supplied, the data provider’s name automatically defaults to the method’s name. A data provider returns an array of objects.

What is testing.xml file?
Testing.xml file captures your entire testing in XML. This file makes it easy to describe all your test suites and their parameters in one file, which you can check in your code repository or e-mail to coworkers. It also makes it easy to extract subsets of your tests or split several runtime configurations

What is the importance of testng.xml file?
  • In a Selenium TestNG project, we use testng.xml file to configure the complete test suite in a single file. Some of the features are as follows.
  • Testng.xml file allows to include or exclude the execution of test methods and test groups
  • It allows to pass parameters to the test cases
  • Allows to parameterize the test cases
  • Allows to add group dependencies
  • Allows to add priorities to the test cases
  • Allows to configure parallel execution of test cases.
How can you run the JUnit tests using TestNG?
You can run the JUnit tests using TestNG by Placing JUnit library on the TestNG classpath, so it can locate and use JUnit classes Change your test runner from JUnit to TestNG in Ant and then run TestNG in “mixed mode”. This will bring all your test in the same this approach also enables you to convert your existing JUnit test to TestNG.

What is the same process both TestNG and JUniT?
  • Annotations: Few annotations are similar in both frameworks suite like @Test, @BeforeClass, @AfterClass. JUnit's Annotations @Before and @After are similar to TestNG's @BeforeMethod and @AfterMethod annotations.
  • We can create test suite in JUnit and TestNG both frameworks.
  • Timeout Test is possible very easily in both the frameworks.
  • We can ignore specific test case execution of software web application from suite in both the frameworks.
  • It is possible to create expected exception test for software web application in both the frameworks. 
What is suite test?
A test suite is a collection of test cases intended to test a behavior or a set of behaviors of software program. In TestNG, we cannot define a suite in testing source code, but it is represented by one XML file, as suite is the feature of execution. It also allows flexible configuration of the tests to be run. A suite can contain one or more tests and is defined by the <suite> tag.
<suite> is the root tag of your testng.xml. It describes a test suite, which in turn is made of several <test> sections. XML file is used to run the suite test.

What is group in TestNG?
Selenium does not support grouping functionality but TestNG does so using TestNG grouping feature we can categorized our test cases. Here group is an attribute and we can create multiple groups in 1 Class.

What is Listeners in TestNG?
A TestNG listener always extends the marker interface org.testng.ITestNGListener. Using listeners, one can extend TestNG in their dealings with notifications, reports and test behavior. The main purpose of using listeners is to create logs. There are many types of listeners such as WebDriver Listeners and TestNG Listeners. Using TestNG listeners we could generate logs and customize TestNG Reports.

What are the different types of Listeners in TestNG?
There are many types of listeners which allows you to change the TestNG's behavior.
Below are the few TestNG listeners:
  • ITestListener
  • IAnnotationTransformer
  • IAnnotationTransformer2
  • IConfigurable
  • IConfigurationListener
  • IExecutionListener
  • IHookable
  • IInvokedMethodListener
  • IInvokedMethodListener2
  • IMethodInterceptor
  • IReporter
  • ISuiteListener
What is Assertion?
Asserts helps us to verify the conditions of the test and decide whether test has failed or passed. A test is considered successful ONLY if it is completed without throwing any exception.

What is execution test?
The test cases are executed using TestNG class. This class is the main entry point for running tests in the TestNG framework. Users can create their own TestNG object and invoke it in many different ways such as −
  • On an existing testng.xml.
  • On a synthetic testng.xml, created entirely from Java.
  • By directly setting the test classes.
What is exception test?
TestNG gives option for tracing the Exception handling of code. You can test whether a code
Throws the expected results or not. The expectedExceptions parameter is availed along with @Test annotation.

What is Timeout Test in TestNG?
When a test method is taking longer time than the specified time to execute, then that test method will be terminated and marked as failed, this feature is available in both JUnit 4 and TestNG. While running test methods there can be cases where certain test methods get struck or may take longer time than to complete the execution than the expected. We need to handle these type of cases by specifying Timeout and proceed to execute further test cases / methods
TestNG allows user to configure a time period to wait for a test to completely execute. Timeout can be configured in two ways:
At suite level: This will be applicable for all the tests in the said TestNG test suite
At each test method level: This will be applicable for the said test method and will override the time period if configured at the suite level
To specify timeout duration, use “timeOut” attribute of @Test annotation.

What is the difference between JUnit and TestNG unit testing frameworks?
  • TestNGsupport @Beforetest,@Aftertest ,@BeforeSuite, @AfterSuite, @BeforeGroups and @AfterGroups which are not supported in JUnit.
  • In TestNG, Parameterized test configuration is very easy while it is very hard to configure Parameterized test in JUnit.
  • TestNG support group test but it is not supported in JUnit.
  • TestNG has a feature to configure dependency test. Dependency test configuration for software web application is not possible in JUnit.
  • Test prioritization, parallel testing is possible in TestNG. It is not supported by JUnit.
What is the Parallel Execution of Classes in TestNG?
TestNG provides an ability to run test classes in parallel. By using parallel execution of
classes, each class will be started and executed simultaneously in different threads.
Let us look at basic example for Parallel Execution of Classes using testng.xml.
We will create a two classes with two test methods each and try to execute in different threads. Create class and name it as: TestParallelClassOne.java.

How to run multiple testng.xml file in TestNG?
Once you start designing test case continuously your test cases count will increase so you have to manager your testcases accordingly. TestNG having very cool concept of creating an xml file and put all relevant testcases inside xml file and run it as a Test Suite.

Mention different ways in which you can produce reports for TestNG results?
There are two ways to produce a report with Test NG, they are
Listeners: For a listener class to implement, the class has to implement the org.testng./TestListener interface. These classes are informed at runtime by TestNG when the test begins, finsishes, skips, passes or fails.
Reporters: For a reporting class to implement, the class has to implement an org.testng/Reporter interface. When the whole suite run ends, these classes are called. When called, the object consisting the information of the whole test run is delivered to this class.

How to execute only failed test cases in Selenium?
 This feature of TestNG that you can run only failed test cases explicitly. This can be easily done by running one simple testng-failed.xml. Note- If your test cases are failing then might be two reason
  • Some locator has been changed in application- so in this case you need to modify your script in other words you have to refine your script.
  •  Either functionality has been broken- in this case you have to raise a
How to get testng-failed.xml?
  • If your testcases are failing then once all testsuite completed then you have to refresh your project.
  • Check test-output folder at last you will get testng-failed.xml
  • Now simply run testng-failed.xml.
What does @Test (invocation Count=?) and (threadPoolSize=?) indicates?
@Test (threadPoolSize=?): The threadPoolSize attributes tell TestNG to form a thread pool to run the test method through multiple threads. With thread pool, the running time of the test method reduces greatly.
@Test (invocation Count=?): The invocation count tells how many times TestNG should run this test method.

What is the time unit we specify in test suites and test cases?
We specify the time unit in test suites and test cases is in milliseconds.

How TestNG allows to state dependencies?
TestNG allows two ways to declare the dependencies.
Using attributes dependsOnMethods in @Test annotations
Using attributes dependsOnGroups in @Test annotations

How to skip a @Test method from execution in TestNG?
By using throw new SkipException ().Once SkipException () thrown, remaining part of that test method will not be executed and control will goes directly to next test method execution
Throw new SkipException ("Skipping - This is not ready for testing "); 1Throw new SkipException ("Skipping - This is not ready for testing ");
Request to Download PDF

Post A Comment:

0 comments: