1. TestNg Overview and Configuration in eclipse
1 Abhishek Chakraborty (abhishek.tec@gmail.com)
What is TestNG?
TestNG is testing framework used for the Java
programming language. TestNG is the framework which is
very useful to use with selenium WebDriver software
testing tool. The TestNG framework is introduced to
overcome the limitations of JUnit framework. Most of
the automation users are using this framework because
of its advantages & more supported features. Using this
TestNG framework we will learn how to generate test
reports. Also we can use Ant and xslt report along with
TestNg.
Features of TestNG Framework:
TestNG supports many powerful features & it is easy to
use. Let’s see what all new features are supported in
new testing framework:
Support for parameters.
Supports dependent methods testing.
Test configuration flexible.
Supports powerful execution model.
TestNG has a more elegant way of handling
parameterized tests with the data-provider concept.
For the same test class TestNG support for multiple
instances.
Default JDK functions for runtime and logging (no
dependencies).
Supported different Annotations like @BeforeSuite,
@AfterSuite, @BeforeClass, @AfterClass,
@BeforeTest, @AfterTest, @BeforeMethod,
@AfterMethod, @DataProvider, @Parameters, @Test
etc.
TestNG supports annotations which are very helpful to
guide test case executions. Similarly in JUnit, the
TestNG annotations are always preceded by the ‘@’
symbol. It permit you do parallel execution of test
2. TestNg Overview and Configuration in eclipse
2 Abhishek Chakraborty (abhishek.tec@gmail.com)
cases & we can also skip the test cases effortlessly
while executing test cases.
TestNG is specially designed to cover all types testing
categories like Unit, Functional testing, Integration
testing, End-to-end etc. Using TestNG framework allows
us to generate test reports in both HTML and XML
formats. Using ANT with TestNG, we can generate
primitive Testng reports as well.
Advantages of TestNG over Junit:
1.In TestNG Annotations are easy to understand over
JUnit.
2.In TestNG there is no constraint like you have to
declare @BeforeClass and @AfterClass
3.As method name constraint is present in JUnit, such
method name constraint is not present in TestNG and
you can specify any test method names.
4.In TestNG enable you to grouping of test cases
easily which is not possible in JUnit.
5.TestNG supports following three 3 additional
setUp/tearDown level:
@Before/AfterSuite, @Before/AfterTest and
@Before/AfterGroup.
6.TestNG allows us to define the dependent test
7.Parallel execution of Selenium test cases is
possible in TestNG.
3. TestNg Overview and Configuration in eclipse
3 Abhishek Chakraborty (abhishek.tec@gmail.com)
Steps to install TestNG in Eclipse IDE:
1) Launch Eclipse IDE & under Help menu click on
“Install New Software” option.
4. TestNg Overview and Configuration in eclipse
4 Abhishek Chakraborty (abhishek.tec@gmail.com)
2) Install dialog box will be appeared same as below:
5. TestNg Overview and Configuration in eclipse
5 Abhishek Chakraborty (abhishek.tec@gmail.com)
3) In the install dialog box, enter URL as
“http://beust.com/eclipse” in Work with text box &
press keyboard enter key.
6. TestNg Overview and Configuration in eclipse
6 Abhishek Chakraborty (abhishek.tec@gmail.com)
4) Under Name column TestNG check box will be displayed
same as below:
7. TestNg Overview and Configuration in eclipse
7 Abhishek Chakraborty (abhishek.tec@gmail.com)
5) Select the “TestNG” check box & click on Next
button.
8. TestNg Overview and Configuration in eclipse
8 Abhishek Chakraborty (abhishek.tec@gmail.com)
6) On Install Details screen, make sure that TestNG is
selected & then click on Next button same as mentioned
below screenshot:
9. TestNg Overview and Configuration in eclipse
9 Abhishek Chakraborty (abhishek.tec@gmail.com)
7) On Review Licenses screen read license agreement &
select “I accept…” option shown in below screen shot &
click on Finish button.
10. TestNg Overview and Configuration in eclipse
10 Abhishek Chakraborty (abhishek.tec@gmail.com)
8) Ensure that Installing Software dialog appear. Wait
till installation gets completed:
9) One security warning message will appear during
installation. Ensure that you click on Ok button if
same security dialog is appeared.
10) Once the installation process is completed then
Eclipse ask you to Restart, click on Yes button.
11. TestNg Overview and Configuration in eclipse
11 Abhishek Chakraborty (abhishek.tec@gmail.com)
11) Once the Eclipse is restarted, to make sure whether
TestNG is installed or not. Just under Run menu click
on “Run As” option & check new option is added called
“TestNG Test”same as below screen shot:
12. TestNg Overview and Configuration in eclipse
12 Abhishek Chakraborty (abhishek.tec@gmail.com)
Creating Java Project with TestNG Dependencies
Before we write our first TestNG test, we have to
create a Java project in Eclipse and add our TestNG
test dependencies.
1) Go to File | New | Other. A window with multiple
options will be shown.
2) Select Java Project as shown in the following
screenshot and click on Next.
13. TestNg Overview and Configuration in eclipse
13 Abhishek Chakraborty (abhishek.tec@gmail.com)
3) On the next screen, enter a Project name for a
Java project, let’s say TestNGExamples, as shown in
the following screenshot, and click on Finish:
This will create a new Java project in Eclipse.
14. TestNg Overview and Configuration in eclipse
14 Abhishek Chakraborty (abhishek.tec@gmail.com)
4)Now go to Project | Properties. Select Java Build
Path on the left-hand side on the Properties window
as shown in the following screenshot. This will
display the build path for the newly created
project.
5) Click on the Libraries tab and click on the Add
Library… option.
15. TestNg Overview and Configuration in eclipse
15 Abhishek Chakraborty (abhishek.tec@gmail.com)
6) Select TestNG on the Add Library window as shown
in the following screenshot and click on Next:
16. TestNg Overview and Configuration in eclipse
16 Abhishek Chakraborty (abhishek.tec@gmail.com)
7) Click on Finish on your next window. This will
add the TestNG library to your Eclipse project.
We have successfully created a new Java project in
Eclipse and added a TestNG library to the build
path of the project.
17. TestNg Overview and Configuration in eclipse
17 Abhishek Chakraborty (abhishek.tec@gmail.com)
Creating your first TestNG class
Perform the following steps to create your first
TestNG class:
1) Go to File | New | Other. This will open a new
Add wizard window in Eclipse.
18. TestNg Overview and Configuration in eclipse
18 Abhishek Chakraborty (abhishek.tec@gmail.com)
2) Select TestNG class from the Add wizard window
and click on Next.
19. TestNg Overview and Configuration in eclipse
19 Abhishek Chakraborty (abhishek.tec@gmail.com)
3) On the next window click on the Browse
button and select the Java project where you need
to add your class.
20. TestNg Overview and Configuration in eclipse
20 Abhishek Chakraborty (abhishek.tec@gmail.com)
4) Enter the package name and the test class
name and click on Finish.
21. TestNg Overview and Configuration in eclipse
21 Abhishek Chakraborty (abhishek.tec@gmail.com)
5) This window also gives you an option to select
different annotations while creating a new TestNG
class. If selected, the plugin will generate dummy
methods for these annotations while generating the
class. This will add a new TestNG class to your
project.
package com.howtodoinjava.test;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class MyFirstTest {
@Test
public void f() {
}
@BeforeTest
public void beforeTest() {
}
@AfterTest
public void afterTest() {
}
}
We have successfully added a new TestNG test class
to the newly created Java project in Eclipse. Feel
free to modify the code as needed. Now let’s run
the newly created test class through Eclipse.
22. TestNg Overview and Configuration in eclipse
22 Abhishek Chakraborty (abhishek.tec@gmail.com)
Running TestNG test
Perform the following steps to run tests through
Eclipse:
1) Select the Java project in the Eclipse and go to Run
| Run Configuration.
2) Select TestNG in the given options and click on the
New button to create a new configuration.
23. TestNg Overview and Configuration in eclipse
23 Abhishek Chakraborty (abhishek.tec@gmail.com)
3) Please notice that TestNG plugin provides multiple
options for running your test cases as follows:
Class: Using this option you can provide the class
name along with the package to run only the said
specific test class.
Method: Using this you can run only a specific method
in a test class.
Groups: In case you would like to run specific test
methods belonging to a particular TestNG group, you can
enter those here for executing them.
Package: If you would like to execute all the tests
inside a package, you can specify these in this box.
Suite: In case you have suite files in the form
of testing.xml files, you can select those here for
execution.
24. TestNg Overview and Configuration in eclipse
24 Abhishek Chakraborty (abhishek.tec@gmail.com)
Let’s enter the configuration name as TestNGRunConfig and
select the newly created class under the Class section
and click on Apply.
4) Now if you would like to run the newly created
configuration, just click on Run after clicking on
Apply. This will compile and run the TestNG test class
that we have written. The result of the test execution
is displayed in the Console and Results windows of
Eclipse as shown in the following screenshot.
[TestNG] Running:
C:Userssomelocalpathtestng-customsuite.xml
PASSED: f
===============================================
Default test
Tests run: 1, Failures: 0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================
[TestNG] Time taken by org.testng.reporters.XMLReporter@177b3cd: 23 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 0 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@b8deef: 46 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@10ab323: 12 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@5e176f: 13 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@d1e89e: 142 ms
You can also run the test class by selecting it and
then right-clicking on it, selecting Run as from the
menu, and then choosing TestNG Test.
25. TestNg Overview and Configuration in eclipse
25 Abhishek Chakraborty (abhishek.tec@gmail.com)
26. TestNg Overview and Configuration in eclipse
26 Abhishek Chakraborty (abhishek.tec@gmail.com)
Conclusion:
TestNG is really impressive framework where we can
execute the simple to complex test scripts with
ease. It supports many powerful features, like
annotations, reporting, data-driven testing etc.
You can develop page object model (POM) with Page
Factory framework with the help of Testng easily.
Also You can use ant with TestNg, generate
graphical report with the help of xslt report.
In this TestNG tutorial, we learned about TestNG,
features offered by TestNG, installing the TestNG
plugin into Eclipse and writing and executing a TestNG
test class through Eclipse
Happy Learning