Writing and executing a TestNG testcase and TestNG suite

Sonal Dwivedi
5 min readJun 15, 2018

--

Selenium does not provide any reporting mechanism, hence testcases are written in TestNG so that we can generate reports based on our selenium scripts.

Pre-requisites to write testcase in TestNG are:

  1. Eclipse installed.
  2. TestNG installed using eclipse marketplace/ TestNG dependency included in pom.xml (if using maven structure)

I. Creating first TestNG class:

  1. Create a project named “DemoTestNG” in eclispe.
  • If its a maven project, add selenium, TestNG jars in pom.xml.
  • If its a simple java project add selenium and TestNG jars in “Libraries” tab under “Build Path”. (Right click on the java project, select “Build Path→ Configure Build Path”→ Select “Add external jars” and add all the selenium jars. And click on “Add Library” and select “TestNG ”)

2. Now after the project is created, create a TestNG class named “Test1”.

How?-> [ Right click on the package created for testcases→ “TestNG”→ “Create TestNG Class”. Source folder and package name will be prepopulated. Set class name as “Test1”. Under annotations, check “@BeforeMethod” and “@AfterMethod” and click Finish.]

Test1- TestNG Class

Newly created “Test1” class should like this with 3 empty methods. One method “f()” by default, “beforeMethod()” and “afterMethod()” as selected during creation of class :

Test1 Basic structure

3. Now lets take a scenario to automate.

Scenario:

a. Launch chrome and direct it to http://newtours.demoaut.com/

b. Login.

c. Close browser.

4. The coding part is as below:

  • Firstly create a WebDriver object globally.

@BeforeMethod-> create chromedriver instance and then direct it to the url.

@Test-> Rename f() as “Login” and write login logic

@AfterMethod-> Close the chrome browser

Test1 Code

Running TestNG class:

Right click on the TestNG class created and select “Run As”→ TestNG Test

Result for TestNG testcase:

Wait for the execution to be completed, and you can see the results in TestNG Result window

Test1 TestNG report

You can also view the detailed report “index.html” under “test-output” folder

Test1- index.html

II. Creating TestNG suite:

  1. Now we know how to execute a single TestNG class. Our next step should what if we have multiple TestNG classes. For this we would require a test suite to execute the same.
  2. We can set test dependency, include or exclude any test, method, class or package and set priority etc in the xml file.
  3. Lets start creating it and name it as “testng.xml”
  • Firstly, create a meaningful folder such as “src/main/resources”. Right click on the project→ New→ Source Folder and give folder name as “src/main/resources”.
  • Right click on the resources folder→ New→ File and give file name as “testng.xml”
  • Write xml code as
<suite thread-count="1" verbose="1" name="Facebook Suite" parallel="tests">

<test name="Login">
<classes>
<class name="com.suite1.LoginTest"/>
</classes>
</test>

<test name="Logout">
<classes>
<class name="com.suite2.LogoutTest"/>
</classes>
</test>
</suite>

i) thread-count: This is used for parallel execution, based on the number script. It will execute in parallel or sequential order.

ii) verbose: It is used to log the execution details in the console. The value should be 1–10. The log details in the console window will get more detailed and clearer as you increase the value of the verbose attribute in the testng.xml configuration file.

iii) name: Name of the suite. Here it is “Gmail Suite”

iv) Parallel: To run scripts parallel, value can be tests/classes/methods/suites. Default value is none

4. Now, here the hierarchical of the tags is very imp. It should be as shown above and nothing else.

5. You can give any name to suite and test but you need to give proper name for your class under classes tag and it should be combination of package and test case name (package_name.testcase_name)

6. If package name is “com.productname.testcases” and testcase name is “HomePageTest”, class name should be “com.productname.testcases.HomePageTest ”

7. Now to run testng.xml suite, right click on it→ Run As→TestNG Suite

testng.xml file
TestNG results

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 tag is run.

@AfterTest: The annotated method will be run after all the test methods belonging to the classes inside the 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.

@Test: The annotated method is a part of a test case

--

--

Sonal Dwivedi

Quality Assurance (Manual+Automation) @TNM, Passionate about Selenium