What is TestNG?
TestNG is the framework created for executing unit tests in java program by the developers.
TestNG is also used by the software testers to efficiently run the automated test scripts created in Selenium Webdriver. Its full form is the “Testing New Generation” framework.
What are the annotations used in TestNG?
There are three sections of annotation in TestNG:
@BeforeSuite, @BeforeClass, @BeforeTest, @BeforeMethod are the precondition annotations.
@Test is the test annotation
What is the sequence of execution of the annotations in TestNG?
@BeforeSuite
@BeforeTest
@BeforeClass
@BeforeMethod
@Test
@AfterMethod
@AfterClass
@Aftertest
@AfterSuite
What are the advantages of TestNG?
How to set priorities in TestNG?
===========
There are always more than one test or method in the class. If we do not prioritize these tests or methods, then the methods are selected alphabetically and executed while execution.
If we want to run the tests in the sequence we want, then we need to set the priority along with the @Test annotation.
This can be done as follows:
@Test (priority=1), @Test (priority=2)
Consider the following Example:
@Test (priority=2)
public void getText()
{
driver.findElement(By.id(“id”)).getText();
}
@Test(priority=1)
public void clickelement()
{
driver.findElement(By.id(“id”)).click();
}How will you define grouping in TestNG?
Groups are the collection of multiple test case methods combined into one single unit. By grouping, we can operate directly onto the group, which will reflect on all the test case methods under it. Moreover, in TestNG, we can also create a group of groups as a bigger unit of test methods.
define the Groups in TestNG by passing the “groups” parameter to the Test annotation with the value being the group name
@Test(groups=”title”)
What is a dependency on TestNG?
```There are some methods on which many methods are dependent on. ***For Example***, If we want to test any application, and if the login page of the application is not working then we won’t be able to test the rest of the scenarios. So, LoginTest is the method on which many tests are dependent on. ============== **Hence, we will write as follows:**
@Test(dependsOnMethods=”LoginTest”)
Public void homePageLaunched()
{
}
~~~
What is InvocationCount in TestNG?
If we want to execute a test case “n” number of times, then we can use the invocationCount attribute as shown in the below example.
====================
Example:
@Test(invocationCount=8)
Public void print()
{
}What is timeOut in TestNG?
If any method in the script takes a long time to execute, then we can terminate that method using “timeout” in TestNG.
@Test(timeout = 5000)
In this case, the method will get terminated in 5000 ms (5 seconds) and the test case is marked as “Failed”.
How to handle exceptions in TestNG?
If there are some methods from which we expect some exceptions, then we can mention the exception in @Test annotation so that the test case does not fail.
==========
Example: If a method is expected to have “numberFormatException” exception, then the test case will fail because of this exception if no try-catch block is specified.
But we can do it in TestNG by using “expectedException” attribute as follows.
@Test(expectedException=numberFormatException.class)
Then the test case will run without failing.
What are the common TestNG assertions?
An asset is a piece of code that helps us verify if the expected result and the actual result are equal or not. In TestNG, we leverage the inbuilt “Assert” class and a lot of its method to determine whether the test case passed or failed. Additionally, in TestNG, a test case acts as a “pass” if none of the assert methods throws an exception during the execution. The syntax for TestNG assert is:
How to disable a test in TestNG?
To disable a test in TestNG, we have to use the “enabled” attribute as follows:
@Test(enabled=”false”)
How to pass parameter in the test case through the testng.xml file?
If we have a class in which a login method is defined, then we can pass the login parameters to this login method from the testing.xml file
We will have to use the “@parameters” annotation as follows:
@Parameters({"username","password"})
@Test
public void loginapp()
{
driverget(“appname”);
driver.findElement(By.id(“login”)).sendkeys(username);
driver.findElement(By.id(“password”)).sendkeys(password);
}==========
Now, go to the testng.xml file and enter the parameters there as follows:
<Suite name = “suitename”> <test name =”testname”> <parameter name =”user_name” value=”user1”/> <parameter password =”password” value =”pass1”/> <Classes> <class name =”passingparameters”/> <classes/> <test/> <Suite/>
What is the need to create a testng.xml file?
When we test a project using Selenium Webdriver, it has a lot of classes on it. We cannot choose these classes one by one and put them for automation. Hence we need to create a suite so that all the classes run in a single test suite.
We can achieve this by creating a testing.xml file.
What are the advantages of TestNG?
What are the types of Asserts in TestNG
To validate the results (pass/fail), we have to use the assertion.
There are two types of assert in TestNG:
Hard Assert is the normal assert which is used to do validations in the TestNG class.
We have to use Assert class for hard assert as follows:
Assert.assertEquals(actual value, expected value);
If the hard assert fails, then none of the code gets executed after the assert statement.
If we want to continue the test execution even after the assert statement fails, then we have to use soft assert.
To create a soft assert, we have to create an object of a “softAssert” class as follows:
softAssert sassert = new softAssert();
sassert.assertAll();
So now if the test case fails, the execution is not terminated when we use soft assert.
How to throw a SKIP Exception in TestNG?
If we want to SKIP any Test using testing, then we have to use the SKIP exception in TestNG.
It is written as follows:
public void skipExc()
{
System.out.println("SKIP me");
throw new skipException(“Skipping skipExc”);
}
}What is the difference between a TestNG test and a TestNG test suite?
TestNG test suite refers to a collection of tests that we can run simultaneously with the help of the TestNG XML file. On the other side, a TestNG test is a single test case file, and when we say “we are running a TestNG test case”, we simply mean we are running a single test case file.
What are the types of reports generated in TestNG by default?
TestNG generates two types of reports by default after the execution of all the test methods finishes. They are:
How do you exclude a group from the test execution cycle?
Excluding a group in TestNG denotes that this particular group refrains from running during the execution, and TestNG will ignore it. Additionally, the name of the group that we want to exclude is defined in the XML file by the following syntax:
<groups>
<run>
<exclude name = "demo">
</exclude>
</run>
</groups>What is meant by dependency in TestNG?
Dependency in TestNG is a process of making one test dependent on the other test. By providing dependencies in the test methods, we assure that a test method B would only run if test method A runs (given B depends on A). Moreover, in TestNG, we can also have one test method dependent on multiple tests.
How do you create dependencies in TestNG?
We can create the dependent tests in TestNG by providing the dependsonMethods parameter on the @Test annotation. The value of the attribute is the name of the method on which we want this method to depend. The usage of this method is as follows:
import org.testng.annotations.Test;
public class DependsOnTest {
@Test (dependsOnMethods = { "OpenBrowser" })
public void SignIn() {
System.out.println("User has signed in successfully");
}
@Test
public void OpenBrowser() {
System.out.println("The browser is opened");
}
}===========
TestNG also allows us to create dependencies between groups through the TestNG XML file. Such dependencies denote the dependence of one group onto another. The following code demonstrates how to achieve the same goal:
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="TestNG XML Dependency Suite" >
<test name="ToolsQA" >
<groups>
<dependencies>
<group depends-on= "openbrowser" name= "login"></group>
</dependencies>
</groups>
<classes>
<class name="GroupDependency" />
</classes>
</test>
</suite>What is meant by parallel test execution in TestNG?
The parallel test execution means executing different test methods simultaneously, i.e., parallelly in TestNG. It is achieved by creating threads and assigning these threads to different test methods (which is done automatically and is an operating system’s job). Moreover, running the tests parallelly rather than sequentially is very efficient.
On what levels can we apply parallel testing in TestNG?