Reading:  

Working with TestNG - Starters Guide


Running tests from Command-Line

To run test from the command prompt:

We can run test outside Eclipse means from command prompt, with help of the TestNG class. This class is main entry point for executing tests in the TestNG framework. We can create our own object of TestNG and invoke in different ways like:

  • By a testng.xml existing.
  • By a testng.xml created completely from Java.
  • By setting directly the test classes.

To define which groups need to beincluded or exclude, assign parameters, etc. The below commands are used:

  • -testjar jar_name: specifies jar containing the tests.
  • -testclass class_name: specifies one or several class names.
  • -groups sourcedir source1; source2;: separated list of source directories (used when annotations of javadoc are used).
  • -d outputdir: specify the output directory.
  • -target
  • -testrunfactory
  • -listener

The below steps to run test outside Eclipse:

Example: To run test from outside the eclipse

package jUnit;

import java.util.Arrays;

public class ListofColor {
	public String[] colorContainer() {
		String[] container = {
			"Green", "Red", "Yellow"
		};
		System.out.println("The List of colurs: " + Arrays.toString(container));
		return container;
	}

	public String[] addThings() {
		String[] container = {
			"Zen", "Swift", "Maruthi", "Nano"
		};
		System.out.println("Now the container: " + Arrays.toString(container));
		return container;
	}
}

Creation of JUnit test case:

JUnit test case: TestA

package jUnit;

import static org.junit.Assert.*;
import org.junit.Test;

public class TestA {

	ListofColor list = new ListofColor();
	String[] bagA = {
		"Green", "Red", "Yellow"
	};
	String[] bagB = {
		"Zen", "Swift", "Maruthi", "Nano"
	};

	@Test
	public void testContainer() {
		System.out.println("Inside testContainer()");
		assertArrayEquals(bagA, list.colorContainer());
	}

	@Test
	public void testAddThings() {
		System.out.println("Inside testAddThings()");
		assertArrayEquals(bagB, list.addThings());
	}
}

Create testing.xml file. 

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

We need to compile the test using javac command.

C:\>Users\testng\Workspace>javac ListofColor.java TestA.java

Now run testng.xml which will run test case defined in <test> tag.

C:\>Users\testng\Workspace>java -cp "C:\>Users\testng\Workspace" testng testng.xml

The Ouput is shown below

Inside testContainer()
The List of colors: [Green, Red, Yellow]

Inside testAddThings()
Now the container: [Zen, Swift, Maruthi, Nano] 
===============================================

test Suite

Total tests run: 2, Failures: 0, Skips: 0
===============================================

 

 

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!