Working with TestNG - Starters Guide
Chapters
Working with Exceptions
To handle the error occurred during run time(exceptions) TestNG provides an parameter expectedExceptions is used along with the @Test annotations.User can test whether a code throws a desired exception or not. Let's see @Test(expectedExceptions)with an example.
Create a Class:
Create a class named as ExceptionExample.java
package testNG; public class ExceptionExample { public void excepTestExample() { System.out.println("Inside excepTestExample()"); int numb1 = 0; int numb2 = 10 / numb1; System.out.println("numb2:" + numb2); } }
Create Test Case Class:
Create a class named as TestException.java
package testNG; import org.testng.annotations.Test; public class TestException { ExceptionExample exp = new ExceptionExample(); @Test(expectedExceptions = ArithmeticException.class) public void test1() { System.out.println("Inside test1()"); exp.excepTestExample(); } }
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.TestException"/>
</classes>
</test>
</suite>
Run the testing.xml file by click on run button in eclipse.
The Output is:
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!