QA Interview: Top 40: TestNG Interview Questions for QA

Other Useful Links:


1. What is TestNG?

  • NG stands for Next Generation, and TestNG is an automation testing framework.
  • Junit, which makes use of annotations, inspired TestNG.
  • TestNG is aimed to make end-to-end testing simple and addresses the shortcomings of Junit.


2. What are the advantages of TestNG?
  • It is simple to set up because it is an open-source framework.
  • It can generate the report in proper format, including the number of test cases run, pass, failed, skipped.
  • We may develop test cases in a methodical way using TestNG.
  • There is no need for a static main() method as the sequence is regulated by annotations that do not require the method to be static.
  • It gives lots of annotations which are easy to understand and use.
  • Priorities and execution sequences for tests can be established using TestNG.
  • Multiple test cases can be grouped easily.
  • Using the keyword 'invocation count,' the same test case can be run numerous times without loops.
  • Data parameterization is possible using TestNG.
  • You may easily link the TestNG framework with other technologies like Maven, Jenkins, and others.
  • TestNG makes it possible for test methods to rely on one another. It's also called Test Dependency in TestNG.
  • TestNG has a feature that allows you to run several test methods on different browsers to see if your website is cross-browser compatible. 
  • For more efficient testing, TestNG includes a number of assertion methods.
  • Uncaught exceptions are handled by TestNG.

3. How is TestNG different from Selenium WebDriver?
  • Selenium is a web automation tool that allows us to automate online applications.
  • Selenium is combined with TestNG to offer testing capabilities to Test Automation suites. With TestNG, we can include capabilities such as different types of assertions, reporting, parallel execution, and parameterization in our automation suite.
  • In short, Selenium assists with 'automation' and TestNG assists with 'testing' skills while performing Automation Testing.

4. What are some advantages of TestNG over JUnit?

  1. TestNG creates easily understandable HTML test reports.
  2. In contrast to JUnit, test methods can be simply grouped and prioritized.
  3. For developing test cases, TestNG annotations are simple to grasp.
  4. We can run many test cases in parallel with TestNG to reduce execution time.
  5. We can define how one test method is dependent on another using TestNG.
  6. TestNG doesn’t need to extend any class.
  7. @Before/After Suite, @Before/After Test, and @Before/After Group are three new annotations supported by TestNG.


5. What annotations are utilised in TestNG?
  • Precondition annotations: Annotations that are executed before test methods are executed are known as precondition annotations. @BeforeSuite, @BeforeClass, @BeforeTest, and @BeforeMethod are the precondition annotations.
  • Test annotation: The test annotation is supplied before the test method is defined. It is specified as @Test.
  • postcondition annotations: @AfterSuite, @AfterClass, @AfterTest, and @AfterMethod are examples of postcondition annotations.

6. What is the sequence of execution of the annotations in TestNG?

The following is the order in which the annotations are executed:
@BeforeSuite
@BeforeTest
@BeforeClass
@BeforeMethod
@Test
@AfterMethod
@AfterClass
@Aftertest
@AfterSuite

             

7. How to set priorities in TestNG?

In every class, there are multiple tests or methods. If these tests or methods are not prioritized, they are selected alphabetically and executed during execution.

We must set the priority along with the @Test annotation if we want the tests to execute in the order we wish.

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();
}

Because the priority is set to 1, clickelement() will be run first in the previous example. Because the priority of getText() is set to 2, it will be executed after clickelement().

8. What happens if we don't give the TestNG method priority?

  • Then, they will execute in alphabetical order.


9. What will happen if two methods have the same priority?

  • Then, they will execute in alphabetical order.


10. Can we assign negative priority?

  • Yes, priority can be negative, zero or positive.


11. Can one method have multiple priorities?

  • No. One method can have only one priority.


12. Can priority pass through XML files?

  • No. We cannot pass priority through XML files?


13. In TestNG, what is the default priority of test cases?

  • When a test's priority is not provided, it defaults to the numeric value 0.
  • So, if we have one test case with priority 1 and another with no priority, the test with no priority value will run first. (default priority=0)


14. What is a dependency on TestNG?
  • Many methods are dependent on certain methods.
  • For example, if we want to test an application and the login page is down, we won't be able to test the other scenarios. Many tests rely on LoginTest.
  • Hence, we will write as follows:
        @Test(dependsOnMethods=”LoginTest”)
            Public void homePageLaunched(){
                // code 
            }

The given code demonstrates that the LoginTest() method is fully reliant on the homePageLaunched() method.

15. What is @Test Annotation in TestNG?
  • The @Test annotation in TestNG is a simple annotation that designates a method as a test method.
  • The @Test annotation on the test method informs TestNG that it is a "test" that should be run when the user runs the class.

16. What are the attributes supported by @Test annotation in TestNG?
  • alwaysRun, dataProvider, dataProviderClass, dependsOnGroups, dependsOnMethods, enabled, priority, timeOut, and others are some of the significant attributes provided by Test annotation.

17. Which attribute is used to run test method always?
  • The @Test annotation's "alwaysRun" feature is used to always run test methods. It accepts a true or false value. If true is set, this function will always run, even if the method it depends on fails.
  • Syntax: @Test(alwaysRun = true)

18. How can we run a Test method multiple times without using for loop?
  • The test method is made to run n times in a loop by using the invocationCount parameter and setting its value to an integer value.
        @Test(invocationCount = 10)
            public void invocationCountTest(){
               //Test logic
            }

19. How will you define grouping in TestNG?
  • The groups attribute in TestNG can be used to specify grouping as illustrated below:
        @Test(groups=”title”)

20. What is timeOut in TestNG?

We can use "timeout" in TestNG to terminate any method in the script that takes a long time to execute.

@Test(timeout = 3000)
The method will be ended at 3000 milliseconds (3 seconds) in this scenario, and the test case will be reported as "Failed."

        


Next: Top 40: TestNG Interview Questions for QA (21-40)


Greetings, reader! Your input is highly important to us. Please share your thoughts in the comments section below.


Contact:

Email:  piyush23agr@gmail.com

Follow on LinkedIn: Piyush Agrawal - LinkedIn

Follow on YouTube: Piyush Agrawal - Youtube

Happy to Help You !!





1 comment: