Reading:  

Working with TestNG - Starters Guide


Working with Dependencies in test cases

Sometimes,we may need to invoke method in a test case in particular order or we want to share data and state between between methods.

TestNG allows to specify dependencies based on :

  • Using attributes dependsOnMethods in @Test annotations or
  • Using attributes dependsOnGroups in @Test annotations

 

Create a Class:

Create a class named as DependentExample.java

package testNG;
import org.testng.annotations.Test;

public class DependentExample {

	@Test(dependsOnMethods = {
		"openBrowser"
	})
	public void signIn() {
		System.out.println("Inside signIn()");
	}

	@Test
	public void openBrowser() {
		System.out.println("Inside openBrowser()");
	}

	@Test(dependsOnMethods = {
		"signIn"
	})
	public void logOut() {
		System.out.println("Inside logOut()");
	}
}

Create testing.xml file as shown below.

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

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

The Output is 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

  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!