Question: What is TestNG?
Answer:
TestNG is a testing framework designed to simplify a broad range of testing needs, from unit testing to integration testing.
Question. What are the annotations available in TestNG?
Answer:
@BeforeTest
@AfterTest
@BeforeClass
@AfterClass
@BeforeMethod
@AfterMethod
@BeforeSuite
@AfterSuite
@BeforeGroups
@AfterGroups
@Test
Question: What is TestNG Assert and list out some common Assertions supported by TestNG?
Answer:
TestNG Asserts help us to verify the condition of the test in the middle of the test run. Based on the TestNG Assertions, we will consider a successful test only if it is completed the test run without throwing any exception.
Some of the common assertions supported by TestNG are
assertEqual(String actual,String expected)
assertEqual(String actual,String expected, String message)
assertEquals(boolean actual,boolean expected)
assertTrue(condition)
assertTrue(condition, message)
assertFalse(condition)
assertFalse(condition, message)
Different TestNG Asserts Statements:
a) Assert.assertEqual(String actual,String expected) :
Asserts that two Strings are equal. If they are not, an AssertionError is thrown.
Parameters:
actual – the actual value
expected – the expected value
b) Assert.assertEqual(String actual,String expected, String message) :
Asserts that two Strings are equal. If they are not, an AssertionError, with the given message, is thrown.
Parameters:
actual – the actual value
expected – the expected value
message – the assertion error message
c) Assert.assertEquals(boolean actual,boolean expected) :
Asserts that two booleans are equal. If they are not, an AssertionError is thrown.
Parameters:
actual – the actual value
expected – the expected value
d) Assert.assertTrue(condition) :
Asserts that a condition is true. If it isn’t, an AssertionError is thrown.
Parameters:
condition – the condition to evaluate
e) Assert.assertTrue(condition, message) :
Asserts that a condition is true. If it isn’t, an AssertionError, with the given message, is thrown.
Parameters:
condition – the condition to evaluate
message – the assertion error message
f) Assert.assertFalse(condition) :
Asserts that a condition is false. If it isn’t, an AssertionError is thrown.
Parameters:
condition – the condition to evaluate
g) Assert.assertFalse(condition, message) :
Asserts that a condition is false. If it isn’t, an AssertionError, with the given message, is thrown.
Parameters:
condition – the condition to evaluate
message – the assertion error message
Question: How to set test case priority in TestNG?
Answer:
We use priority attribute to the @Test annotations.
In case priority is not set then the test scripts execute in alphabetical order.
Question: What is Parameterized testing in TestNG?
Answer:
Parameterized tests allow developers to run the same test over and over again using different values.
There are two ways to set these parameters:
With testng.xml
With Data Providers
Let’s see passing parameters with testng.xml:
With this technique, we could define the parameters in the testng.xml file and then reference those parameters in the source files.
Create a java test class, say, ParameterizedTest.java
Add test method parameterizedTest() to your test class. This method takes a string as input parameter
Add the annotation @Parameters(“browser”) to this method. The parameter would be passed a value from testng.xml, which we will see in the next step.
testng.xml
Console Output:
[TestNG] Running:
Open Firefox Driver
===============================================
nextGenerationAutomationLearnTestNG
Total tests run: 1, Failures: 0, Skips: 0
===============================================
TestNG will automatically try to convert the value specified in testng.xml to the type of your parameter. Here are the types supported:
String
int/Integer
boolean/Boolean
byte/Byte
char/Character
double/Double
float/Float
long/Long
short/Short
Question. How can we create a data-driven framework using TestNG?
Answer:
By using @DataProvider annotation, We can create a Data Driven Testing Framework.
Question: What are TestNG Groups?
Answer:
TestNG allows you to perform sophisticated groupings of test methods. Not only can you declare that methods belong to groups, but you can also specify groups that contain other groups. Then TestNG can be invoked and asked to include a certain set of groups (or regular expressions) while excluding another set. This gives you maximum flexibility in how you partition your tests and doesn’t require you to recompile anything if you want to run two different sets of tests back to back.
Groups are specified in your testng.xml file and can be found either under the <test> or <suite> tag. Groups specified in the <suite> tag apply to all the <test> tags underneath.
Script – Test Case 1:
Script – Test Case 2:
testng.xml:
Group of Groups in TestNG Groups:
Groups can also include other groups. These groups are called MetaGroups. For example, you might want to define a group all that includes smokeTest and functionalTest. Let’s modify our testng.xml file as follows:
testng.xml – Group of Groups:
Groups Exclusion:
TestNG allows you to include groups as well as exclude them. You can ignore a group by using the <exclude> tag as shown below:
For example, it is quite usual to have tests that temporarily break because of a recent change, and you don’t have time to fix the breakage yet. However, you do want to have clean runs of your functional tests, so you need to deactivate these tests but keep in mind they will need to be reactivated.
You can also disable tests on an individual basis by using the “enabled” property available on both @Test and @Before/After annotations.
Question: What are TestNG Listeners – Selenium WebDriver
Answer:
Listeners “listen” to the event defined in the selenium script and behave accordingly. The main purpose of using listeners is to create logs. There are many types of listeners such as WebDriver Listeners and TestNG Listeners.
Let’s see how to implement TestNG Listeners.
Step 1: Create a Class “ListenerTestNG” to implement ITestListener methods
In the example we are implementing 3 methods of ITestListerner:
onTestFailure, onTestSkipped, onTestSuccess.
Step 2: Add the listeners annotation (@Listeners) in the Class “ListenerTestNGTestCase”
The complete “ListenerTestNGTestCase” class after adding Listener annotation is mentioned below:
Step 3: Execute the “ListenerTestNGTestCase” class. Methods in class “ListenerTestNG” are called automatically according to the behavior of methods annotated as @Test.
Step 4: Verify the Output in the console. You could find the logs in the console.
If you want to use listeners in multiple classes.
Add the below lines of code in the TestNG.xml file
<listeners>
<listener class-name="listeners.listenerTestNG"/>
</listeners>
Final testng.xml file will be like this:
Execute it by right clicking on testng.xml and run as TestNG Suite
Building better for tomorrow
Comments