testng quick guide

Upload: hathanh1

Post on 04-Jun-2018

236 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/14/2019 Testng Quick Guide

    1/35

    http://www.tuto rialspoint.co m/testng /tes tng_quick_guide.htm Copyright tutorialspoint.com

    TESTNG - QUICK GUIDE

    Testing is the proce ss of checking the functionality of the application whether it is working as per re quirementsand to ensure that at deve loper level, unit testing comes into picture . Unit testing is the tes ting of sing le e ntity(class or method). Unit testing is very e ssential to e very software company to g ive a quality product to theircustomers.

    JUnit has driven developers to understand the use fulness of tes ts, e specially of unit tests when compared to anyother tes ting framework. Le verag ing a rather s imple, prag matic, and strict architecture, JUnit has been able to"infect" great number of developers . Fe atures of JUnit can be se en inJunit Features.

    Some of the short coming s of JUnit are:

    Initially designed to e nable unit testing only, now used for all kinds of testing .

    Cannot do de pendency testing .

    Poor config uration control (se tUp/tearDown).

    Intrusive (forces you to extend class es and name your methods a certain way).

    Static prog ramming model (forces you to recompile unnece ssarily).

    The manag ement of different suites of tests in complex projects can be ve ry tricky..

    What is TestNG?

    De finition of TestNG as per its documentation is:

    TestNG is a testing framework inspired from JUnit and NUnit but introducing some new functionalities thatmake it more powerful and easier to use.

    TestNG is an open source automated tes ting framework; where NGof Te stNGmeans Next Generation.TestNG is similar to JUnit (especially JUnit 4), but its not a JUnit extension. Its inspired by JUnit. It is designedto be be tter than JUnit, especially when testing integ rated class es. The cre ator of Te stNG is Cedric Beust.

    TestNG eliminates most of the limitations of the older framework and g ives the developer the ability to writemore flexible and powerful tests. As it heavily borrows from Java Annotations (introduced with JDK 5.0) to definetests, it can also show you how to use this new feature of the Java lang uag e in a re al production environment.

    TestNG Features

    Annotations.

    Tes tNG use s more Java and OO features .

    Supports tes ting integ rated classes (e .g .,by default, no need to cre ate a new test class instance for eve rytest method).

    Separate compile-time test code from run-time config uration/data info.

    Flexible runtime config uration.

    Introduce s test groups. Once you have compiled your tests, you can just ask TestNG to run all the"front-end" tests, or "fast", "slow", "database", etc...

    Supports Dependent test methods , parallel testing , load testing , partial failure.

    Flexible plug -in API.

    Support for multi threaded testing .

    http://www.tutorialspoint.com/testng/testng_quick_guide.htmhttp://www.tutorialspoint.com/junit/junit_overview.htmhttp://www.tutorialspoint.com/testng/testng_quick_guide.htm
  • 8/14/2019 Testng Quick Guide

    2/35

    ENVIRONMENT SET-UP

    TestNG is a framework for Java, so the very first requirement is to have JDK installed in your machine.

    System Requirement

    JDK 1.5 or above.

    Memory no minimum requirement.

    Disk Space no minimum requirement.

    Operating System no minimum requirement.

    Step 1 - verify Java installation in your machine

    Now, open console and exe cute the following javacommand.

    OS Task Command

    Windows Open Command Console c:\> java -version

    Linux Open Command Terminal $ java -version

    Mac Open Terminal machine:~ joseph$ java -version

    Le t's verify the output for all the operating systems:

    OS Output

    Windows java version "1.7.0_25"Java(TM) SE Runtime Environment (build 1.7.0_25-b15)Java HotSpot(TM) 64-Bit Serve r VM (build 23.25-b01, mixed mode)

    Linux java version "1.7.0_25"Java(TM) SE Runtime Environment (build 1.7.0_25-b15)Java HotSpot(TM) 64-Bit Serve r VM (build 23.25-b01, mixed mode)

    Mac java version "1.7.0_25"

    Java(TM) SE Runtime Environment (build 1.7.0_25-b15)Java HotSpot(TM) 64-Bit Serve r VM (build 23.25-b01, mixed mode)

    If you do not have Java installed, install the Java Software De velopment Kit (SDK) fromhttp://www.oracle.com/technetwork/java/javase /downloads/index.html. We are assuming Java 1.7.0_25 asinstalled version for this tutorial.

    Step 2: Set JAVA environment

    Set the JAVA_HOME environment variable to point to the base directory location, where Java is installed onyour machine. For e xample;

    OS Output

    Windows Set the e nvironment variable JAVA_HOME to C:\Prog ram Files\Java\jdk1.7.0_25

    http://www.oracle.com/technetwork/java/javase/downloads/index.html
  • 8/14/2019 Testng Quick Guide

    3/35

    Linux export JAVA_HOME=/usr/local/java-current

    Mac export JAVA_HOME=/Library/Java/Home

    Append Java compiler location to System Path.

    OS Output

    Windows Append the s tring ;C:\Prog ram Files\Java\jdk1.7.0_25\bin to the e nd of the systemvariable, Path.

    Linux export PATH=$PATH:$JAVA_HOME/bin/

    Mac not required

    Verify Java Installation using java -versioncommand explained above.

    Step 3: Download T estNG archive

    Download latest vers ion of Te stNG jar file fromhttp://www.testng .org . At the time of writing this tutorial, Idownloaded testng-6.8.jarand copied it into C:\>T estNG folder.

    OS Archive name

    Windows testng -6.8.j ar

    Linux testng -6.8.jar

    Mac testng -6.8.jar

    Step 4: Set TestNG environment

    Set the TESTNG_HOME environment variable to point to the base directory location, where TestNG jar isstore d on your machine. Assuming , we've stored testng -6.8.j ar in TestNG folder on various Ope rating Systemsas follows.

    OS Output

    Windows Set the e nvironment variable TEST NG_HOME to C:\TEST NG

    Linux export TESTNG_HOME=/usr/local/TESTNG

    Mac export TESTNG_HOME=/Library/TESTNG

    Step 5: Set CLASSPATH variable

    Set the CLASSPATHenvironment variable to point to the TestNG jar location. Assuming , we've storedtestng -6.8.jar in TestNG folder on various Ope rating Systems as follows.

    OS Output

    Windows Set the e nvironment variable CLASSPATH to%CLASSPATH%;%TESTNG_HOME%\testng-6.8.jar;

    http://testng.org/doc/download.html
  • 8/14/2019 Testng Quick Guide

    4/35

    Linux e xport CLASSPAT H=$CLASSPAT H:$T EST NG_HOME/te stng -6.8.jar:

    Mac e xport CLASSPAT H=$CLASSPAT H:$T EST NG_HOME/te stng -6.8.jar:

    Step 6: Test TestNG Setup

    Create a java class file name TestNGSimpleTest inC:\ > TestNG_WORKSPACE

    importorg.testng.annotations.Test;importstaticorg.testng.Assert.assertEquals;

    publicclassTestNGSimpleTest{@TestpublicvoidtestAdd(){ Stringstr ="TestNG is working fine"; assertEquals("TestNG is working fine",str);}}

    TestNG can be invoked in several different ways:

    With a testng .xml file

    With ant

    From the command line

    Le t us invoke using the tes tng .xml file. Create an xml file with name testng .xml inC:\ >TestNG_WORKSPACEto execute T es t case (s).

    Step 7: Verify the Result

    Compile the class using javaccompiler as follows:

    C:\TestNG_WORKSPACE>javac TestNGSimpleTest.java

    Now, invoke the testng .xml to s ee the result.

    C:\TestNG_WORKSPACE>java -cp "C:\TestNG_WORKSPACE" org.testng.TestNG testng.xml

    Verify the output.

    ===============================================Suite1Total tests run: 1, Failures: 0, Skips: 0===============================================

    WRITING TESTSWriting a test in TestNG basically involves following steps:

    Write the business log ic of your test and inse rt TestNG annotations in your code.

  • 8/14/2019 Testng Quick Guide

    5/35

    Add the information about your test (e.g . the class name, the g roups you wish to run, etc...) in a testng .xmlfile or in build.xml..

    Run TestNG.

    Here, we will see one complete example of Tes tNG testing using POJO class, Business log ic c lass and a testxml, which will be run by TestNG.

    Create EmployeeDetails.javainC:\ > TestNG_WORKSPACEwhich is a POJO class .

    publicclassEmployeeDetails{

    privateStringname; privatedoublemonthlySalary; privateintage;

    /** * @return the name */ publicStringgetName(){ returnname; } /**

    * @param name the name to set */ publicvoidsetName(Stringname){ this.name =name; } /** * @return the monthlySalary */ publicdoublegetMonthlySalary(){ returnmonthlySalary; } /** * @param monthlySalary the monthlySalary to set */

    publicvoidsetMonthlySalary(doublemonthlySalary){ this.monthlySalary =monthlySalary; } /** * @return the age */ publicintgetAge(){ returnage; } /** * @param age the age to set */ publicvoidsetAge(intage){ this.age =age;

    }}

    EmployeeDetailsclass is use d to:

    get/set the value of employee's name.

    g et/se t the value of employe e's monthly salary.

    g et/set the value of employee's ag e.

    Create a EmpBusinessLogic.javainC:\ > TestNG_WORKSPACEwhich contains business logic.

    publicclassEmpBusinessLogic{ // Calculate the yearly salary of employee publicdoublecalculateYearlySalary(EmployeeDetailsemployeeDetails){ doubleyearlySalary=0; yearlySalary =employeeDetails.getMonthlySalary()*12; returnyearlySalary;

  • 8/14/2019 Testng Quick Guide

    6/35

    }

    // Calculate the appraisal amount of employee publicdoublecalculateAppraisal(EmployeeDetailsemployeeDetails){ doubleappraisal=0; if(employeeDetails.getMonthlySalary()

  • 8/14/2019 Testng Quick Guide

    7/35

    Details of the above file are as be low:

    A suite is re presented by one XML file. It can contain one or more tests and is defined by the tag .

    Tag represe nts one test and can contain one or more Tes tNG classes.

    tag represe nts a TestNG class is a Java class that contains at least one TestNG annotation. It cancontain one or more test methods.

    Compile the T est case classe s using javac.

    C:\TestNG_WORKSPACE>javac EmployeeDetails.java EmpBusinessLogic.java TestEmployeeDetails.java

    Now TestNG with the following command:

    C:\TestNG_WORKSPACE>java -cp "C:\TestNG_WORKSPACE" org.testng.TestNG testng.xml

    If all has been done correctly, you should see the results of your tests in the console. Furthermore , TestNGcreates a very nice HTML report in a folder called test-outputthat is automatically created in the currentdirectory. If you open it and load index.html, you will see a pag e similar to the one in the imag e below:

    BASIC ANNOTATIONS

    The traditional way to indicate tes t methods in JUnit 3 is by prefixing their name with tes t. This is a very effectivemethod for tag g ing ce rtain methods in a class as having a special meaning , but the naming doe snt scale very well(what if we want to add more tags for different frameworks?) and is rather inflexible (what if we want to passadditional parameters to the testing framework?).

    Annotations were formally added to the Java lang uag e in JDK 5 and Te stNG made the choice to use annotationsto annotate test classes.

    Here is the list of annotations that TestNG supports:

    Annotation Desc ription

    @BeforeSuite The annotated method will be run only once before all tests in this suite have run.

    @AfterSuite The annotated method will be run only once after all tests in this suite have run.

    @BeforeClass The annotated method will be run only once before the first test method in the curre ntclass is invoked.

    @AfterClass The annotated method will be run only once after all the tes t methods in the curre ntclass have been run.

  • 8/14/2019 Testng Quick Guide

    8/35

    @BeforeTest The annotated method will be run before any tes t method be long ing to the classesinside the tag is run.

    @AfterTest The annotated method will be run after all the tes t methods belong ing to the classesinside the tag have run.

    @BeforeGroups The list of g roups that this config uration method will run before . T his method isg uarantee d to run shortly before the first tes t method that belongs to any of thesegroups is invoked.

    @AfterGroups The list of groups that this config uration method will run after. T his method isg uarantee d to run shortly after the last test method that belongs to any of these g roupsis invoked.

    @BeforeMethod The annotated method will be run before each test method.

    @AfterMethod The annotated method will be run after e ach tes t method.

    @DataProvider Marks a method as supplying data for a test method. The annotated method mustreturn an Object[][] where each Object[] can be assigned the parameter list of thetest method. The @Test method that wants to rece ive data from this DataProviderneeds to use a dataProvider name equals to the name of this annotation.

    @Factory Marks a method as a factory that returns objects that will be used by TestNG as T estclasses. The method must return Object[].

    @Listeners Defines listeners on a test class.

    @Parameters Describes how to pass parameters to a @T es t method.

    @Test Marks a class or a method as part of the test.

    Benefits of using annotations

    Following are some of the benefits of using annotations:

    TestNG identifies the methods it is interested in by looking up annotations. Hence method names are notrestricted to any pattern or format.

    We can pass additional parameters to annotations.

    Annotations are strong ly typed, so the compiler will flag any mistakes right away.

    Tes t classes no long er nee d to extend anything (such as T estCase, for JUnit 3).

    EXCECUTION PROCEDUREThis tutorial explains the execution procedure of methods in TestNG which means that which method is calledfirst and which one after that. Here is the execution procedure of the TestNG test API methods with the example.

    Create a java class file name T estng Annotation.java in C:\ > TestNG_WORKSPACEto test annotation.

    importorg.testng.annotations.Test;importorg.testng.annotations.BeforeMethod;importorg.testng.annotations.AfterMethod;importorg.testng.annotations.BeforeClass;importorg.testng.annotations.AfterClass;importorg.testng.annotations.BeforeTest;importorg.testng.annotations.AfterTest;importorg.testng.annotations.BeforeSuite;importorg.testng.annotations.AfterSuite;

    publicclassTestngAnnotation{// test case 1

  • 8/14/2019 Testng Quick Guide

    9/35

    @TestpublicvoidtestCase1(){ System.out.println("in test case 1");}

    // test case 2@TestpublicvoidtestCase2(){ System.out.println("in test case 2");}

    @BeforeMethodpublicvoidbeforeMethod(){ System.out.println("in beforeMethod");}

    @AfterMethodpublicvoidafterMethod(){ System.out.println("in afterMethod");}

    @BeforeClasspublicvoidbeforeClass(){ System.out.println("in beforeClass");}

    @AfterClasspublicvoidafterClass(){ System.out.println("in afterClass");}

    @BeforeTestpublicvoidbeforeTest(){ System.out.println("in beforeTest");}

    @AfterTestpublicvoidafterTest(){

    System.out.println("in afterTest");}

    @BeforeSuitepublicvoidbeforeSuite(){ System.out.println("in beforeSuite");}

    @AfterSuitepublicvoidafterSuite(){ System.out.println("in afterSuite");}

    }

    Next, let's cre ate the file testng .xmlinC:\ > TestNG_WORKSPACEto execute annotations.

    Compile the Test case class using javac.

    C:\TestNG_WORKSPACE>javac TestngAnnotation.java

    Now, run the tes tng .xml, which will run tes t case defined in provided Test Case class.

  • 8/14/2019 Testng Quick Guide

    10/35

    C:\TestNG_WORKSPACE>java org.testng.TestNG testng.xml

    Verify the output.

    in beforeSuitein beforeTestin beforeClassin beforeMethodin test case 1

    in afterMethodin beforeMethodin test case 2in afterMethodin afterClassin afterTestin afterSuite

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

    See the above output and this is how the TestNG execution procedure is:

    First of all before Suite() method is executed only once .

    Lastly, the afterSuite() method executes only once.

    Even the methods beforeTest(), be foreClass(), afterClass() and afterTest() methods are executed onlyonce.

    beforeMethod() method executes for each test case but before e xecuting the test case.

    afterMethod() method executes for e ach test case but after the execution of test case.

    In between beforeMethod() and afterMethod() each test case executes.

    EXECUTING TESTS

    The test case s are e xecuted using TestNGclass. T his class is the main entry point for running tes ts in theTestNG framework. Users can create their own TestNG obje ct and invoke it in many differe nt ways:

    On an existing testng .xml

    On a synthetic testng .xml, created e ntire ly from Java

    By directly setting the test classes.

    You can also define which g roups to include or exclude, ass ig n parameters, etc. T he command line parametersare:

    -d outputdir: spe cify the output directory

    -testclass class_name: specifies one or seve ral class names

    -tes tjar jar_name: spe cifies the jar containing the tests

    -sourcedir src1;src2: ; separated list of source directories (used only when javadoc annotations are used)

    -target

    -groups

    -testrunfactory

    -listener

  • 8/14/2019 Testng Quick Guide

    11/35

    We will cre ate the T estNG obje ct an existing testng .xml in our example be low.

    Create a Class

    Cre ate a java class to be tested say Messag eUtil.java inC:\ > TestNG_WORKSPACE

    /** This class prints the given message on console.*/

    publicclassMessageUtil{

    privateStringmessage;

    //Constructor //@param message to be printed publicMessageUtil(Stringmessage){ this.message =message; }

    // prints the message publicStringprintMessage(){ System.out.println(message); returnmessage;

    }}

    Create Test Case Class

    Cre ate a java test class say SampleT es t.java.

    Add a test method testPrintMessag e() to your test class .

    Add an Annotation @Test to method testPrintMessag e().

    Implement the test condition and check the condition using assertEquals API of TestNG.

    Create a java class file name SampleTest.java inC:\ > TestNG_WORKSPACE

    importorg.testng.Assert;importorg.testng.annotations.Test;

    publicclassSampleTest{

    Stringmessage ="Hello World"; MessageUtilmessageUtil =newMessageUtil(message);

    @Test publicvoidtestPrintMessage(){ Assert.assertEquals(message,messageUtil.printMessage()); }}

    Create testng .xml

    Next, let's create testng .xml file inC:\ > TestNG_WORKSPACEto execute Tes t case(s). This file capturesyour entire testing in XML. This file makes it easy to des cribe all your test suites and their parameters in one file,which you can check in your code repository or email to coworkers. It also makes it easy to extract subsets ofyour tests or split several runtime config urations (e.g ., testng -database .xml would run only tests that exerciseyour database).

  • 8/14/2019 Testng Quick Guide

    12/35

    Compile the Test case using javac.

    C:\TestNG_WORKSPACE>javac MessageUtil.java SampleTest.java

    Now, run the tes tng .xml, which will run tes t case defined in tag.

    C:\TestNG_WORKSPACE>java -cp "C:\TestNG_WORKSPACE" org.testng.TestNG testng.xml

    Verify the output.

    Hello World

    ===============================================Sample test SuiteTotal tests run: 1, Failures: 0, Skips: 0===============================================

    SUITE TESTS

    A Test suite is a collection of test case s that are intended to test a behavior or set of behaviors of softwareprog ram. In TestNG, we cannot define a suite in testing source code, but it is represe nted by one XML file assuite is the feature of exe cution. This also allows flexible config uration of the teststo be run. A suite can containone or more tests and is defined by the tag .

    is a root tag of your tes tng .xml. It describes a tes t suite, which in turn is made of several sections.

    Table be low lists all the leg al attributes acce pts.

    Attribute Desc ription

    name The name of this suite. It is a mandatoryattribute.

    verbose The leve l or verbos ity for this run.

    parallel Whether TestNG should run different threads to run this suite.

    thread-count The number of threads to use ,if parallel mode is enabled (ig nored other-wise ).

    annotations The type of annotations you are using in your tests.

    time-out The default timeout that will be used on all the test methods found in this test.

    In this chapter we will show you an example having two T est1 & Te st2 test class es to run together using TestSuite.

    Create a Class

    Cre ate a java class to be tested say Messag eUtil.java inC:\ > JUNIT_WORKSP ACE

    /** This class prints the given message on console.*/publicclassMessageUtil{ privateStringmessage;

    // Constructor // @param message to be printed publicMessageUtil(Stringmessage){ this.message =message; }

  • 8/14/2019 Testng Quick Guide

    13/35

    // prints the message publicStringprintMessage(){ System.out.println(message); returnmessage; }

    // add "Hi!" to the message publicStringsalutationMessage(){ message ="Hi!"+message; System.out.println(message); returnmessage; }}

    Create Test Case Classes

    Create a java class file name Test1.java in C:\ > TestNG_WORKSPACE

    importorg.testng.Assert;importorg.testng.annotations.Test;

    publicclassTest1{ Stringmessage ="Manisha";

    MessageUtilmessageUtil =newMessageUtil(message);

    @Test publicvoidtestPrintMessage(){ System.out.println("Inside testPrintMessage()");Assert.assertEquals(message,messageUtil.printMessage()); }}

    Create a java class file name Test2.java in C:\ > TestNG_WORKSPACE

    importorg.testng.Assert;importorg.testng.annotations.Test;

    publicclassTest2{ Stringmessage ="Manisha";

    MessageUtilmessageUtil =newMessageUtil(message);

    @Test publicvoidtestSalutationMessage(){ System.out.println("Inside testSalutationMessage()"); message ="Hi!"+"Manisha"; Assert.assertEquals(message,messageUtil.salutationMessage()); }}

    Now, let's write the tes tng .xml inC:\ > TestNG_WORKSPACEwhich would contain the tag asfollows:

    Suite1includes exampletest1and exampletest2.

  • 8/14/2019 Testng Quick Guide

    14/35

    Compile all java classes using javac.

    C:\TestNG_WORKSPACE>javac MessageUtil.java Test1.java Test2.java

    Now, run the tes tng .xml, which will run tes t case defined in provided Test Case class.

    C:\TestNG_WORKSPACE>java -cp "C:\TestNG_WORKSPACE" org.testng.TestNG testng.xml

    Verify the output.

    Inside testPrintMessage()ManishaInside testSalutationMessage()Hi!Manisha

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

    IGNORE TESTS

    Sometimes, it happens that our code is not re ady and test case written to tes t that method/code will fail if run. Insuch cases annotation @Test(enabled = false)helps to disable this test case .

    A test method annotated with@Test(enabled = false), then the test case that is not ready to test is bypasse d.

    Now, let's see @Test(enabled = false) in action.

    Create a Class

    Cre ate a java class to be tested say Messag eUtil.java inC:\ > TestNG_WORKSPACE

    /*

    * This class prints the given message on console.*/publicclassMessageUtil{

    privateStringmessage;

    //Constructor //@param message to be printed publicMessageUtil(Stringmessage){ this.message =message;

    }

    // prints the message publicStringprintMessage(){ System.out.println(message); returnmessage; }

    // add "Hi!" to the message publicStringsalutationMessage(){ message ="Hi!"+message; System.out.println(message); returnmessage; }}

    Create Test Case Class

    Create a java tes t class say IgnoreT est.java.

    Add test methods testPrintMessag e(), testSalutationMessag e() to your test class.

    Add an Annotation @Test(enabled = false) to method testPrintMessag e().

  • 8/14/2019 Testng Quick Guide

    15/35

    Cre ate a java class file name IgnoreTes t.java inC:\ > TestNG_WORKSPACE

    importorg.testng.Assert;importorg.testng.annotations.Test;

    publicclassIgnoreTest{ Stringmessage ="Manisha"; MessageUtilmessageUtil =newMessageUtil(message);

    @Test(enabled =false) publicvoidtestPrintMessage(){ System.out.println("Inside testPrintMessage()"); message ="Manisha";Assert.assertEquals(message,messageUtil.printMessage()); }

    @Test publicvoidtestSalutationMessage(){ System.out.println("Inside testSalutationMessage()");message ="Hi!"+"Manisha";Assert.assertEquals(message,messageUtil.salutationMessage()); }}

    Create testng .xml

    Create a testng .xml C:\ > TestNG_WORKSPACEto execute T est case (s)

    Compile the MessageUtil, Test case classes using javac.

    C:\TestNG_WORKSPACE>javac MessageUtil.java IgnoreTest.java

    Now, run the testng .xml, which will not run testPrintMessag e() test case defined in provided T est Case class.

    C:\TestNG_WORKSPACE>java -cp "C:\TestNG_WORKSPACE" org.testng.TestNG testng.xml

    Verify the output. testPrintMessag e() test case is not tested.

    Inside testSalutationMessage()Hi!Manisha

    ===============================================Suite1Total tests run: 1, Failures: 0, Skips: 0===============================================

    GROUP TEST

    The g roup test is a new innovative feature in TestNG, it doesnt exist in JUnit framework, it permits you dispatchmethods into prope r portions and pre form sophisticated g rouping s of tes t methods. Not only can you declarethose methods that belong to g roups, but you can also specify g roups that contain other g roups. T hen, T estNGcan be invoked and asked to include a ce rtain se t of groups (or reg ular expressions) while excluding another set.This g ives you maximum flexibility in how you partition your tests and doe sn't require you to recompile anything if

    you want to run two different se ts of tests back to back.

  • 8/14/2019 Testng Quick Guide

    16/35

    Groups are spe cified in your testng .xml file using the tag . It can be found e ither under the or tag . Groups specified in the tag apply to all the tag s underneath.

    Now, let's see an example of how to g roup test.

    Create a Class

    Cre ate a java class to be tested say Messag eUtil.java inC:\ > TestNG_WORKSPACE

    /** This class prints the given message on console.*/publicclassMessageUtil{ privateStringmessage;

    // Constructor // @param message to be printed publicMessageUtil(Stringmessage){ this.message =message; }

    // prints the message publicStringprintMessage(){

    System.out.println(message);returnmessage; }

    // add "tutorialspoint" to the message publicStringsalutationMessage(){ message ="tutorialspoint"+message;System.out.println(message);returnmessage; }

    // add "www." to the message publicStringexitMessage(){message ="www."+message;

    System.out.println(message);returnmessage; }}

    Create Test Case Class

    Cre ate a java tes t class say GroupTe stExample.java.

    Add test methods testPrintMessag e(), testSalutationMessag e() to your test class.

    Group the test method in two categ ories say:

    Check-in tests (checkintest): These tests should be run before you submit new code . They shouldtypically be fast and just make sure no basic functionality is broken.

    Functional tests (functest): These tests should cover all the functionalities of your software and berun at least once a day, althoug h ideally you would want to run them continuously.

    Create the java class file name GroupTestExample.java inC:\ > TestNG_WORKSPACE

    importorg.testng.Assert;importorg.testng.annotations.Test;

    publicclassGroupTestExample{

    Stringmessage =".com"; MessageUtilmessageUtil =newMessageUtil(message);

    @Test(groups ={"functest","checkintest"}) publicvoidtestPrintMessage(){ System.out.println("Inside testPrintMessage()");message =".com";

  • 8/14/2019 Testng Quick Guide

    17/35

    Assert.assertEquals(message,messageUtil.printMessage()); }

    @Test(groups ={"checkintest"}) publicvoidtestSalutationMessage(){ System.out.println("Inside testSalutationMessage()");message ="tutorialspoint"+".com";Assert.assertEquals(message,messageUtil.salutationMessage()); }

    @Test(groups ={"functest"})

    publicvoidtestingExitMessage(){ System.out.println("Inside testExitMessage()"); message ="www."+"tutorialspoint"+".com";Assert.assertEquals(message,messageUtil.exitMessage()); }}

    Create testng .xml

    Create a testng .xml C:\ > TestNG_WORKSPACEto execute Test case (s). Here, we would be e xecutingonly those tests which belong to the g roupfunctest.

    Compile the MessageUtil, Test case classes using javac.

    C:\TestNG_WORKSPACE>javac MessageUtil.java GroupTestExample.java

    Now, run the tes tng .xml, which will run only the method testPrintMessag e() as it belong s to the g roupfunctest.

    C:\TestNG_WORKSPACE>java -cp "C:\TestNG_WORKSPACE" org.testng.TestNG testng.xml

    Verify the output. Only the method testPrintMessag e() is executed.

    Inside testPrintMessage().comInside testExitMessage()www..com

    ===============================================Suite1Total tests run: 2, Failures: 1, Skips: 0===============================================

    Groups of groups

    Groups can also include other g roups. Thes e g roups are calledMetaGroups. For example, you might want todefine a g roup allthat includes checkintestandfunctest. Let's modify our testng .xml file as below:

  • 8/14/2019 Testng Quick Guide

    18/35

    Executing the above testng .xml will execute all the three tes ts and will g ive you the below result:

    Inside testPrintMessage().comInside testSalutationMessage()tutorialspoint.comInside testExitMessage()www.tutorialspoint.com

    ===============================================Suite1Total tests run: 3, Failures: 0, Skips: 0===============================================

    Exclusion g roups

    You can ig nore a group by using the tag as shown below:

    EXCEPTION TEST

    TestNG provides a option of tracing the Exception handling of code. You can test whether a code throws desiredexce ption or not. The expectedExceptions parameter is used along with @Test annotation. Now let's see@Test(expectedExceptions)in action.

    Create a Class

    Cre ate a java class to be tested say Messag eUtil.java inC:\ > TestNG_WORKSPACE .

    Add a error condition inside printMessag e() method.

    /** This class prints the given message on console.*/publicclassMessageUtil{

  • 8/14/2019 Testng Quick Guide

    19/35

    privateStringmessage;

    //Constructor //@param message to be printed publicMessageUtil(Stringmessage){ this.message =message;

    }

    // prints the message publicvoidprintMessage(){

    System.out.println(message); inta =0; intb =1/a; }

    // add "Hi!" to the message publicStringsalutationMessage(){ message ="Hi!"+message; System.out.println(message); returnmessage; }}

    Create Test Case Class

    Create a java test class say ExpectedExceptionTest.java.

    Add expected exception ArithmeticExce ption to testPrintMessag e() test case.

    Cre ate a java class file name ExpectedExceptionTes t.java inC:\ > TestNG_WORKSPACE

    importorg.testng.Assert;importorg.testng.annotations.Test;

    publicclassExpectedExceptionTest{ Stringmessage ="Manisha";

    MessageUtilmessageUtil =newMessageUtil(message);

    @Test(expectedExceptions =ArithmeticException.class) publicvoidtestPrintMessage(){

    System.out.println("Inside testPrintMessage()");messageUtil.printMessage();

    } @Test publicvoidtestSalutationMessage(){ System.out.println("Inside testSalutationMessage()"); message ="Hi!"+"Manisha"; Assert.assertEquals(message,messageUtil.salutationMessage()); }}

    Create Test Runner

    Create a testng .xml inC:\ > TestNG_WORKSPACEto execute T est case (s).

    Compile the Mess ageUtil, Te st case classe s using javac

    C:\TestNG_WORKSPACE>javac MessageUtil.java TestJunit.java

  • 8/14/2019 Testng Quick Guide

    20/35

    Now, run the Test Runner which will run test cases defined in provided T est Case class .

    C:\TestNG_WORKSPACE>java -cp "C:\TestNG_WORKSPACE" org.testng.TestNG testng.xml

    Verify the output. testPrintMessag e() test case will be passed.

    Inside testPrintMessage()Manisha

    Inside testSalutationMessage()Hi!Manisha

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

    DEPENDENCY TEST

    Sometimes, you may need to invoke methods in a Test case in a particular order or you want to share some dataand state between methods. T his kind of dependency is supported by TestNG as it supports the declaration ofexplicit dependencies between test methods.

    TestNG allows you to specify dependencies either with:

    Using attributes dependsOnMethodsin @Test annotations OR

    Using attributes dependsOnGroupsin @Test annotations.

    Example using attribute dependsOnMethods

    Create a Class

    Cre ate a java class to be tested say Messag eUtil.java inC:\ > TestNG_WORKSPACE

    publicclassMessageUtil{ privateStringmessage;

    // Constructor // @param message to be printed publicMessageUtil(Stringmessage){ this.message =message; }

    // prints the message publicStringprintMessage(){ System.out.println(message);

    returnmessage; }

    // add "Hi!" to the message publicStringsalutationMessage(){message ="Hi!"+message;System.out.println(message);returnmessage; }}

    Create Test Case Class

    Cre ate a java test class say DependencyTestUsing Annotation.java.

    Add test methods testPrintMessag e(),testSalutationMessag e() and initEnvironmentTest() to your testclass.

    Add attribute dependsOnMethods = { "initEnvironmentTest" }to the @Test annotation of

  • 8/14/2019 Testng Quick Guide

    21/35

    testSalutationMessage()method.

    Create the java class file name De pendencyT estUsing Annotation.java inC:\ > TestNG_WORKSPACE

    importorg.testng.Assert;importorg.testng.annotations.Test;

    publicclassDependencyTestUsingAnnotation{ Stringmessage ="Manisha"; MessageUtilmessageUtil =newMessageUtil(message);

    @Test publicvoidtestPrintMessage(){System.out.println("Inside testPrintMessage()");message ="Manisha";Assert.assertEquals(message,messageUtil.printMessage()); }

    @Test(dependsOnMethods ={"initEnvironmentTest"}) publicvoidtestSalutationMessage(){ System.out.println("Inside testSalutationMessage()");message ="Hi!"+"Manisha";Assert.assertEquals(message,messageUtil.salutationMessage()); }

    @Test publicvoidinitEnvironmentTest(){System.out.println("This is initEnvironmentTest"); }}

    Create testng .xml

    Create a testng .xml C:\ > TestNG_WORKSPACEto execute T est case (s).

    Compile the MessageUtil, Test case classes using javac.

    C:\TestNG_WORKSPACE>javac MessageUtil.java DependencyTestUsingAnnotation.java

    Now, run the testng .xml, which will run the testSalutationMessage()method only after the execution ofinitEnvironmentTest()method.

    C:\TestNG_WORKSPACE>java -cp "C:\TestNG_WORKSPACE" org.testng.TestNG testng.xml

    Verify the output.

    This is initEnvironmentTestInside testPrintMessage()ManishaInside testSalutationMessage()Hi!Manisha

    ===============================================Suite1Total tests run: 3, Failures: 0, Skips: 0===============================================

  • 8/14/2019 Testng Quick Guide

    22/35

    Example using attribute dependsOnGroups

    You can also have methods that depend on entire g roups. Le t's se e an example be low:

    Create a Class

    Cre ate a java class to be tested say Messag eUtil.java inC:\ > TestNG_WORKSPACE

    publicclassMessageUtil{

    privateStringmessage;

    // Constructor // @param message to be printed publicMessageUtil(Stringmessage){ this.message =message; }

    // prints the message publicStringprintMessage(){ System.out.println(message);returnmessage; }

    // add "Hi!" to the message publicStringsalutationMessage(){message ="Hi!"+message;System.out.println(message);returnmessage; }}

    Create Test Case Class

    Cre ate a java test class say DependencyTestUsing Annotation.java.

    Add test methods testPrintMessag e(),testSalutationMessag e() and initEnvironmentTest() to your testclass and them to the g roup "init".

    Add attribute dependsOnMethods = { "init.*" }to the @T est annotation of testSalutationMessage()method.

    Create the java class file name De pendencyT estUsing Annotation.java inC:\ > TestNG_WORKSPACE

    importorg.testng.Assert;importorg.testng.annotations.Test;

    publicclassDependencyTestUsingAnnotation{ Stringmessage ="Manisha";

    MessageUtilmessageUtil =newMessageUtil(message);

    @Test(groups ={"init"}) publicvoidtestPrintMessage(){System.out.println("Inside testPrintMessage()");message ="Manisha";Assert.assertEquals(message,messageUtil.printMessage()); }

    @Test(dependsOnGroups ={"init.*"}) publicvoidtestSalutationMessage(){System.out.println("Inside testSalutationMessage()");message ="Hi!"+"Manisha";Assert.assertEquals(message,messageUtil.salutationMessage());

    }

    @Test(groups ={"init"}) publicvoidinitEnvironmentTest(){System.out.println("This is initEnvironmentTest"); }}

  • 8/14/2019 Testng Quick Guide

    23/35

    In this example, testSalutationMessag e() is declared as depending on any g roup matching the reg ularexpre ssion "init.*", which guarantee s that the methods tes tPrintMessag e() and initEnvironmentTe st() will always

    be invoked before testSalutationMessag e().

    If a method depended upon fails and you have a hard dependency on it (alwaysRun=false,which is the default), the methods that depend on it are not marked as FAIL but as SKIP.

    Skipped methods will be reported as such in the final report (in a color that is neither red nor

    green in HTML), which is important since skipped methods are not necessarily failures.

    Create testng .xml

    Create a testng .xml C:\ > TestNG_WORKSPACEto execute T est case (s).

    Compile the MessageUtil, Test case classes using javac.

    C:\TestNG_WORKSPACE>javac MessageUtil.java DependencyTestUsingAnnotation.java

    Now, run the testng .xml, which will run the testSalutationMessage()method only after the execution ofinitEnvironmentTest()method.

    C:\TestNG_WORKSPACE>java -cp "C:\TestNG_WORKSPACE" org.testng.TestNG testng.xml

    Verify the output.

    This is initEnvironmentTestInside testPrintMessage()ManishaInside testSalutationMessage()Hi!Manisha

    ===============================================Suite1

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

    dependsOnGroupsVs dependsOnMethods

    On using g roups, we are no long er exposed to re factoring problems. as long as we dont modify thedependsOnGroups or g roups attributes, our tes ts will kee p running with the proper de pendencies se t up.

    Whenever a new method needs to be added in the de pendency g raph, all we need to do is put it in the rightg roup and make sure it depends on the correct g roup. We dont need to modify any other method.

    PARAMETERIZED TEST

    Another interesting feature available in TestNG isparametric testing. In most case s, you'll come across ascenario where the business log ic re quires a hug ely varying number of tests.Parameterized testsallowdeveloper to run the same test over and ove r ag ain using different values.

    TestNG lets you pass parameters directly to your test methods in two differe nt ways:

  • 8/14/2019 Testng Quick Guide

    24/35

    With testng .xml

    With Data Providers

    Passing Parameters with testng.xml

    With this technique you define the s imple parameters in the testng.xmlfile and then refere nce those parametersin source files. Le t us see an example below on how to use this technique to pass parameters.

    Create Test Case Class

    Create a java test class say ParameterizedTest1.java.

    Add test method parameterT est() to your test class. This method takes a String as input parameter.

    Add the annotation @Parameters("myName")to this method. T he parameter would be pass ed valuesfrom testng .xml which we will see in the next step.

    Create the java class file name ParameterizedTest1.java in C:\ > TestNG_WORKSPACE

    importorg.testng.annotations.Parameters;

    importorg.testng.annotations.Test;

    publicclassParameterizedTest1{ @Test @Parameters("myName") publicvoidparameterTest(StringmyName){ System.out.println("Parameterized value is : "+myName); }}

    Create testng .xml

    Create a testng .xml C:\ > TestNG_WORKSPACEto execute T est case (s).

    We can also define the parameters at the level. Suppose we have defined myName atboth and , levels then, in such cases regular scoping rules apply. This means thatany class inside tag will see the value of parameter defined in , while the classesin the rest of the testng.xml file will see the value defined in .

    Compile the Test case class using javac.

    C:\TestNG_WORKSPACE>javac ParameterizedTest1.java

    Now, run the testng .xml, which will run theparameterTestmethod. TestNG will try to find a parameter named

    myNamefirst in the tag , and then, if it cant find it, it searches in the tag that encloses it.

    C:\TestNG_WORKSPACE>java -cp "C:\TestNG_WORKSPACE" org.testng.TestNG testng.xml

    Verify the output.

  • 8/14/2019 Testng Quick Guide

    25/35

    Parameterized value is : manisha

    ===============================================Suite1Total tests run: 1, Failures: 0, Skips: 0===============================================

    TestNG will automatically try to convert the value specified in testng .xml to the type of your parameter. Here arethe types supported:

    String

    int/Integ er

    boolean/Boolean

    byte/Byte

    char/Character

    double/Double

    float/Float

    long /Long

    short/Short

    Passing Parameters with Dataproviders

    When you need to pass complex parameters or parameters that need to be created from Java (complexobje cts, objects re ad from a property file or a database , etc...), in such cases parameters can be passed usingDataproviders. A Data Provider is a method annotated with @DataProvider. This annotation has only one stringattribute: its name. If the name is not supplied, the Data Providers name automatically de faults to the methodsname. A Data Provider re turns an array of obj ects.

    Let us check out examples below of using Dataproviders. T he first example is about @DataProvider usingVector, String or Integ er as parameter and the second example is about @DataProvider using object asparameter

    Example 1

    Here, the @DataProvider passes Integ er and Boolean as parameter.

    Create Java class

    Create a java class PrimeNumberChecker.java. This c lass checks if the number is prime. Create this c lass inC:\ > TestNG_WORKSPACE

    publicclassPrimeNumberChecker{ publicBooleanvalidate(finalIntegerprimeNumber){ for(inti =2;i

  • 8/14/2019 Testng Quick Guide

    26/35

    returns array of object array.

    Add test method testPrimeNumberChecker() to your test class. This method takes a Integ er and Booleanas input parameters. This method validates if the parameter passed is a prime number.

    Add the annotation @Test(dataProvider = "test1")to this method. The attribute dataProvider is mappedto "test1".

    Create the java c lass file name ParamTestWithDataProvider1.j ava in C:\ > TestNG_WORKSPACE

    importorg.testng.Assert;importorg.testng.annotations.BeforeMethod;importorg.testng.annotations.DataProvider;importorg.testng.annotations.Test;

    publicclassParamTestWithDataProvider1{ privatePrimeNumberCheckerprimeNumberChecker;

    @BeforeMethod publicvoidinitialize(){ primeNumberChecker =newPrimeNumberChecker(); }

    @DataProvider(name ="test1") publicstaticObject[][]primeNumbers(){ returnnewObject[][]{{2,true},{6,false},{19,true}, {22,false},{23,true}}; }

    // This test will run 4 times since we have 5 parameters defined @Test(dataProvider ="test1") publicvoidtestPrimeNumberChecker(IntegerinputNumber, BooleanexpectedResult){System.out.println(inputNumber +" "+expectedResult);Assert.assertEquals(expectedResult, primeNumberChecker.validate(inputNumber)); }

    }

    Create testng .xml

    Create a testng .xml C:\ > TestNG_WORKSPACEto execute T est case (s).

    Compile the Test case class using javac.

    C:\TestNG_WORKSPACE>.javac ParamTestWithDataProvider1.java PrimeNumberChecker.java

    Now, run the tes tng .xml.

    C:\TestNG_WORKSPACE>java -cp "C:\TestNG_WORKSPACE" org.testng.TestNG testng.xml

    Verify the output.

    2 true6 false19 true22 false

  • 8/14/2019 Testng Quick Guide

    27/35

    23 true

    ===============================================Suite1Total tests run: 5, Failures: 0, Skips: 0===============================================

    Example 2

    Here, the @DataProvider pass es Object as parameter.

    Create Java class

    Create a java class Bean.java, which is simple object with g et/se t methods, inC:\ >TestNG_WORKSPACE.

    publicclassBean{ privateStringval; privateinti; publicBean(Stringval,inti){ this.val=val; this.i=i;

    } publicStringgetVal(){returnval; } publicvoidsetVal(Stringval){this.val =val; } publicintgetI(){returni; } publicvoidsetI(inti){this.i =i; }}

    Create Test Case Class

    Cre ate a java tes t class say ParamTestWithDataProvider2.java.

    De fine the method primeNumbers() which is defined as a Dataprovider using the annotation. This methodreturns array of object array.

    Add test method testMethod() to your test class . This method takes object bean as parameter.

    Add the annotation @Test(dataProvider = "test1")to this method. The attribute dataProvider is mappedto "test1".

    Create the java c lass file name ParamTestWithDataProvider2.java inC:\ > TestNG_WORKSPACE

    importorg.testng.annotations.DataProvider;importorg.testng.annotations.Test;

    publicclassParamTestWithDataProvider2{ @DataProvider(name ="test1") publicstaticObject[][]primeNumbers(){ returnnewObject[][]{{newBean("hi I am the bean",111)}}; }

    @Test(dataProvider ="test1")

    publicvoidtestMethod(BeanmyBean){ System.out.println(myBean.getVal()+" "+myBean.getI()); }}

    Create testng .xml

  • 8/14/2019 Testng Quick Guide

    28/35

    Create a testng .xml C:\ > TestNG_WORKSPACEto execute T est case (s).

    Compile the Test case class using javac.

    C:\TestNG_WORKSPACE>javac ParamTestWithDataProvider2.java Bean.java

    Now, run the tes tng .xml.

    C:\TestNG_WORKSPACE>java -cp "C:\TestNG_WORKSPACE" org.testng.TestNG testng.xml

    Verify the output.

    hi I am the bean 111

    ===============================================Suite1Total tests run: 1, Failures: 0, Skips: 0===============================================

    RUN JUNIT TESTS

    Now that you have understood TestNG and its various tests, you must be worried by now as to how to re factoryour existing Junit code. There's no need to worry as T estNG provides a way to shift from Junit to TestNG at

    your own pace . You can execute your existing Junit test cases using TestNG.

    TestNG can automatically re cog nize and run JUnit tests, s o you can use TestNG as a runner for all your existingtests and write new tests using TestNG. All you have to do is to put JUnit library on the TestNG classpath, so itcan find and use JUnit classes, chang e your test runner from JUnit to T estNG in Ant and then run TestNG in"mixed" mode. T his way you can have all your tes ts in the same project, even in the same package, and startusing TestNG. T his approach also allows you to convert your existing JUnit tests to T estNG incre mentally.

    Let us see an example below and try out the above feature:

    Create Junit Test case Class

    Create a java class which is a Junit test class, T estJunit.java inC:\ > TestNG_WORKSPACE

    importorg.junit.Test;importstaticorg.testng.AssertJUnit.assertEquals;

    publicclassTestJunit{ @Test publicvoidtestAdd(){ Stringstr="Junit testing using TestNG"; assertEquals("Junit testing using TestNG",str); }}

    Now, let's write the tes tng .xml inC:\ > TestNG_WORKSPACEwhich would contain the tag asfollows:

  • 8/14/2019 Testng Quick Guide

    29/35

    To e xecute the Junit test cases define propertyjunit="true"as in the xml above. The Junit test case class

    TestJunit is defined in class name.

    For Junit 4, TestNG will use the org.junit.runner.JUnitCorerunner to run your tests.

    Compile all java classes using javac.

    C:\TestNG_WORKSPACE>javac TestJunit.java

    Now, run the tes tng .xml, which will run Junit test case as TestNG.

    C:\TestNG_WORKSPACE>java -cp "C:\TestNG_WORKSPACE:C:\TestNG_WORKSPACE\lib\junit-4.11.jar"org.testng.TestNG testng.xml

    Here I've placed the junit-4.11.jar under C:\TestNG_WORKSPACE\lib\junit-4.11.jar.

    Verify the output.

    ===============================================Converted JUnit suite

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

    TEST RESULTS

    Reporting is the most important part of any tes t exe cution, reason being it helps the user to understand the resultof the tes t exe cution, point of failure, and reasons for the failure . Log g ing , on the other hand, is important to keepan eye on the execution flow or for debug g ing in case of any failures.

    TestNG by de fault generates a different type of report for its test execution. T his includes an HTML and anXML re port output. Te stNG also allows its users to write their own reporter and use it with TestNG. T here isalso an option to write your own logg ers, which are notified at runtime by T estNG.

    There are two main ways to g enerate a report with TestNG:

    Listeners :For implementing a listener class, the class has to implement the org.testng.ITestListenerinterface . T hese classes are notified at runtime by T estNG when the test starts, finishes, fails, skips, or

    passes.

    Reporters :For implementing a re porting class, the class has to implement an org.testng.IReporterinterface . T hese classes are called when the whole suite run ends. T he object containing the informationof the whole test run is passed to this class when called.

    The table below lists examples for differe nt case s of reporting and log g ing :

    Custom Logg ing This example illustrates how to write your own log g er.

    Custom Reporter This example illustrates how to write your own reporter.

    HTML and XMLreport

    This example illustrates the default HTML and XML re port generated by T estNG.

    JUnit Reports This example illustrates the about g enerating Junit reports from TestNG reports.

    http://localhost/var/www/apps/conversion/tmp/scratch_9/testng/testng_junitreports.htmhttp://localhost/var/www/apps/conversion/tmp/scratch_9/testng/testng_html_xml_report.htmhttp://localhost/var/www/apps/conversion/tmp/scratch_9/testng/testng_custom_reporter.htmhttp://localhost/var/www/apps/conversion/tmp/scratch_9/testng/testng_custom_logger.htm
  • 8/14/2019 Testng Quick Guide

    30/35

    PLUG WITH ANT

    In this example, we will demonstrate how to run TestNG using ANT. Le t's follow the g iven steps:

    Step 1: Download Apache Ant

    DownloadApache Ant

    OS Archive name

    Windows apache-ant-1.8.4-bin.zip

    Linux apache-ant-1.8.4-bin.tar.g z

    Mac apache-ant-1.8.4-bin.tar.g z

    Step 2: Set Ant Environment

    Set theANT _HOME environment variable to point to the base directory location where ANT libraries isstore d on your machine. For example, We 've stored Ant libraries in apache-ant-1.8.4 folder on variousOperating Systems as follows.

    OS Output

    Windows Set the e nvironment variable ANT_HOME to C:\Prog ram Files\Apache SoftwareFoundation\apache-ant-1.8.4

    Linux export ANT_HOME=/usr/local/\apache-ant-1.8.4

    Mac export ANT_HOME=/Library/\apache-ant-1.8.4

    Append Ant compiler location to System Path is as follows for different OS:

    OS Output

    Windows Append the s tring ;%ANT_HOME\bin to the e nd of the system variable, Path.

    Linux export PATH=$PATH:$ANT_HOME/bin/

    Mac not required

    Step 3: Download TestNG Archive

    Download http://www.testng .org .

    OS Archive name

    Windows testng -6.8.j ar

    Linux testng -6.8.jar

    Mac testng -6.8.jar

    http://www.testng.org/http://ant.apache.org/bindownload.cgi
  • 8/14/2019 Testng Quick Guide

    31/35

    Step 4: Create Project Structure

    Create folder TestNGWithAntinC:\ > TestNG_WORKSPACE

    Create folder src inC:\ > TestNG_WORKSPACE > TestNGWithAnt

    Create folder testin C:\ > TestNG_WORKSPACE > TestNGWithAnt

    Create folder libinC:\ > TestNG_WORKSPACE > TestNGWithAnt

    Cre ate Me ssageUtil class inC:\ > TestNG_WORKSPACE > TestNGWithAnt > src folder

    /** This class prints the given message on console.*/publicclassMessageUtil{

    privateStringmessage;

    //Constructor //@param message to be printed publicMessageUtil(Stringmessage){ this.message =message;

    }

    // prints the message publicvoidprintMessage(){ System.out.println(message); returnmessage; }

    // add "Hi!" to the message publicStringsalutationMessage(){ message ="Hi!"+message; System.out.println(message); returnmessage; }

    }

    Create TestMessag eUtil class inC:\ > TestNG_WORKSPACE > TestNGWithAnt > src folder

    importorg.testng.Assert;importorg.testng.annotations.Test;

    publicclassTestMessageUtil{ Stringmessage ="Manisha";

    MessageUtilmessageUtil =newMessageUtil(message);

    @Test publicvoidtestPrintMessage(){

    System.out.println("Inside testPrintMessage()");Assert.assertEquals(message,messageUtil.printMessage());

    }

    @Test publicvoidtestSalutationMessage(){ System.out.println("Inside testSalutationMessage()"); message ="Hi!"+"Manisha"; Assert.assertEquals(message,messageUtil.salutationMessage()); }}

    Copy testng -6.8.jar in C:\ > TestNG_WORKSPACE > TestNGWithAnt > lib folder

    Create ANT build.xml

    First we nee d define the TestNG ant task as follows:

  • 8/14/2019 Testng Quick Guide

    32/35

    Then, we'll be using task in Ant to execute our Te stNG test cases.

    The C:\ > TestNG_WORKSPACE > TestNGWithAnt >\ build.xmlis as follows:

    Run the following ant command.

    C:\TestNG_WORKSPACE\TestNGWithAnt>ant

    Verify the output.

    test: [testng] [TestNG] Running: [testng] C:\TestNG_WORKSPACE\TestNGWithAnt\src\testng.xml [testng]

    [testng] Inside testPrintMessage() [testng] Manisha [testng] Inside testSalutationMessage() [testng] Hi!Manisha [testng]

    [testng] =============================================== [testng] Plug ANT test Suite [testng] Total tests run: 2, Failures: 0, Skips: 0 [testng] =============================================== [testng]

  • 8/14/2019 Testng Quick Guide

    33/35

    BUILD SUCCESSFULTotal time: 1 second

    PLUG WITH ECLIPSE

    To set up TestNG with eclipse , the following steps need to be followed.

    Step 1: Download TestNG archive

    Download http://www.testng .org

    OS Archive name

    Windows testng -6.8.j ar

    Linux testng -6.8.jar

    Mac testng -6.8.jar

    Assume you copied above JAR file in C:\>T estNG folder.

    Step 2: Set Eclipse environment

    Ope n eclipse -> right click on project and click on property > Build Path > Configure Build Path and addthe testng -6.8.jar in the libraries using Add External Jarbutton.

    We as sume that your eclipse has inbuilt TestNG plug in if it is not available then please g et the latestversion using the update s ite:

    In your eclipse IDE selectHelp / Software updates / Find and Install.

    Search for new features to install.

    http://www.testng.org/
  • 8/14/2019 Testng Quick Guide

    34/35

    New remote site.

    For Eclipse 3.4 and above, enter http://beust.com/eclipse.

    For Eclipse 3.3 and below, enter http://beust.com/eclipse1.

    Make sure the check box next to URL is checked and clickNext.

    Eclipse will then guide you through the process .

    Now, your ec lipse is ready for the development of TestNG test cases.

    Step 3: Verify TestNG installation in Eclipse

    Create a project TestNGProject in eclipse at any location.

    Cre ate a class Me ssageUtil to test in the proje ct.

    /** This class prints the given message on console.*/publicclassMessageUtil{

    privateStringmessage;

    //Constructor //@param message to be printed publicMessageUtil(Stringmessage){ this.message =message; }

    // prints the message publicStringprintMessage(){ System.out.println(message); returnmessage;

    }}

    Cre ate a test class TestNGExample in the proje ct.

    importorg.testng.Assert;importorg.testng.annotations.Test;

    publicclassTestNGExample{ Stringmessage ="Hello World";

    MessageUtilmessageUtil =newMessageUtil(message);

    @Test publicvoidtestPrintMessage(){

    Assert.assertEquals(message,messageUtil.printMessage()); }}

    Following should be the project structure:

    http://beust.com/eclipse1http://beust.com/eclipse
  • 8/14/2019 Testng Quick Guide

    35/35

    Finally, ve rify the output of the prog ram by right click on prog ram and run as TestNG.

    Verify the re sult.