English 中文(简体)
TestNG - Writing Tests
  • 时间:2024-12-22

TestNG - Writing Tests


Previous Page Next Page  

Writing a test in TestNG basically involves the following steps −

    Write the business logic of your test and insert TestNG annotations in your code.

    Add the information about your test (e.g. the class name, the groups you wish to run, etc.) in a testng.xml file or in build.xml.

    Run TestNG.

Here, we will see one complete example of TestNG testing using POJO class, Business logic class and a test xml, which will be run by TestNG.

Create EmployeeDetails.java in /work/testng/src, which is a POJO class.

pubpc class EmployeeDetails {

   private String name;
   private double monthlySalary;
   private int age;

   // @return the name

   pubpc String getName() {
      return name;
   }

   // @param name the name to set

   pubpc void setName(String name) {
      this.name = name;
   }

   // @return the monthlySalary

   pubpc double getMonthlySalary() {
      return monthlySalary;
   }

   // @param monthlySalary the monthlySalary to set

   pubpc void setMonthlySalary(double monthlySalary) {
      this.monthlySalary = monthlySalary;
   }

   // @return the age

   pubpc int getAge() {
      return age;
   }

   // @param age the age to set

   pubpc void setAge(int age) {
      this.age = age;
   }
}

EmployeeDetails class is used to −

    get/set the value of employee s name.

    get/set the value of employee s monthly salary.

    get/set the value of employee s age.

Create an EmpBusinessLogic.java in /work/testng/src, which contains business logic.

pubpc class EmpBusinessLogic {

   // Calculate the yearly salary of employee
   pubpc double calculateYearlySalary(EmployeeDetails employeeDetails) {
      double yearlySalary = 0;
      yearlySalary = employeeDetails.getMonthlySalary() * 12;
      return yearlySalary;
   }

   // Calculate the appraisal amount of employee
   pubpc double calculateAppraisal(EmployeeDetails employeeDetails) {

      double appraisal = 0;

      if(employeeDetails.getMonthlySalary() < 10000) {
         appraisal = 500;

      } else {
         appraisal = 1000;
      }

      return appraisal;
   }
}

EmpBusinessLogic class is used for calculating −

    the yearly salary of employee.

    the appraisal amount of employee.

Now, let s create a TestNG class called TestEmployeeDetails.java in /work/testng/src. A TestNG class is a Java class that contains at least one TestNG annotation. This class contains test cases to be tested. A TestNG test can be configured by @BeforeXXX and @AfterXXX annotations (we will see this in the chapter TestNG - Execution Procedure), which allows to perform some Java logic before and after a certain point.

import org.testng.Assert;
import org.testng.annotations.Test;

pubpc class TestEmployeeDetails {
   EmpBusinessLogic empBusinessLogic = new EmpBusinessLogic();
   EmployeeDetails employee = new EmployeeDetails();

   @Test
   pubpc void testCalculateAppriasal() {

      employee.setName("Rajeev");
      employee.setAge(25);
      employee.setMonthlySalary(8000);

      double appraisal = empBusinessLogic.calculateAppraisal(employee);
      Assert.assertEquals(500, appraisal, 0.0, "500");
   }

   // Test to check yearly salary
   @Test
   pubpc void testCalculateYearlySalary() {

      employee.setName("Rajeev");
      employee.setAge(25);
      employee.setMonthlySalary(8000);

      double salary = empBusinessLogic.calculateYearlySalary(employee);
      Assert.assertEquals(96000, salary, 0.0, "8000");
   }
}

TestEmployeeDetails class is used for testing the methods of EmpBusinessLogic class. It does the following −

    Tests the yearly salary of the employee.

    Tests the appraisal amount of the employee.

Before you can run the tests, you must configure TestNG using a special XML file, conventionally named testng.xml. The syntax for this file is very simple, and its contents are as shown below. Create this file in /work/testng/src.

<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name = "Suite1">
   <test name = "test1">
      <classes>
         <class name = "TestEmployeeDetails"/>
      </classes>
   </test>
</suite>

Details of the above file are as follows −

    A suite is represented by one XML file. It can contain one or more tests and is defined by the <suite> tag.

    Tag <test> represents one test and can contain one or more TestNG classes.

    <class> tag represents a TestNG class. It is a Java class that contains at least one TestNG annotation. It can contain one or more test methods.

Compile the Test case classes using javac.

/work/testng/src$ javac EmployeeDetails.java EmpBusinessLogic.java TestEmployeeDetails.java

Now TestNG with the following command −

/work/testng/src$ java org.testng.TestNG testng.xml

If all has been done correctly, you should see the results of your tests in the console. Furthermore, TestNG creates a very nice HTML report in a folder called test-output that is automatically created in the current directory. If you open it and load index.html, you will see a page similar to the one in the image below −

Writing Tests Advertisements