March 7, 2017

TestNG Interview Questions


Explain what is TestNG?
TestNG is an automated open source testing framework.  It is based on JUnit framework but is not a JUnit extension. We can use TestNG with selenium webDriver to configure and run test cases very easily, easy to understand, read and manage test cases, and to generate HTML or XSLT test reports.

Mention what are the TestNG features?
TestNG features include
    • TestNG uses more OO (object-oriented) and Java features
    • It supports testing integrated classes
    • Different Annotations are supported
    • Run-time configuration is flexible
    • Flexible plug-in API
    • Multi-threaded testing support
    • Supports parallel testing, load testing, partial failure, dependent test methods
    • For the same test class TestNG support for multiple instances
    • For logging no dependencies, default JDK functions for logging and run-time
 List out the advantages of TestNG over Junit?
  • Compared to JUnit annotations, TestNG are easy to understand
  • Unlike JUnit, TestNG does not require to declare @BeforeClass and @AfterClass
  • Method name constraint is not there in TestNG
  • TestNG allows you the grouping of test cases easily which is not possible in JUnit
  • TestNG supports following three additional setup: @Before/AfterSuite, @Before/AfterTest and @Before/AfterGroup
  • TestNG does not need to extend any class
  • In TestNG, it is possible to run selenium test cases in parallel
  • Based on group TestNG allows you to execute the test cases
  • TestNG allows you to determine the dependent test cases; each test case is autonomous to other test case
 Explain what are the basic steps required in writing TestNG tests?
The basic steps required in writing TestNG includes
    • Write down the business logic of your test and insert TestNG annotations in your code
    • In a build.xml or testing.xml, add the information about your test
    • Run TestNG
 List out various ways in which TestNG can be invoked?
TestNG can be invoked in different ways like
    • Using Eclipse
    • With ant
    • From the command line
    • Using IntelliJ’s IDEA
Explain what is testing.xml file used for?
 File testing.xml captures your entire testing in XML.  This file makes it easy to define all your test suites and their parameters in one file, which you can verify in your code repository or e-mail to coworkers. It also makes easy to pull out subsets of your tests or split several runtime configurations.
 In TestNG how can you disable a test?
To disable the test case you don’t want, you can use annotations @Test(enabled = false).

Explain what is Time-Out test in TestNG?
The Time-Out test in TestNG is nothing but time allotted to perform unit testing. If the unit test fails to finish in that specific time limit, TestNG will abandon further testing and mark it as a failed.

Explain 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.
 Mention what does the “suite test” does in TestNG?
“Suite Test” is done when you have to run few unit test together, “ Suite Test” bundle this unit test together.  XML file is used to run the suite test.

Explain what is parametric testing?
Parameterized testing allows developers to execute the same test over and over again using different values.  In two different ways TestNG allows you to pass parameters directly to your test methods.
    • With testing.xml
    • With Data Providers
 Explain 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 your entire test in the same
 This approach also enables you to convert your existing JUnit test to TestNG.
 Explain what does @Test(invocationCount=?) and (threadPoolSize=?) indicates?
  • @Test (threadPoolSize=<integer value>): The threadPoolSize attributes tell TestNG to form a thread pool to run the test method through multiple threads. With threadpool, the running time of the test method reduces greatly.
  • @Test(invocationCount==<integer value>): The invocationcount tells how many times TestNG should run this test method
 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.
 Explain what is Group Test in TestNG?
It is a new feature included in TestNG, it allows you to dispatch methods into proper portions and perform grouping of test methods.  With group test, you can not only declare methods that belong to groups, but you can also specify groups that contain other groups.  Groups are determined in your testing.xml file using the group test.

Explain in what ways does TestNG allows you to specify dependencies?
TestNG allows you to specify dependencies in two ways
    • Using attributes dependsOnMethods in @Test annotations
    • Using attributes dependsOnGroups in @Test annotations
 Explain what it means when you have to pass parameters using data-providers in TestNG?
When you have to pass complex parameter or parameters that are to be created from Java, in such instances parameters can be passed using Dataproviders.  The annotation for data provider is @DataProvider.  This annotation has only single string attribute, if the name is not declared; the Data provider’s name automatically defaults to the method’s name.  A data provider yields back an array of objects.

Explain how you can execute tests in TestNG?
The tests in TestNG are executed using TestNG class. For running tests in TestNG framework, class is the main entry point.  Users can make their own TestNG object and invoke it in many different ways like
    • On an already existing testing.xml
    • On a synthetic testing.xml created entirely from Java
    • By directly setting the test classes
 Can you describe major features of TestNG?
TestNG has many major features like
    • Support of @DataProvider annotation to perform data driven testing on software web application
    • We can configure dependent test methods in TestNG, means TestTwo () is dependent to TestOne (). We can also configure that if earlier test method (TestOne ()) fails during execution then dependent software test method (     TestTwo ()) has to be executed or not.
    • Support of configuring test groups like backendtest-group, frontendtest-group etc. and we can tell TestNG to execute only specific group/groups.
    • TestNG is supported by many tools and plug-ins like Eclipse, Maven, IDEA, etc..
    • Generate HTML and XSLT test execution report for software web application etc..
 Describe the similarities and difference between JUnit and TestNG unit testing frameworks?
Similarities:
    • Timeout Test is possible very easily in both the frameworks using @Test(timeOut=<seconds>)
    • We can ignore specific test case execution of software web application from suite in both the frameworks (In Junit @Ignore followed by @Test, In TestNG @Test(enable=false))
    • It is possible to create expected exception test for software web application in both the frameworks using @Test(expected =<Exception Class>.class)
    • 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.
Difference:
    • In TestNG, Parameterized test configuration is very easy while it is very hard to configure Parameterized test in JUnit.
    • TestNG support group test  using @Test(groups={"<group1>", "<group2>",...}) but it is not supported in JUnit.
    • TestNG has a feature to configure dependency test using @Test(dependsOnMethods = { "<@Test Method>" }). Dependency test configuration for software web application is not possible in JUnit.
    • TestNG support @BeforeTest, @AfterTest, @BeforeSuite, @AfterSuite, @BeforeGroups and @AfterGroups which are not supported in JUnit.
    • Test prioritization, parallel testing is possible in TestNG using DataProvider method and textng.xml file. It is not supported by JUnit.
 How to Install TestNG in Eclipse? How do you verify that TestNG Is installed properly In Eclipse?
  1. Open Eclipse and go to Menu Help -> Install New Software. It will open new software installation window.
  2. In new software installation window, Type URL = http://beust.com/eclipse exactly in Work with field and click on Add button.
    1. It will show you option TestNG with check box. Select that check box and click on Next Button.
    2. When you click on Next button, it will check for requirements and dependency first
    3. When it completes requirement and dependency test, click on Next button. On Next screen, it will ask you to accept TestNg terms and license agreement. Accept it and click on Finish button.
    4. TestNG will take few minutes to finish its installation when you click on finish button.
  3. Now you need to verify that TestNG is installed in your eclipse or not.
    1. To verify Go to Eclipse's Menu Window -> Show View -> Others
    2. It will open Show View window. Expand java folder and verify that TestNG is available inside it or not. If it is there means TestNG is installed successfully in eclipse.
What are different annotations supported by TestNG?
Annotation
Description
@BeforeSuite
Will be run only once before all tests in this suite have run.
@AfterSuite
Will be run only once after all tests in this suite have run.
@BeforeClass
Will be run only once before the first test method in the current class is invoked.
@AfterClass
Will be run only once after all the test methods in the current class have run.
@BeforeTest
Will be run before any test method belonging to the classes inside the <test> tag is run.
@AfterTest
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
Will be run before each test method.
@AfterMethod
Will be run after each test method.
@DataProvider
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.
What is the usage of testng.xml file?
In selenium WebDriver software testing tool, We are using testng.xml file to configure our whole test suite In single file. Few of the tasks which we can specify in testng.xml file are as bellow.
    • We can define software testing test suite using set of test cases to run them from single place.
    • Can Include or exclude test methods from software web application's test execution using <include> and <exclude> tags.
    • Can specify a group to Include or exclude.
    • Can pass parameter to use In test case of software web application using <parameter name="xyz" value="abc" /> tag.
    • Can specify group dependencies.
    • Can configure parallel test execution for software web application using <suite name="Test-method Suite" parallel=<item> thread-count="2" >.
      • here item might be classes, methods and tests
      • Please refer to the attached document - 'TestNG Executing Parallel Tests.pdf'
    • Can define listeners.
How to pass parameter with testng.xml file to use It In test case?
We can define parameter in testng.xml file using syntax like <parameter name="<Parameter Name>" value="<value>" />
o     name attribute defines parameter
o    value attribute defines value of that parameter.
Then we can use that parameter in selenium webDriver  using syntax like @Parameters ({"<Parameter Name>"}), Where Parameter Name is case sensitive.
Please refer to the attached document for its usage with example (Question No: 24)
I have a test case with two @Test methods. I want to exclude one @Test method from execution. Can I do it? How?
Yes you need to specify @Test method exclusion In testng.xml file as bellow.
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Test Exclusion Suite">
                <test name="Exclusion Test" >
                                <classes>
                                                <class name="Your Test Class Name">
                                                                <methods>
                                                                                <exclude name="Your Test Method Name To Exclude"/>
                                                                </methods>
                                                </class>     
                                </classes>
                </test>
</suite>
 You need to provide @Test method name In exclude tag to exclude It from execution.
Please refer to the attached document for its usage with example (Question No: 25)
Tell me syntax to skip @Test method from execution.
You can use bellow given syntax Inside @Test method to skip It from test execution.
if(titl.equals("Only Testing: New Test"))
throw new SkipException("Test Check_Checkbox Is Skipped");

0 comments:

Post a Comment