1. What is TestNG?
TestNG is a testing framework designed to simplify a broad range of testing needs, from unit testing (testing a class in isolation of the others) to integration testing
(testing entire systems made of several classes, several packages and even several external frameworks, such as application servers).
2. What Are The Essential TestNG Framework Features?
TestNG framework introduces below features that make it more powerful and easier to use:
1. Annotations.
2. Run your tests in arbitrarily big thread pools with various policies available (all methods in their own thread, one thread per test class, etc...).
3. Test that your code is multithread safe.
4. Flexible test configuration.
5. Support for data-driven testing (with @DataProvider).
6. Support for parameters.
7. Powerful execution model (no more TestSuite).
8. Supported by a variety of tools and plug-ins (Eclipse, IDEA, Maven, etc...).
9. Embeds BeanShell for further flexibility.
10.Default JDK functions for runtime and logging (no dependencies).
11. Dependent methods for application server testing.
3. What are TestNG annotations?
@BeforeSuite: The annotated method will be run before all tests in this suite have run.
@AfterSuite: The annotated method will be run after all tests in this suite have run.
@BeforeTest: The annotated method will be run before any test method belonging to the classes inside the <test> tag is run.
@AfterTest: The annotated method 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.
@BeforeClass: The annotated method will be run before the first test method in the current class is invoked.
@AfterClass: The annotated method will be run after all the test methods in the current class have been run.
@BeforeMethod: The annotated method will be run before each test method.
@AfterMethod: The annotated method will be run after each test method.
4. What are different annotations Attributes:
alwaysRun For before methods (beforeSuite, beforeTest, beforeTestClass and beforeTestMethod, but not beforeGroups): If set to true, this configuration method will be run regardless of what groups it belongs to.
For after methods (afterSuite, afterClass, ...): If set to true, this configuration method will be run even if one or more methods invoked previously failed or was skipped.
dependsOnGroups The list of groups this method depends on.
dependsOnMethods The list of methods this method depends on.
enabled Whether methods on this class/method are enabled.
groups The list of groups this class/method belongs to.
inheritGroups If true, this method will belong to groups specified in the @Test annotation at the class level.
5. What is @DataProvider?
@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.
Ex:
@DataProvider(name = "test1")
public Object[][] createData1() {
return new Object[][] {
{ "Cedric", new Integer(36) },
{ "Anne", new Integer(37)},
};
}
//This test method declares that its data should be supplied by the Data Provider
//named "test1"
@Test(dataProvider = "test1")
public void verifyData1(String n1, Integer n2) {
System.out.println(n1 + " " + n2);
}
Output:
Cedric 36
Anne 37
6. What is @Parameters?
@Parameters: Describes how to pass parameters to a @Test method.
If you are using simple values for your parameters, you can specify them in your testng.xml:
@Parameters({ "first-name" })
@Test
public void testSingleString(String firstName) {
System.out.println("Invoked testString " + firstName);
assert "Cedric".equals(firstName);
}
In this code, we specify that the parameter firstName of your Java method should receive the value of the XML parameter called first-name. This XML parameter is defined in testng.xml:
view sourceprint?
<suite name="My suite">
<parameter name="first-name" value="Cedric"/>
<test name="Simple example">
<-- ... -->
7. What is @Test?
It marks a class or a method as part of the test.
Some Attributes for @Test
alwaysRun If set to true, this test method will always be run even if it depends on a method that failed.
dataProvider : The name of the data provider for this test method.
dataProviderClass :The class where to look for the data provider. If not specified, the data provider will be looked on the class of the current test method or one of its base classes. If this attribute is specified, the data provider method needs to be static on the specified class.
description : The description for this method.
enabled : Whether methods on this class/method are enabled.
expectedExceptions : The list of exceptions that a test method is expected to throw. If no exception or a different than one on this list is thrown, this test will be marked a failure.
groups : The list of groups this class/method belongs to.
invocationTimeOut : The maximum number of milliseconds this test should take for the cumulated time of all the invocationcounts. This attribute will be ignored if invocationCount is not specified.
priority : The priority for this test method. Lower priorities will be scheduled first.
successPercentage : The percentage of success expected from this method
singleThreaded : If set to true, all the methods on this test class are guaranteed to run in the same thread, even if the tests are currently being run with parallel="methods".
This attribute can only be used at the class level and it will be ignored if used at the method level. Note: this attribute used to be called sequential (now deprecated).
8. What is dataProviderClass?
Ex:
public class StaticProvider {
@DataProvider(name = "create")
public static Object[][] createData() {
return new Object[][] {
new Object[] { new Integer(42) }
};
}
}
public class MyTest {
@Test(dataProvider = "create", dataProviderClass = StaticProvider.class)
public void test(Integer n) {
// ...
}
}
9. What is dependsOnGroups?
dependsOnGroups The list of groups this method depends on.
@Test(groups = { "init" })
public void serverStartedOk() {}
@Test(groups = { "init" })
public void initEnvironment() {}
@Test(dependsOnGroups = { "init.*" })
public void method1() {}
10. What is dependsOnMethods?
dependsOnMethods The list of methods this method depends on.
@Test
public void serverStartedOk() {}
@Test(dependsOnMethods = { "serverStartedOk" })
public void method1() {}
12. What is invocationCount?
invocationCount The number of times this method should be invoked.
@Test(threadPoolSize = 3, invocationCount = 10, timeOut = 10000)
public void testServer() {}
13. What is timeOut?
timeOut The maximum number of milliseconds this test should take.
@Test(threadPoolSize = 3, invocationCount = 10, timeOut = 10000)
public void testServer() {}
14. What is threadPoolsize?
threadPoolSize The size of the thread pool for this method. The method will be invoked from multiple threads as specified by invocationCount.
@Test(threadPoolSize = 3, invocationCount = 10, timeOut = 10000)
public void testServer() {}
Note: this attribute is ignored if invocationCount is not specified
15. How to exclude a @Test method from a test case with two @Test methods? Is it possible?
Yes, you need to add @Test method in the exclude tag of <testng.xml> file as mentioned below.
<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>
16. How to define the priority of @Test method? Also, mention its usage?
In your Selenium WebDriver project, you can set priority for TestNG @Test annotated methods as shown in the following example.
Java
@Test(priority=0)
Using priority, you can manage @Test method execution sequence as per your requirement. That means @Test method with priority = 0 will run 1st and @Test method with priority = 1 will execute 2nd and so on.
17. List out the benefits of TestNG over Junit?
TestNG framework has following benefits over JUnit.
TestNG annotations are more logical and easier to understand.
TestNG supports three additional setups:
@Before/AfterSuite,
@Before/AfterTest, and
@Before/AfterGroup.
In TestNG, it is possible to run Selenium test cases in parallel.
TestNG supports grouping of test cases which is not possible in JUnit.
TestNG permits you to determine the dependent test cases. Every test case is autonomous to other test cases.
18. How can you disable a test in TestNG?
To disable the test case, you can use the following annotation.
@Test(enabled = false).
19. What are the different ways to produce reports for TestNG results?
TestNG offers following two ways to produce a report.
Listeners: For a listener class, the class has to implement the org.testng./TestListener interface. TestNG notifies these classes at runtime when the test enters into any of the below states.
e.g. Test begins, finishes, 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.
20. How to disabling testng default listner and enable reporting listners:
TestNG is a testing framework designed to simplify a broad range of testing needs, from unit testing (testing a class in isolation of the others) to integration testing
(testing entire systems made of several classes, several packages and even several external frameworks, such as application servers).
2. What Are The Essential TestNG Framework Features?
TestNG framework introduces below features that make it more powerful and easier to use:
1. Annotations.
2. Run your tests in arbitrarily big thread pools with various policies available (all methods in their own thread, one thread per test class, etc...).
3. Test that your code is multithread safe.
4. Flexible test configuration.
5. Support for data-driven testing (with @DataProvider).
6. Support for parameters.
7. Powerful execution model (no more TestSuite).
8. Supported by a variety of tools and plug-ins (Eclipse, IDEA, Maven, etc...).
9. Embeds BeanShell for further flexibility.
10.Default JDK functions for runtime and logging (no dependencies).
11. Dependent methods for application server testing.
3. What are TestNG annotations?
@BeforeSuite: The annotated method will be run before all tests in this suite have run.
@AfterSuite: The annotated method will be run after all tests in this suite have run.
@BeforeTest: The annotated method will be run before any test method belonging to the classes inside the <test> tag is run.
@AfterTest: The annotated method 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.
@BeforeClass: The annotated method will be run before the first test method in the current class is invoked.
@AfterClass: The annotated method will be run after all the test methods in the current class have been run.
@BeforeMethod: The annotated method will be run before each test method.
@AfterMethod: The annotated method will be run after each test method.
4. What are different annotations Attributes:
alwaysRun For before methods (beforeSuite, beforeTest, beforeTestClass and beforeTestMethod, but not beforeGroups): If set to true, this configuration method will be run regardless of what groups it belongs to.
For after methods (afterSuite, afterClass, ...): If set to true, this configuration method will be run even if one or more methods invoked previously failed or was skipped.
dependsOnGroups The list of groups this method depends on.
dependsOnMethods The list of methods this method depends on.
enabled Whether methods on this class/method are enabled.
groups The list of groups this class/method belongs to.
inheritGroups If true, this method will belong to groups specified in the @Test annotation at the class level.
5. What is @DataProvider?
@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.
Ex:
@DataProvider(name = "test1")
public Object[][] createData1() {
return new Object[][] {
{ "Cedric", new Integer(36) },
{ "Anne", new Integer(37)},
};
}
//This test method declares that its data should be supplied by the Data Provider
//named "test1"
@Test(dataProvider = "test1")
public void verifyData1(String n1, Integer n2) {
System.out.println(n1 + " " + n2);
}
Output:
Cedric 36
Anne 37
6. What is @Parameters?
@Parameters: Describes how to pass parameters to a @Test method.
If you are using simple values for your parameters, you can specify them in your testng.xml:
@Parameters({ "first-name" })
@Test
public void testSingleString(String firstName) {
System.out.println("Invoked testString " + firstName);
assert "Cedric".equals(firstName);
}
In this code, we specify that the parameter firstName of your Java method should receive the value of the XML parameter called first-name. This XML parameter is defined in testng.xml:
view sourceprint?
<suite name="My suite">
<parameter name="first-name" value="Cedric"/>
<test name="Simple example">
<-- ... -->
7. What is @Test?
It marks a class or a method as part of the test.
Some Attributes for @Test
alwaysRun If set to true, this test method will always be run even if it depends on a method that failed.
dataProvider : The name of the data provider for this test method.
dataProviderClass :The class where to look for the data provider. If not specified, the data provider will be looked on the class of the current test method or one of its base classes. If this attribute is specified, the data provider method needs to be static on the specified class.
description : The description for this method.
enabled : Whether methods on this class/method are enabled.
expectedExceptions : The list of exceptions that a test method is expected to throw. If no exception or a different than one on this list is thrown, this test will be marked a failure.
groups : The list of groups this class/method belongs to.
invocationTimeOut : The maximum number of milliseconds this test should take for the cumulated time of all the invocationcounts. This attribute will be ignored if invocationCount is not specified.
priority : The priority for this test method. Lower priorities will be scheduled first.
successPercentage : The percentage of success expected from this method
singleThreaded : If set to true, all the methods on this test class are guaranteed to run in the same thread, even if the tests are currently being run with parallel="methods".
This attribute can only be used at the class level and it will be ignored if used at the method level. Note: this attribute used to be called sequential (now deprecated).
8. What is dataProviderClass?
Ex:
public class StaticProvider {
@DataProvider(name = "create")
public static Object[][] createData() {
return new Object[][] {
new Object[] { new Integer(42) }
};
}
}
public class MyTest {
@Test(dataProvider = "create", dataProviderClass = StaticProvider.class)
public void test(Integer n) {
// ...
}
}
9. What is dependsOnGroups?
dependsOnGroups The list of groups this method depends on.
@Test(groups = { "init" })
public void serverStartedOk() {}
@Test(groups = { "init" })
public void initEnvironment() {}
@Test(dependsOnGroups = { "init.*" })
public void method1() {}
10. What is dependsOnMethods?
dependsOnMethods The list of methods this method depends on.
@Test
public void serverStartedOk() {}
@Test(dependsOnMethods = { "serverStartedOk" })
public void method1() {}
12. What is invocationCount?
invocationCount The number of times this method should be invoked.
@Test(threadPoolSize = 3, invocationCount = 10, timeOut = 10000)
public void testServer() {}
13. What is timeOut?
timeOut The maximum number of milliseconds this test should take.
@Test(threadPoolSize = 3, invocationCount = 10, timeOut = 10000)
public void testServer() {}
14. What is threadPoolsize?
threadPoolSize The size of the thread pool for this method. The method will be invoked from multiple threads as specified by invocationCount.
@Test(threadPoolSize = 3, invocationCount = 10, timeOut = 10000)
public void testServer() {}
Note: this attribute is ignored if invocationCount is not specified
15. How to exclude a @Test method from a test case with two @Test methods? Is it possible?
Yes, you need to add @Test method in the exclude tag of <testng.xml> file as mentioned below.
<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>
16. How to define the priority of @Test method? Also, mention its usage?
In your Selenium WebDriver project, you can set priority for TestNG @Test annotated methods as shown in the following example.
Java
@Test(priority=0)
Using priority, you can manage @Test method execution sequence as per your requirement. That means @Test method with priority = 0 will run 1st and @Test method with priority = 1 will execute 2nd and so on.
17. List out the benefits of TestNG over Junit?
TestNG framework has following benefits over JUnit.
TestNG annotations are more logical and easier to understand.
TestNG supports three additional setups:
@Before/AfterSuite,
@Before/AfterTest, and
@Before/AfterGroup.
In TestNG, it is possible to run Selenium test cases in parallel.
TestNG supports grouping of test cases which is not possible in JUnit.
TestNG permits you to determine the dependent test cases. Every test case is autonomous to other test cases.
18. How can you disable a test in TestNG?
To disable the test case, you can use the following annotation.
@Test(enabled = false).
19. What are the different ways to produce reports for TestNG results?
TestNG offers following two ways to produce a report.
Listeners: For a listener class, the class has to implement the org.testng./TestListener interface. TestNG notifies these classes at runtime when the test enters into any of the below states.
e.g. Test begins, finishes, 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.
20. How to disabling testng default listner and enable reporting listners:
Step 1: Add the below Jars Files to your project.
reportng-1.1.4.jar
velocity-dep-1.4.jar
guice-3.0.jar
Step 2: The Most important step, to make sure reportNG
reports, we need to disable the default TestNG Listeners.
It can be done by following the below steps:
1. Right Click on Properties
2. Click on TestNG
3. You will find an option as "Disable default
listeners", check the checbox
4. Click on "Apply" button, it will show as
message as "Project preferences are saved".
5. Now Click on "OK" button.
Step 3: We need to add the below two listeners to testng.xml
file.
<listeners>
<listener
class-name="org.uncommons.reportng.HTMLReporter"/>
<listener
class-name="org.uncommons.reportng.JUnitXMLReporter"/>
</listeners>