Working with TestNG - Starters Guide
Chapters
Preparing TestNG tests
Points to be considered while preparing a test in TestNG:
- Add TestNG annotations in code and put the business logic of test.
- Add the information about test like the class name, the groups etc.in a TestNG.xml file.
- Run TestNG.
Let us see the example:
Create StudentDetails.java
public class StudentDetails { private String student_Name; private int marks_Scored; //to return the name public String getName() { return student_Name; } // parameter student_Name the student_Name to set public void setName(String student_Name) { this.student_Name = student_Name; } //return the marks_Scored public int getMarks() { return marks_ Scored; } // parameter marks_Scored the marks_Scored to set public void setMarks(intmarks_Scored) { this.marks_Scored = marks_Scored; } }
StudentDetails class is used to.
- Set/Get value of student name.
- Set/Get value of studentmarks_Scored.
Create a StudentGradeLogic.java which contains business logic which is used to assign the grade based on marks scored.
public class StudentGradeLogic { // Return grade based on marks scored public StringcalculateGrade(StudentDetails studentDetails) { String grade; marks = studentDetails.getMarks(); if (studentDetails.getMarks() > 96) { grade = ‘A’; } else { grade = ’B’; } return grade; } }
Create a TestNG class TestDetails.java. A TestNG class is a Java class that contains at least one TestNG annotation
import org.TestNG; import org.TestNG.annotations.Test; public class DetailsTest { StudentDetails sd = new StudentDetails(); StudentGradeLogic sgl = new StudentGradeLogic(); @Test public void testStudentGrade() { sd.setName("Nemo"); sd.setMarks(80); String grade = sgl.calculateGrade(sd); Assert.assertEquals(“B”, grade); } }
Execute the TestNG class and go to test-output that is automatically created in current directory.
Open it and load index.html, will see a similar page as shown below:
Description
This tutorial is focused on getting you started on TestNG, the testing framework inspired from JUnit and NUnit. Here is a quick table of contents
- What is TestNG?
- Environment Set-up
- Writing Tests
- Basic Annotations
- Execution Procedure
- Executing Tests
- Suite Test
- Ignore Test
- Group Test
- Exception Test
- Dependency Test
- Parametrized Test
- JUnit Tests
- Test Reports
- Running tests without Eclipse
- Plugin with ANT
Environment
A computer capable of running Java. IntelliJ Community IDE is also required for this course.
Prerequisites
Good knowledge of Java programming language and eclipse is essential.
Audience
Students looking to get started with TestNG
Learning Objectives
This tutorial will get you started with TestNG.
Author: Subject Coach
Added on: 12th Mar 2015
You must be logged in as Student to ask a Question.
None just yet!