TestNG : Java Testing Next Generation :: TestNG Example
TestNG Example |
SampleTestNGClass.java
package com.javaskool;
import org.testng.Assert;
import org.testng.SkipException;
import org.testng.annotations.Test;
public class SampleTestNGClass {
@Test
public void Login()
{
System.out.println("You are logging here");
//Assert.assertEquals("james", "james");
//throw new SkipException("Skipping this due to some issue");
}
@Test(dependsOnMethods={"Login"})
public void sendMail()
{
System.out.println("Sending Mail");
}
@Test(dependsOnMethods={"Login","sendMail"})
public void ChangeProfile()
{
System.out.println("Changing Profile");
}
}
Here in the above sample code, if you click right and select Run As => TestNG Test. You will find right screen with Green Ribbon if test passes. if test fails, you will get Red Ribbon and you will have yellow in case of Skipped test. And also you will find message on console tab. If you notice, i have added two jar file in build path. that is as below.
- testng-6.8.5.jar [helps to get all annotations and APIs]
- testng-xslt-1.1.1.zip [this file will help you to make report]
For Generating report i have written ant file i.e build.xml, if you run this file one test-output folder will get created and it will have index.html. that you can view in browser and will give you following screen that contains whole report of your test.
Creating Report
build.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project basedir="." default="build" name="02_TestNGDemo">
<property environment="env"/>
<property name="ECLIPSE_HOME" value="../../TestNG/eclipse"/>
<property name="debuglevel" value="source,lines,vars"/>
<property name="target" value="1.7"/>
<property name="source" value="1.7"/>
<path id="02_TestNGDemo.classpath">
<pathelement location="bin"/>
<pathelement location="../../TestNG/testng-6.8.5.jar"/>
</path>
<target name="init">
<mkdir dir="bin"/>
<copy includeemptydirs="false" todir="bin">
<fileset dir="src">
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>
<target name="clean">
<delete dir="bin"/>
</target>
<target depends="clean" name="cleanall"/>
<target depends="build-subprojects,build-project" name="build"/>
<target name="build-subprojects"/>
<target depends="init" name="build-project">
<echo message="${ant.project.name}: ${ant.file}"/>
<javac debug="true" debuglevel="${debuglevel}" destdir="bin" includeantruntime="false" source="${sourcce}" target="${target}">
<src path="src"/>
<classpath refid="02_TestNGDemo.classpath"/>
</javac>
</target>
<target description="Build all projects which reference this project. Useful to propagate changes." name="build-refprojects"/>
<target description="copy Eclipse compiler jars to ant lib directory" name="init-eclipse-compiler">
<copy todir="${ant.library.dir}">
<fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>
</copy>
<unzip dest="${ant.library.dir}">
<patternset includes="jdtCompilerAdapter.jar"/>
<fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>
</unzip>
</target>
<target description="compile project with Eclipse compiler" name="build-eclipse-compiler">
<property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/>
<antcall target="build"/>
</target>
</project>
Output of index.html that is created in test-output folder in project after running build.xml file
Downloads Examples |
Recent Comments