Reading:  

Working with TestNG - Starters Guide


Working with tests

TestNG is used to execute test cases. Class is the starting point for executing tests in the TestNG. TestNG object is created by users and can invoke in various ways:

  • On a testng.xml, created entirely from Java.
  • By a testng.xml existing.
  • By setting the test classes.

Users can define which groups to include/exclude, assign parameters, etc.

 The parameters of command line are:

  • -groups
  • -d outputdir: specify the output directory.
  • -target
  • -testclass class_name: several or specifies one class names
  • -sourcedir src1, src2 , source directories of list
  • -testjar jar_name: specifies the jar containing the tests.
  • -listener

Create a Class:

Create a class named as StudentMessage

package testNG;

public class StudentMessage {
	private static int marks;

	//Constructor
	public StudentMessage(int marks) {
		StudentMessage.marks = marks;
	}

	//message
	public static int printMarks() {
		System.out.println("Student marks: " + marks);
		returnmarks;
	}
}

Create Test Case Class:

Create java class named as TestCase1

package testNG;

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

public class TestCase_1 {
	int marks = 78;
	StudentMessage stud = newStudentMessage(marks);

	@Test
	Public void method_1() {
		Assert.assertEquals(marks, StudentMessage.printMarks());
	}
}

Create testing.xml

testing.xml is another way to invoke testNG. This file captures entire testing in XML. This file allows it to describe all test suites and their parameters in a file which can check in code repository.

<?xml version="1.0" encoding="UTF-8"?>
<suite name="test Suite">
<test name="test">
<classes>
<class name="testNG.TestCase1"/>
</classes>
</test>
</suite>

Run the testing.xml file by clicking on run button in eclipse.

You will get this Output

 

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

  1. What is TestNG?
  2. Environment Set-up
  3. Writing Tests
  4. Basic Annotations
  5. Execution Procedure
  6. Executing Tests
  7. Suite Test
  8. Ignore Test
  9. Group Test
  10. Exception Test
  11. Dependency Test
  12. Parametrized Test
  13. JUnit Tests
  14. Test Reports
  15. Running tests without Eclipse
  16. 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!