Home / Interview /Top 20 TestNG Inter...
Admin
2023-09-07
0 mins read
Similar Blogs
How are the data structures and algorithms useful for SDET?
0 mins read
2023-09-14
How To Use DataProvider in TestNG – Parameterization
0 mins read
2023-09-13
What Are The Skills Required for SDET?
0 mins read
2023-09-13
How can I switch from QA to SDET?
0 mins read
2023-09-14
SDET Demand In Market !!
0 mins read
2023-09-13
Essential Skills Required to Become an SDET
0 mins read
2024-05-02
How can a QA or Test engineer become SDET?
0 mins read
2023-09-12
What is the difference between SDET and TESTER?
0 mins read
2023-09-12
What Is TestNG Annotations-Benefits, Hierarchy
0 mins read
2023-09-13
How to write a Good Test Case in Software Testing
0 mins read
2023-09-14
The following set of frequently asked interview questions have been compiled to help students and professionals preparing for Certified SDET professional courses, SDET Foundation Training, Certified SDET Architect courses. You can see the full set of SDET Interview questions here.
TestNG is a testing framework used for executing the unit tests in Java.
TestNG is an automated open-source testing framework that can be integrated with Selenium and capable of making Selenium tests easier to understand and provide multiple capabilities like an assertion, report generation, parallel test execution, etc.
It is inspired by JUnit. It has all the features of JUnit and has its own new features which make it more powerful. Full form of TestNG is “Testing Next Generation”.
The advantages of TestNG over JUnit are:
Testng.xml is used for configuring the whole test suite. The various uses of TestNG are as follows:
The different annotations of TestNG are:
The sequence of execution of annotations is as follows:
To create xml file in TestNG, follow the following steps:
This will add testing.xml file under Java project folder.
Dependency is used for some of the methods on which many methods are dependent on.
For e.g.: For any application, if the login page does not work, then it should not test the rest of the test scenarios.
In this case, we would be using the LoginTest method on which other tests are dependent. @Test(dependsOnMethods=“LoginTest”) Public void SearchPage() { }
Since Search Page is dependent on Login Test method, so if LoginTest method fails, then Search Page method will not get executed.
Invocation Count is used to execute the same test case multiple times.
For e.g.:
@Test(invocationCount = 10)Public void Login()
{}
In this case, Login() method will execute 10 times.
If we want to terminate any method in the test script which is taking too much time to execute, then we can use “timeOut” attribute in TestNG.
Time is provided in milliseconds(ms)
@Test(timeOut = 2000) Public void Login() { }
In this case, the Login() method will get terminated in 2000 ms (2 seconds) and the test case gets Failed.
The common TestNG assertions are:
•Assert.assertEquals(string actual, string expected) :
If both the strings are equal, then only test case will pass.
•Assert.assertTrue(condition) :
It accepts a Boolean value. The assertion will pass if the condition is True, otherwise, it will get fail.
•Assert.assertFalse(condition) :
It accepts a Boolean value. The assertion will pass if the condition is False, otherwise, it will get fail.
To disable any test case in TestNG, we use the “enabled” attribute.
For e.g.:
@Test(enabled= “false”) Public void LoginTest() { }
In this case, LoginTest() method will get disabled.
The assertion is used to validate the results of test cases.
There are two types of assertion:
•Hard Assert: Hard Assert is used to validate the test result. If hard assert fails, then none of the code execution will take place after the Assert statement.
Assert.assertEquals(actual value, expected value)
•Soft Assert: Soft Assert is also used to validate the test results but if soft assert fails, then also execution of code will be continued for next statements.
To create a soft assert, an object of “softAssert” class is created:
soft Assert sAssert = new softAssert();
sAssert.assertAll();
The priority attribute is used to set the order of execution of test cases.
If priority is not set, then the test scripts are executed in alphabetical order.
For e.g.:
Public class PriorityTestCase { @Test(priority=0) public void testCase1() {system.out.println(“DLA Test Case 1”); } @Test(priority=1) public void testCase2() {system.out.println(“DLA Test Case 2”); } }
In this case, testCase1 will be executed first and then testCase2 will get executed.
We can pass the parameter to test scripts by using @Parameter annotation in test and “parameter” tag in testing.xml.
Sample testing.xml:
<suite name = “dlaTestSuite”> <test name = “dlaTest”> <parameter name = “dlaParamName” value = “dlaParamValue”><classes><class name = “dlaTestFile” /> </classes> </test> </suite>
Sample Test Script:
public class dlaTestFile {@Test@Parameters(“dlaParamName”) public void dlaParameterTest(string paramValue) {System.out.println(sampleParamName);} }
To create a data-driven framework, @DataProvider is used, in which data is passed to the associated test method and multiple iterations of tests run for different data values passed from @DataProvider method.
For e.g.:
@DataProvider(name = “dlaDataProvider”) public Object[] [] dataProviderMethod() {returnnew Object[] [] {{“dev”, “lab”}, {“devlabs”, “alliance”}};} @Test(dataProvider = “dlaDataProvider”) public void dlaTest(string s1, string s2) {system.out.println(s1 + “ “ + s2);}
TestNG provides some kinds of listeners using which some actions can be performed in case an event has triggered. Mostly TestNG listeners are used for configuration of reports and logging.
The most widely used listener in TestNG is ITestListener interface. It contains methods like onTestSuccess, onTestFailure, onTestSkipped etc. To implement this interface, we have to create a listener class of our own. After that, @Listener annotation is used to specify that for a particular test class, our customized listener class should be used.
For e.g.:
@Listeners(PackageName.CustomizedListenerClassName.class) public class dlaTestClass {WebDriver driver = new FirefoxDriver(); @Testpublic void dlaTestMethod() {//test logic}}
@Factory method creates instances of test class and runs all the test methods in that class with a different set of data.
@DataProvider is bound to individual test methods and run specific methods multiple times.
To run the tests in parallel in TestNG, we have to add these two key-value pairs in the suite-
For e.g.:
<suite name = “DLATestSuite” parallel = “methods” thread-count = “5”>
To run the test method even if test methods or groups on which it depends get fail or skipped, we use “alwaysRun” attribute of @Test annotation.
For e.g.:
@Testpublic void parentTest() {Assert.Fail(“Failed Test”);} @Test(dependsOnMethods = {“parentTest”}, alwaysRun = true) Public void DependentTest() {System.out.println(“Test DLA”);}
To handle exception in methods, we can mention the exception in @Test annotation so that the test case does not fail.
For e.g.: If a test 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 this can be handled in TestNG by using “expectedException” attribute:
@Test(expectedException=numberFormatException.class)
After this, the test case will run without failing.
INQUIRY
By tapping continuing, you agree to our Privacy Policy and Terms & Conditions
SkillAhead Solutions
Gurgaon
USA
1603, Capitol Avenue, Suite 413A, 2659, Cheyenne, WY 82001, USA
`Copyright © DevLabs Alliance. All rights Reserved`
|
Refund & Reschedule Policy
Privacy Policy
Terms of Use