[go: up one dir, main page]

0% found this document useful (0 votes)
122 views36 pages

TestNg Quick Notes.

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
122 views36 pages

TestNg Quick Notes.

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 36
TESTNG - QUICK GUIDE Testing is the process of checking the functionality of the application whether it is working as per requirements and to ensure that at developer level, unit testing comes into picture. Unit testing is the testing of single entity classormethod. Unit testing is very essential to every software company to give a quality product to their customers. JUnit has driven developers to understand the usefulness of tests, especially of unit tests when ‘compared to any other testing framework. Leveraging a rather simple, pragmatic, and strict architecture, JUnit has been able to "infect" great number of developers. Features of JUnit can be seen in Junit Features. Some of the short comings of JUnit are: + Initially designed to enable unit testing only, now used for all kinds of testing. + Cannot do dependency testing + Poor configuration control setUp/tearDown ‘+ Intrusive forcesyoutoextendclassesandnameyourmethodsacertainway. + Static programming model forcesyoutorecompileunnecessarily. + The management of different suites of tests in complex projects can be very tricky.. What is TestNG? Definition of TestNG as per its documentation is: TestNG is a testing framework inspired from JUnit and NUnit but introducing some new functionalities that make it more powerful and easier to use. TestNG is an open source automated testing framework; where NG of TestNG means Next Generation. TestNG is similar to JUnit especiallyJUnit4, but its not a JUnit extension. Its inspired by JUnit. Itis designed to be better than JUnit, especially when testing integrated classes. The creator of TestNG is Cedric Beust. TestNG eliminates most of the limitations of the older framework and gives the developer the ability to write more flexible and powerful tests. As it heavily borrows from Java Annotations introducedwithJDKS.0 to define tests, it can also show you how to use this new feature of the Java language in a real production environment. TestNG Features + Annotations. + TestNG uses more Java and 00 features. + Supports testing integrated classes e. g., bydefault, noneedtocreateanewtestclassinstanceforeverytestmethod. + Separate compile-time test code from run-time configuration/data info + Flexible runtime configurati + Introduces ‘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 ENVIRONMENT SET-UP TTestNG is a framework for Java, so the very first requirement is to have JDK installed in your machine. System Requirement JDK 15 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 execute the following java command. os Task Command Windows Open Command Console _c:\> java -version Linux Open Command Terminal §$ java -version Mac Open Terminal machine:~ josephs java -version Let's verify the output for all the operating systems: os Output Windows java version "1.7.0_2 JavaTM SE Runtime Environment buildl.7.0,5 - b15 Java HotSpot?™M 64-Bit Server VM build23.25 — bO1, mixedmode Linux java version "1.7.0_2! JavaTM SE Runtime Environment build!.7.0)5 ~ b15 Java HotSpot™M 64-Bit Server VM build: = BOL, mixedmode Mac java version “1.7.0_25 JavaTM SE Runtime Environment buildi.7.0,5 - b15 Java HotSpotT™M 64-Bit Server VM build23 15 ~ bOL, mixedmode If you do not have Java installed, install the Java Software Development Kit SDK from http://www.oracle.com/technetwork/iava/iavase/downloads/index.html. We are assuming Java 1.,7.0_25 as installed 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 on your machine. For example; os Output Whig dc Cat the emuieammmentuarinkle JAVA UAME ta CADeRare ee, Linux Mac export JAVA_HOME=/usr/local/java-current export JAVA_HOME=/Library/Java/Home ‘Append Java compiler location to System Path, os Windows Linux Mac Output Append the string ;C:\Program Files\Java\jdk1.7.0_25\bin to the end of the system variable, Path. export PATH=PATH:JAVA_HOME/bin/ not required \Verify Java Installation using java -version command explained above. Step 3: Download TestNG archive Download latest version of TestNG jar file from http://www.testng.org. At the time of writing this tutorial, | downloaded testng-6.8,jar and copied it into C:\>TestNG folder. os Windows Linux Mac Archive name testng-6.8,jar testng-6.8,jar 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 is stored on your machine, Assuming, we've stored testng-6.8,jar in TestNG folder on Various Operating Systems as follows. os Windows Linux Mac Output Set the environment variable TESTNG_HOME to C:\TESTNG export TESTNG_HOME=/usr/local/TESTNG export TESTNG_HOME=/Library/TESTNG Step 5: Set CLASSPATH variable Set the CLASSPATH environment variable to point to the TestNG jar location. Assuming, we've btored testng-6.8.jar in TestNG folder on various Operating Systems as follows os Windows Linux Output Set the environment variable CLASSPATH to %CLASSPATH%;%TESTNG_HOME%\testng-6.8 jar; export CLASSPATI ASSPATH:TESTNG HOME/testna-6.8 jar: Mac export CLASSPATH=CLASSPATH: TESTNG_HOME/testng-6.8 jar: Step 6: Test TestNG Setup Create a java class file name TestNGSimpleTest in C:\ > TestNG_WORKSPACE import org. testng annotations. test; import static org. testng Assert.assertEquals; public class TestNGSimpleTest { OTest public void testadd() { String str = "TestNG is working fine"; assertEquals("TestNG is working fine", str); 3 } TestNG can be invoked in several different ways: + With a testng.xm! file * With ant + From the command line Let us invoke using the testng.xml file. Create an xml file with name testng.xml in Cx\ > TestNG WORKSPACE to execute Test cases. Step 7: Verify the Result ‘Compile the class using javac compiler as follows: €:\TestNG_WORKSPACE>javac TestnGSimpleTest. java Now, invoke the testng.xml to see the result. :\TestNG_WORKSPACE>java -cp "C:\TestNG_WORKSPACE" org.testng.TestNG testng.xm1 Verify the output. suiter Total tests run: Failures: ©, Skips: @ WRITING TESTS Writing a test in TestNG basically involves following steps: + Write the business logic of your test and insert TestNG annotations in your code. file or in build.xml. + Run TestNG. Here, we will see one complete example of TestNG testing using POJO class, Business logic class and a test xml, which will be run by TestNG. create EmployeeDetails.java in C:\ > TestNG WORKSPACE which is a POJO class. public class EmployeeDetails { private String name; private double monthlySalary; private int age; pe * @return the nane 7 public String getName() { return name; i jee * @paran name the name to set ” public void setName(String name) { this.name = name; 3 yee * @return the monthlySalary 7 public double getmonthlysalary() { return monthlySalary; 3 ye * @paran monthlySalary the monthlySalary to set a public void setMonthlySalary(double monthlySalary) { this.monthlySalary = monthlySalary; } yee * @return the age +7 public int getage() { return age; 3 jee * @param age the age to set */ public void setAge(int age) { this.age = age; } } EmployeeDetails class is used to: + get/set the value of employee's name. + get/set the value of employee's monthly salary. + get/set the value of employee's age create a EmpBusinessLogic.java in C:\ > TestNG_WORKSPACE which contains business logic. public class EmpBusinessLogic { 7/ Calculate the yearly salary of employee public double calculatevearlySalary(EmployeeDetails employeeDetails){ ee eee eee return yearlySalary; 3 // Calculate the appraisal amount of employee public double calculateappraisal (EmployeeDetails employeeDetails){ double appraisal=0; if (employeeDetails.getMonthlySalary() < 19000){ appraisal = 500; jelset appraisal = 1000; } return appraisal; ) } EmpBusinessLogic class is used for calculating, + the yearly salary of employee. + the appraisal amount of employee. Now, let's create a TestNG class called TestEmployeeDetails.java in C:\ > TTestNG_WORKSPACE.A TestNG class is a Java class that contains at least one TestNG annotation. This class contains test cases to be tested. A TestNG test can be configured by @BeforeXXX and @AfterXXX annotations (we will see this in the chapter TestNG - Execution Procedure) which allows to perform some Java logic before and after a certain point. import org.testng.Assert; import org. testng annotations Test; Public class TestemployeeDetails { EmpBusinessLogic empBusinessLogic = new EmpBusinessLogic(); EmployeeDetails employee = new EmployeeDetails(); Gest public void testcalculateappriasal() { employee. setName("Rajeev"); employee. setage(25); employee. setMonthlySalary (8000); double appraisal = empBusinessLogic -calculateAppraisal (employee); Assert.assertEquals(500, appraisal, 0.9, "500"); } // test to check yearly salary @Test public void testcalculateyearlySalary() { employee. setName("Rajeev"); employee. setage(25); employee. setMonthlySalary (8000); double salary = empBusinessLogic -calculateYear lySalary(emplayee); Assert.assertEquals(96000, salary, 0.0, "8000"); } } TTestEmployeeDetails class is used for testing the methods of EmpBusit essLogic class. It + tests the yearly salary of the employee. + tests the appraisal amount of the employee. Before you can run the tests, however, you must configure TestNG using a special XML file, conventionally named testng.xml. The syntax for this file is very simple, and its contents as below. Create this file in C:\ > TestNG WORKSPACE: Details of the above file are as below + Asuite is represented by one XML file. It can contain one or more tests and is defined by the tag, + Tag represents one test and can contain one or more TestNG classes. + tag represents a TestNG class is a Java class that contains at least one TestNG annotation. It can contain one or more test methods. ‘ompile the Test case classes using javac :\TestNG_WORKSPACE>javac EmployeeDetails. java EmpBusinessLogic.java TestemployeeDetails. java pow TestNG with the following command: C:\TestNG_WORKSPACE>java -cp "C:\TestNG_WORKSPACE" org.testng.TestN testng.xml if all has been done correctly, you should see the results of your tests in the console. Furthermore, TestNG creates a very nice HTML report in a folder called test-output that is automatically reated in the current directory. If you open it and load index.html, you will see a page similar to ihe one in the image below: a BASIC ANNOTATIONS The traditional way to indicate test methods in JUnit 3 is by prefixing their name with test. This is a /ery effective method for tagging certain methods in a class as having a special meaning, but the \aming doesn’t scale very well whatifwewanttoaddmoretagsfordifferent{rameworks? and is rather infle: :tfwewanttopassadditionalparameterstothetestingframework?. notations were formally added to the Java language in JOK 5 and TestNG made the choice to use notations to annotate test classes. jere is the list of annotations that TestNG supports: Annotation Description @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. @AfterClass @BeforeTest @AfterTest @BeforeGroups @AfterGroups @BeforeMethod @AfterMethod @DataProvider @Factory @Listeners @Parameters @Test The annotated method will be run only once after all the test methods in the current class have been run. The annotated method will be run before any test method belonging to the classes inside the tag is run. The annotated method will be run after all the test methods belonging to the classes inside the tag have run. The list of groups that this configuration method will run before. This method is guaranteed to run shortly before the first test method that belongs to any of these groups is invoked. The list of groups that this configuration method will run after. This method. is guaranteed to run shortly after the last test method that belongs to any of these groups is invoked. The annotated method will be run before each test method. The annotated method will be run after each test method Marks a method as supplying data for a test method. The annotated method must return an Object/][] where each Object(] can be assigned the parameter list of the test method. The @Test method that wants to receive data from this DataProvider needs to use a dataProvider name equals to the name of this annotation. Marks a method as a factory that returns objects that will be used by TestNG as Test classes, The method must return Object). Defines listeners on a test class. Describes how to pass parameters to a @Test method. 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 not restricted to any pattern or format, + We can pass additional parameters to annotations. + Annotations are strongly typed, so the compiler will flag any mistakes right away. + Test classes no longer need to extend anything suchasTestCase, forJUnit3. EXCECUTION PROCEDURE This tutorial explains the execution procedure of methods in TestNG which means that which method is called first and which one after that. Here is the execution procedure of the TestNG test ‘AP| methods with the example. ‘create a java class file name TestngAnnotation java in C:\ > TestNG_WORKSPACE to test annotation. import import import import Smport org org org org ora testng testng testng testng testna annotations Test; annotations .BeforeMethod; annotations .AfterMethod; annotations .BeforeClass; annotations AfterC las: import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeSuite; import org, testng annotations AfterSuite; public class TestngAnnotation { // test case 1 @Test public void testcase1() { System out.printIn("in test case 1"); + // test case 2 @Test public void testcase2() { ‘System .out.println("in test case 2"); 3 @Beforemethod public void beforeMethod() { ‘System .out.println("in beforeMethod"); + @afterMethod public void aftermethod() { ‘System .out.println("in aftermethod"); 3 @eforeclass public void beforeClass() { System.out.println("in beforeClass" ); + @afterClass public void afterclass() { System out.printIn("in afterClass"); 3 @Beforetest public void beforeTest() { ‘System .out.println("in beforeTest"); } @aftertest public void afterTest() { System .out.println("in afterTest"); + @Beforesuite public void beforesuite() { ‘System .out.println("in beforesuite"); 3 @aftersuite public void afterSuite() { System out .printIn("in afterSuite"); } } Next, let's create the file testng.xml in C:\ > TestNG WORKSPACE to execute annotations. Compile the Test case class using javac. C:\TestNG_WORKSPACE>javac TestngAnnotation. java Now, run the testng.xml, which will run test case defined in provided Test Case class. C:\TestNG_WORKSPACE>java org.testng.TestNe testng.xm1 Verify the output. in beforesuite in beforeTest in beforeclass in beforeMethod in test case 1 in aftermethod in beforeMethod in test case 2 in afterMethod in afterClass in afterTest in afterSuite suite Total tests run: 2, Failures: 0, Skips: See the above output and this is how the TestNG execution procedure is: + First of all beforeSuite method is executed only once. + Lastly, the afterSuite method executes only once. + Even the methods beforeTest, beforeClass, afterClass and afterTest methods are executed only once. + beforeMethod method executes for each test case but before executing the test case. + afterMethod method executes for each test case but after the execution of test case, + In between beforeMethod and afterMethod each test case executes. EXECUTING TESTS The test cases are executed using TestNG class, This class is the main entry point for running tests in the TestNG framework, Users can create their own TestNG object and invoke it in many different ways: + Onan existing testng.xml + Ona synthetic testng.xml, created entirely from Java + By directly setting the test classes. You can also define which groups to include or exclude, assign parameters, etc. The command line parameters are: + -d outputdir: specify the output directory + -testclass class_name: specifies one or several class names -sourcedir src1;src2: ; separated list of source directories usedonlywhenjavadocannotationsareused target -groups -testrunfactory -listener We will create the TestNG object an existing testng.xm| in our example below. Create a Class 7 Create a java class to be tested say MessageUtil.java in C:\ > TestNG_WORKSPACE * this class prints the given message on console. +7 public class MessageUtil { } private String message; //constructor //@paran message to be printed public MessageUtil(String message){ this.message = message; } // prints the message public String printMessage(){ System .out.printIn(message) ; return message; 3 Create Test Case Class Create a java test class say SampleTestjava. ‘Add a test method testPrintMessage to your test class. ‘Add an Annotation @Test to method testPrintMessage Implement the test condition and check the condition using assertEquals API of TestNG. Create a java class file name SampleTest.java in C:\ > TestNG_WORKSPACE imp imp jort org. testng Assert; ort org. testng. annotations. Test; public class SampleTest { } String message = "Hello World"; MessageUtil messageUtil = new MessageUtil(message); OTest public void testPrintMessage() { Assert.assertEquals(message, messageUtil.printMessage()); } Create testng.xml Next, let's create testng.xml file in C:\ > TestNG WORKSPACE to execute Test cases. This file makes it easy to extract subsets of your tests or split several runtime configurations kg. , testng ~ database. xmlwouldrunonlyteststhatexerciseyourdatabase. <2xm1 version="1.0" encoding="UTF-8"?> ‘SampleTest" /> Compile the Test case using javac. C:\TestNG_WORKSPACE>javac MessageUtil.java SanpleTest.java Now, run the testng.xml, which will run test case defined in tag. C:\TestNG_WORKSPACE>java -cp "C:\TestNG_WORKSPACE" org.testng,TestNG testng.xm1 \Verify the output. Hello World Sample test Suite Total tests run: 1, Failures: 0, Skips: @ SUITE TESTS | Test suite is a collection of test cases that are intended to test a behavior or set of behaviors of software program. In TestNG, we cannot define a suite in testing source code, but it is represented by one XML file as suite is the feature of execution, This also allows flexible configuration of the tests to be run. A suite can contain one or more tests and is defined by the tag. is a root tag of your testng.xml. It describes a test suite, which in turn is made of several sections. Table below lists all the legal attributes accepts. Attribute Description name The name of this suite. It is a mandatory attribute verbose The level or verbosity 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 ignoredother ~ 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 Test & Test2 test classes to run together using Test Suite. Create a Class beg tae nla ce tn ha tactadd eau: Maccamal II ieue inet qe “a * This class prints the given message on console. y public class MessageUtil { private String message; // Constructor // @paran message to be printed public NessageUtil(String message) { this.message = message; } // prints the message public String printMessage() { ‘System .out.printLn(message); return message; } // add “Hi!” to the message public String salutationMessage() { message = "Hil" + message; System .out.printin(message); return message; } } (Create Test Case Classes Create a java class file name Test.java in > TestNG WORKSPACE import org. testng Assert; import org. testng annotations test; public class Test { String message = "Manisha"; MessageUtil messageUtil = new MessageUtil (message); @Test public void testPrintMessage() { ‘system .out.println("Inside testPrintMessage()"); Assert assertEquals(message, messageUtil printMessage()); } + ‘create a java class file name Test2 java in C:\ > TestNG_WORKSPACE import org.testng Assert; import org. testng annotations Test; public class Test2 { String message = "Manisha"; MessageUtil messageUtil = new MessageUtil (message); atest public void testSalutationMessage() { system .out.printIn("Inside testSalutationtessage()"); message = "Hi!" + "Manisha"; Assert assertEquals (message, messageUtil. salutationMessage()); } Now, let's write the testng.xm| in C:\ > TestNG_WORKSPACE which would contain the tag as follows: Testi" /> ‘exampletest2"> Test2" /> Suitel includes exampletest1 and exampletest2. Compile all java classes using javac. C:\TestNG_WORKSPACE>javac MessageUtil.java Testi.java Test2.java Now, run the testng.xml, which will run test 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() Manisha Inside testSalutationMessage() Hi tManisha suiter Total tests run: 2, Failures: 0, Skips: 0 IGNORE TESTS Sometimes, it happens that our code is not ready and test case written to test that method/code will fail if run. In such cases annotation @Testenabled = alse helps to disable this test case. [A test method annotated with @Testenabled ~ false, then the test case that is not ready to test is, bypassed. Now, let's see @Testenabled = false in action. Create a Class + Create a java class to be tested say MessageUtil java in C:\ > TestNG WORKSPACE ye * This class prints the given message on console. “fy public class MessageUtil { private String message; /{Constructor //@paran message to be printed public MessageUtil(string message){ this.message = message; } // prints the message public String printMessage(){ ‘Svctem cur orintin(messane) } // add "Hil" to the message public String salutationMessage(){ message = "Hi!" + message; ‘System out println(message) ; return messag } (Create Test Case Class * Create a java test class say IgnoreTest java. + Add test methods testPrintMessage, testSalutationMessage to your test class. + Add an Annotation @Testenabled = false to method testPrintMessage. Create a java class file name IgnoreTestjava in > TestNG_WORKSPACE import org. testng.Assert; import org. testng annotations test; public class IgnoreTest { ‘String message = "Manisha"; MessageUtil messageUtil = new MessageUtil (message); @Test(enabled = false) public void testPrintMessage() ( system .out.printIn("Inside testPrintMessage()"); message = ‘Manisha’; Assert assertequals(message, messageUtil.printMessage()); } Otest public void testSalutationMessage() { system .out.println(""Inside testSalutationtessage()"); message = "Hi!" 4 "Manisha"; Assert assertEquals(message, messageUtil.salutationMessage()); } } Create testng.xml (Create a testng.xml C:\ > TestNG_ WORKSPACE to execute Test cases IgnoreTest" /> 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 testPrintMessage test case defined in provided Test Case class. C:\TestNG_WORKSPACE>java -cp "C:\TestNG_WORKSPACE" org.testng.TestNG testng.xml Inside testSalutationMessage() Hi !Manisha suite1 Total tests run: 1, Failures: 0, Skips: 0 GROUP TEST ‘The group test is a new innovative feature in TestNG, it doesn’t exist in JUnit framework, it permits you dispatch methods into proper portions and preform sophisticated groupings of test methods Not only can you declare those methods that belong to groups, but you can also specify groups that contain other groups. Then, TestNG can be invoked and asked to include a certain set of, groups orregularexpressions while excluding another set. This gives you maximum flexibility in how you partition your tests and doesn’t require you to recompile anything if you want to run two different Sets of tests back to back. Groups are specified in your testng.xml file using the tag. It can be found either under the or tag. Groups specified in the tag apply to all the tags underneath, Now, let's see an example of how to group test. Create a Class + Create a java class to be tested say MessageUtil java in C:\ > TestNG WORKSPACE 7 * This class prints the given message on console. " public class MessageUtil { private String message; // Constructor // @paran message to be printed Public MessageUtil(String message) { this.message = message; } // prints the message public String printMessage() { ‘system .out.println(message) ; return message; } // add “tutorialspoint" to the message public String salutationMessage() { message = "tutorialspoint" + message; system .out.printIn(message) ; return message; } // add “wuw." to the message public String exitMessage() { message = "www." + message; system .out.println(message); return message; } } Create Test Case Class + Add test methods testPrintMessage, testSalutationMessage to your test class. + Group the test method in two categories say: © Check-in tests checkintest: These tests should be run before you submit new code. They should typically 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 be run atleast once a day, although ideally you would want to run them continuously. Create the java class file name GroupTest€xample java in C:\ > TestNG_WORKSPACE import org. testng Assert; import org. testng annotations Test; public class GroupTestexample { String message = ".com"; MessageUtil messageUtil = new MessageUtil (message); @Test(groups = { “functest", “checkintest” }) public void testprintMessage() { ‘System .out.println("Inside testPrintMessage()"); message = ".com"; Assert assertEquals(message, messageUtil.printmessage()); } @Test(groups = { "checkintest" }) public void testsalutationMessage() { System out.printIn("Inside testSalutationtessage()"); message = "tutorialspoint" + ".com"; Assert assertEquals(message, messageUtil .salutationtessage()); } @Test(groups = { “functest" }) public void testingExitmessage() { ‘System-out println("Inside testExitMessage()"); message = "www." + “tutorialspoint"+" com"; Assert assertequals(message, messageUtil .exitMessage()); } } Create testng.xml Create a testng.xml €:\ > TestNG_WORKSPACE to execute Test cases. Here, we would be executing only those tests which belong to the group functest. functest" /> (Compile the MessageUtil, Test case classes using javac. C:\TestNG_WORKSPACE>javac MessageUtil. java GroupTestExample.java functest. C:\TestNG_WORKSPACE>java -cp "C:\TestNG_WORKSPACE" org.testng.TestNG testng.xm1 Verify the output. Only the method testPrintMessage is executed. Inside testPrintMessage() con Inside testExitMessage() www. .com Groups of groups Groups can also include other groups. These groups are called MetaGroups. For example, you might want to define a group ail that includes checkintest and functest. Let's modify our testng.xml. file as below: ‘all"> functest"/> ‘checkintest"/> al1"/> GroupTestexample" /> Executing the above testng.xmi will execute all the three tests and will give you the below result: Inside testPrintMessage() con Inside testSalutationMessage() tutorialspoint.com Inside testExitMessage() www. tutorial spoint.com Suite1 Total tests run: 3, Failures: ©, Skips: 0 Exclusion groups You can ignore a group by using the tag as shown below: ‘all"/> EXCEPTION TEST TestNG provides a option of tracing the Exception handling of code. You can test whether a code throws desired exception or not. The expectedExceptions parameter is used along with @Test annotation. Now let's see @TestexpectedExceptions in action. Create a Class + Create a java class to be tested say MessageUtil java in C:\ > TestNG_ WORKSPACE. + Add a error condition inside printMessage method ye * This class prints the given message on console. “fy public class MessageUtil { private String message; /{Constructor //@paran message to be printed public MessageUtil(string message){ this.message = message; } // prints the message public void printMessage(){ ‘System -out printIn(message) ; int a =0; int b = 1/a; } // add "Hil" to the message public String salutationMessage(){ message = "Hil" + message; system .out.printIn(message) ; return message; 3 } Create Test Case Class * Create a java test class say ExpectedExceptionTestjava. + Add expected exception ArithmeticException to testPrintMessage test case. ‘Create a java class file name ExpectedExceptionTest java in C:\ > TestNG_WORKSPACE import org.testng.Assert; import org. testng annotations Test; public class ExpectedexceptionTest { String message = “Manish OTest(expectedExceptions = Arithmeticexception.class) public void testPrintMessage() { ‘system .out.println("Inside testPrintMessage()"); messageUtil printmessage(); } OTest public void testSalutationMessage() { System out.printIn("Inside testSalutationMessage()"); message = "Hi!" + "Manisha"; Assert .asser tEquals(message, messageUtil. salutationMessage()); > } Create Test Runner Create a testng.xml in C:\ > TestNG_WORKSPACE to execute Test cases. OCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd' Compile the MessageUtil, Test case classes using javac C:\TestNG_WORKSPACE>javac MessageUtil.java TestJunit.java Now, run the Test Runner which will run test cases defined in provided Test Case class. C:\TestNG_WORKSPACE>java -cp "C:\TestNG_WORKSPACE" org-testng.TestNG testng.xm1 \Verify the output. testPrintMessage test case will be passed. Inside testPrintMessage() Manisha Inside testSalutationMessage() Hi Manisha Suite1 Total 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 data and state between methods. This kind of dependency is supported by TestNG as it Supports the declaration of explicit dependencies between test methods. [TestNG allows you to specify dependencies either with: + Using attributes dependsOnMethods in @Test annotations OR + Using attributes dependsOnGroups in @Test annotations. Example using attribute dependsOnMethods Create a Class public class MessageUtil { private String message; // Constructor // @paran message to be printed Public MessageUtil(String message) { this.message = message; } // prints the message public String printMessage() { ‘system .out.print1n(message) ; return message; } // add “Hil” to the message Public String salutationMessage() { message = "Hi!" + message; System .out.printIn(message); return message; } } Create Test Case Class + Create a java test class say DependencyTestUsingAnnotation java ‘+ Add test methods testPrintMessage,testSalutationMessage and initEnvironmentTest to your test class. + Add attribute dependsOnMethods = { "initEnvironmentTest’ } to the @Test annotation of testSalutationMessage method. Create the java class file name DependencyTestUsingAnnotation java in C:\ > TTestNG_WORKSPACE import org. testng.Assert; import org. testng annotations Test; public class DependencyTestUsingAnnotation { ‘String message = "Manisha"; MessageUtil messageUtil = new MessageUtil (message); @Test public void testPrintMessage() ( system.out.printin("Inside testPrintMessage()"); message = "Manisha"; Assert assertequals(message, messageUtil.printMessage()); } @Test(dependsonMethods = { "initEnvironmentTest" }) public void testSalutationMessage() { ‘System .out.println("Inside testSalutationMessage()"); message = "Hi!" 4 "Manisha"; Assert assertEquals(message, messageUtil.salutationMessage()); } @test public void initenvironmenttest() { System.out.printin("This is initenvironmentTest"); } } Create testng.xml ‘Suite1"> ‘testi"> ependencyTestUsingAnnotation" /> Compile the MessageUtil, Test case classes using javac. :\TestNG_WORKSPACE>javac MessageUtil. java DependencyTestUsingAnnotation. java Now, run the testng.xml, which will run the testSalutationMessage method only after the execution bf initénvironmentTest method. C:\TestNG_WORKSPACE>java -cp "C:\TestNG_WORKSPACE" org-testng.TestNG testng.xm1 Verify the output. This is initenvironmenttest Inside testPrintMessage() Manisha Inside testSalutationMessage() Hi Manisha suite1 Total tests run: 3, Example using attribute dependsOnGroups You can also have methods that depend on entire groups. Let's see an example below: Create a Class Create a java class to be tested say MessageUtil.java in C:\ > TestNG_WORKSPACE public class MessageUtil { private String message; // Constructor // @paran message to be printed Public essageUtil(String message) { this.message = message; } // prints the message public String printMessage() { system .ut.printIn(message) ; return message; } // add "Hil" to the message public String salutationMessage() { message = "Hi!" + message; System .out.printIn(message); return message; } + Create a java test class say DependencyTestUsingAnnotation java ‘+ Add test methods testPrintMessage,testSalutationMessage and initEnvironmentTest to your test class and them to the group “init”, + Add attribute dependsOnMethods = { "init.*" } to the @Test annotation of testSalutationMessage method. Create the java class file name DependencyTestUsingAnnotation java in C:\ > TTestNG_WORKSPACE import org. testng.Assert; import org. testng.annotations.Test; public class DependencyTestUsingAnnotation { ‘String message = "Manisha"; MessageUtil messageUtil = new MessageUtil (message); @Test(groups = { "init" }) public void testPrintMessage() ( system.out.printin("Inside testPrintMessage()"); message = "Manisha"; Assert assertEquals(message, messageUtil.printMessage()); } @Test(dependsonGroups = { “init.*" }) public void testSalutationMessage() { system.out.printin("Inside testSalutationMessage()"); message = "Hi!" + "Manisha"; Assert .assertEquals(message, messageUtil.salutationMessage()); } @Test(groups = { "init" }) public void initenvironmenttest() { System.out.printin("This is initenvironmentTest } + In this example, testSalutationMessage is declared as depending on any group matching the regular expression "init.*", which guarantees that the methods testPrintMessage and initenvironmentTest will always be invoked before testSalutationMessage. Ifa method depended upon fails and you have a hard dependency on it aalwaysRun = false, whichisthedefault, 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 inacolorthatisneitherrednorgreeninHTML, which is important since skipped methods are not necessarily failures. Create testng.xml create a testng.xml C:\ > TestNG_WORKSPACE to execute Test cases. €:\TestNG_WORKSPACE>javac MessageUtil.java DependencyTestUsingAnnotation. java Now, run the testng.xml, which will run the testSalutationMessage method only after the execution bf initénvironmentTest method. C:\TestNG_WORKSPACE>java -cp "C:\TestNG_WORKSPACE" org.testng.TestNs testng.xml Verify the output. This is initenvironmenttest Inside testPrintMessage() Manisha Inside testSalutationMessage() Hi Manisha suite1 Total tests run: 3, Failures: 0, Skip: dependsOnGroups Vs dependsOnMethods * On using groups, we are no longer exposed to refactoring problems. as long as we don't modify the dependsOnGroups or groups attributes, our tests will keep running with the proper dependencies set up. + Whenever a new method needs to be added in the dependency graph, all we need to do is put it in the right group and make sure it depends on the correct group. We don’t need to modify any other method. PARAMETERIZED TEST ‘Another interesting feature available in TestNG is parametric testing. In most cases, you'll come across a scenario where the business logic requires a hugely varying number of tests Parameterized tests allow developer to run the same test over and over again using different Values. TestNG lets you pass parameters directly to your test methods in two different ways: + With testng.xml + With Data Providers Passing Parameters with testng.xml With this technique you define the simple parameters in the testng.xml file and then reference those parameters in source files. Let us see an example below on how to use this technique to pass parameters. Create Test Case Class + Create a java test class say ParameterizedTestl java + Add test method parameterTest to your test class. This method takes a String as input parameter. + Add the annotation @Parameters'myName" to this method. The parameter would be passed values from testng.xml which we will see in the next step. Create the java class file name ParameterizedTesti java in C:\ > TestNG_ WORKSPACE import org. testng annotations Paraneters; public class ParameterizedTesti { @Test @Paraneters(*myNane" ) public void parameterTest(String myName) { System.out.println( "Parameterized value is : " + myName); ? reate testng.xml reate a testng.xml C:\ > TestNG WORKSPACE to execute Test cases. ParameterizedTest1" /> We can also define the parameters at the level. Suppose we have defined myName at both and , levels then, in such cases regular scoping rules apply. This means that any class inside tag will see the value of parameter defined in , while the classes in the rest of the testng.xm! file will see the value defined in . Compile the Test case class using javac C:\TestNG_WORKSPACE>javac ParaneterizedTest1.java how Tun the testng.xml, which will run the parameterTest method. TestNG will try to find a arameter named myName first in the tag , and then, if it can’t find it, it searches in the suit> tag that encloses it. C:\TestNG_WORKSPACE>java -cp "C:\TestNG_WORKSPACE" org.testng.TestNG testng.xm1 erify the output. Parameterized value is : manisha Suitet Total tests run: 1, Failures: 0, Skips: 0 TestNG will automatically try to convert the value specified in testng.xm! to the type of your parameter. Here are the types supported: * String int/integer boolean/Boolean byte/Byte char/Character + 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 s read from a property file or a database, etc..., in such cases parameters fan be passed using Dataproviders. A’Data Provider is a method annotated with @DataProvider This annotation has only one string attribute: its name. If the name is not supplied, the Data Provider's name automatically defaults to the method’s name. A Data Provider returns an array of objects. Let us check out examples below of using Dataproviders. The first example is about @DataProvider using Vector, String or integer as parameter and the second example is about @DataProvider using object as parameter Example 1 Here, the @DataProvider passes Integer and Boolean as parameter. Create Java class (Create a java class PrimeNumberChecker java. This class check: this class in C:\ > TestNG_WORKSPACE the number is prime, Create public class PrimeNumberChecker { public Boolean validate(final Integer primeNumber) { for (int i = 2; i < (primeNumber / 2); it+) { if (primeNumber % i == 0) { return false; 3 } return true; Create Test Case Class + Create a java test class say ParamTestWithDataProvider! java. + Define the method primeNumbers which is defined as a Dataprovider using the annotation. This method returns array of object array. * Add test method testPrimeNumberChecker to your test class. This method takes a Integer and Boolean as input parameters. This method validates if the parameter passed is a prime number. * Add the annotation @TestdataProvider = "testl" to this method, The attribute dataProvider is mapped to "test1", Create the java class file name ParamTestWithDataProviderl java in C:\ > TestNG WORKSPACE import org. testng.Assert; import org. testng annotations Beforemethod; import org. testng.annotations.DataProvider; import org. testng annotations Test; public class ParanTestwithDataProvidert { private PrimeNumberChecker primeNumberchecker ; @ReforeMethod primeNumberchecker = new PrimeNumberchecker(); } @DataProvider (name = “testi") public static Object[][] primeNumbers() { return new Object(}[] { { 2, true }, { 22, false }, { 23, true } }; { 6, false }, { 19, true }, } // This test will run 4 times since we have 5 parameters defined OTest(dataProvider = “testi") public void testPrimeNumberChecker (Integer inputNunber, Boolean expectedResult) { system.out.printLn(inputNumber +" " + expectedResult); Assert asser tequals(expectedResult, primeNumber Checker .validate(inputNumber )) ; } Create testng.xml ‘Create a testng.xml C:\ > TestNG WORKSPACE to execute Test cases. aranTestWithDataProvider1" /> (Compile the Test case class using javac C:\TestNG_WORKSPACE>.javac ParamTestWithDataProvidert. java PrimeNumberChecker .java Now, run the testng.xml. :\TestNG_WORKSPACE>java -cp "C:\TestNG_WORKSPACE" org.testng.TestNG testng.xm1 Verify the output. 2 true 6 false 19 true 22 false 23 true suiter Total tests run: 5, Failures: 0, Skips: 0 Example 2 Here, the @DataProvider passes Object as parameter. Create Java class (create a java class Bean java, which is simple object with get/set methods, in C:\ > TTestNG_WORKSPACE. private String val; private int i; public Bean(String val, int i){ this.val=val; this. } public String getval() { return val; } public void setVal(string val) { this.val = val, } public int gett() ¢ return i; public void setz(int i) { this. } Create Test Case Class + Create a java test class say ParamTestWithDataProvider2.java. + Define the method primeNumbers which is defined as a Dataprovider using the annotation. This method returns array of object array. + Add test method testMethod to your test class. This method takes object bean as parameter. + Add the annotation @Testdatar mapped to "test" ' to this method. The attribute dataProvider is Create the java class file name ParamTestWithDataProvider2.java in C:\ > TestNG_ WORKSPACE import org. testng annotations DataProvider; import org. testng annotations Test; public class ParanTestwithDataProvider2 { @DataProvider (name = "testi") public static Object[][] primeNumbers() { return new Object[][] { { new Bean("hi I am the bean", 111) } }; } @Test(dataProvider = “testi") public void testMethod(Bean myBean) { ‘system out .println(myBean.getval() +" " + myBean.getI()); } Create testng.xml Create a testng.xml C:\ > TestNG WORKSPACE to execute Test cases. ‘Suite1"> “testi"> “ParanTestwithDataProvider2" /> Compile the Test case class using javac Now, run the testng.xml. C:\TestNG_WORKSPACE>java -cp "C:\TestNG WORKSPACE" org.testng.TestNG testng.xm1 Verify the output. hi 1 am the bean 111 suiter Total 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 refactor your existing Junit code. There's no need to worry as TestNG 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 recognize and run JUnit tests, so you can use TestNG as a runner for all your existing tests and write new tests using TestNG. All you have to do is to put JUnit library on the TestNG classpath, so it can find and use JUnit classes, change your test runner from JUnit to TestNG in Ant and then run TestNG in "mixed" mode, This way you can have all your tests in the same project, even in the same package, and start using TestNG. This approach also allows you to convert your existing JUnit tests to TestNG incrementally. 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, TestJunit.java in C:\ > TestNG WORKSPACE import org. junit.Test; import static org. testng AssertJUnit assertEquals; public class TestJunit { OTest public void testadd() { ‘String str= "Junit testing using Testne"; assertequals("Junit testing using TestNG", str); } Now, let's write the testng.xml in C:\ > TestNG_WORKSPACE which would contain the tag as follows: To execute the Junit test cases define property junit="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.runnerJUnitCore runner to run your tests. C:\TestNG_WORKSPACE>Javac TestJunit.java Now, run the testng.xml, which will run Junit test case as TestNG. :\TestNG_WORKSPACE>java -cp "C:\TestNG_WORKSPACE:C:\TestNG_WORKSPACE\1ib\junit- 4.11.jar" org, testng.TestNc testng.xm1 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 test execution, reason being it helps the user to understand the result of the test execution, point of failure, and reasons for the failure. Logging, on the other hand, is important to keep an eye on the execution flow or for debugging in case of any failures. TTestNG by default generates a different type of report for its test execution. This includes an HTML and an XML report output. TestNG also allows its users to write their own reporter and use it with TTestNG. There is also an option to write your own loggers, which are notified at runtime by TestNG. There are two main ways to generate a report with TestNG: + Listeners : For implementing a listener class, the class has to implement the org.testng.|TestListener interface. These classes are notified at runtime by TestNG when the test starts, finishes, fails, skips, or passes. + Reporters : For implementing a reporting class, the class has to implement an org.testng.!Reporter interface. These classes are called when the whole suite run ends. The object containing the information of the whole test run is passed to this class when called. The table below lists examples for different cases of reporting and logging: Custom Logging —_—This example illustrates how to write your own logger. Custom Reporter —_This example illustrates how to write your own reporter, HTML and XML This example illustrates the default HTML and XML report generated by report TestNG. nit Reports, This example illustrates the about generating Junit reports from TestNG reports. PLUG WITH ANT In this example, we will demonstrate how to run TestNG using ANT. Let's follow the given steps: Step 1: Download Apache Ant Download Apache Ant Windows apache-ant-1.8.4-bin.zip Linux apache-ant-1.8.4-bin.tar.gz Mac apache-ant-1.8.4-bin tar.gz Step 2: Set Ant Environment Set the ANT_HOME environment variable to point to the base directory location where ANT libraries is sfored on your machine. For example, We've stored Ant libraries in apache-ant-1.8.4 folder on various Operating Systems as follows. os Output Windows Set the environment variable ANT_HOME to C:\Program Files\Apache Software Foundation\apache-ant-1.8.4 Linux export ANT_HOME=/usr/local/\apache-ant-1.8.4 Mac export ANT_HOME=/Libraryapache-ant-1.8.4 Append Ant compiler location to System Path is as follows for different OS: os Output Windows Append the string ;%ANT_HOME\bin to the end 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,jar Linux testng-6.8,jar Mac testng-6.8,jar Step 4: Create Project Structure + Create folder TestNGWithAnt in C:\ > TestNG_WORKSPACE + Create folder src in C:\ > TestNG WORKSPACE > TestNGWithAnt + Create folder test in C:\ > TestNG WORKSPACE > TestNGWithAnt + Create folder lib in C:\ > TestNG_ WORKSPACE > TestNGWithAnt + Create MessageUtil class in €:\ > TestNG_WORKSPACE > TestNGWithAnt > src folder Bp | thie piece neinbe the niunn mecenme an panente. public class MessageUtil { private String message; //constructor //@paran message to be printed public MessageUtil(String message){ this.message = message; } // prints the message public void printmessage(){ system .out.printIn(message) ; return message; 3 // add "Hil" to the message public String salutationMessage(){ message = "Hi!" + message; System .out.println(message); return message; + Create TestMessageUtil class in C:\ > TestNG_WORKSPACE > TestNGWithAnt > src folder import org. testng.Assert; import org. testng. annotations. Test; public class TestMessageUtil { String message = "Manisha"; MessageUtil messageUtil = new MessageUtil (message); @Test public void testPrintMessage() { ‘system .out.printn("Inside testPrintMessage()"); Assert .assertEquals(message, messageUtil..printMessage()); } atest public void testSalutationMessage() { System .out.printIn("Inside testSalutationtessage()"); message = "Hil" + "Manisha"; Assert .asser tEquals(message, messageUtil. salutationMessage()); + Copy testng-6. Create ANT build.xm! jar in C:\ > TestNG_WORKSPACE > TestNGWithAnt > lib folder First we need define the TestNG ant task as follows: Then, we'll be using task in Ant to execute our TestNG test cases. The €:\ > TestNG_ WORKSPACE > TestNGWithAnt >\ build.xml is as follows: “clean"> S{testdir)" Lasspath.test"> testng.xm1"/> Run the following ant command. C:\TestNG_WORKSPACE\TestNGwi thant>ant Verify the output. test [testng] [Testne] Running [testng] — C:\TestNG_WORKSPACE\TestNGWithAnt\src\testng .xm1. [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: © [testng] [testng] BUILD SUCCESSFUL Total 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.ora os Archive name Windows testng-6.8,jar ux testng-6.8,jar Mac testng-6.8,jar Assume you copied above JAR file in Step 2: Set Eclipse environment + Open eclipse -> right click on project and click on property > Build Path > Configure Build Path and add the testng-6.8 jar in the libraries using Add External Jar button 1B Properiestor estont =e eee ‘ypefitertot Jove Build Path @e-9+4 Resource : eS ——— Bearinfe Path @ Source [1G Projects) HA Linrasec | By Order and Expo butuers IaRs and clas fader onthe ul path Eee (i joni-sanger- CUNT ae) Boosle AIRE Stem iby Gd 0.2) ——— HTML CodeFormetter ‘Aid Exteel Jan Inebuisan aduioay—_] dove Code Sle Ti ee ole bea Compile ‘a Eel Chee folder SE |S Code Formater pao Project Natures Project References unDebug Sexings TaskResositon’ Task Tage Yalestion CK) Eciress + We assume that your eclipse has inbuilt TestNG plugin if it is not available then please get the latest version using the update site: © Inyour eclipse IDE select Help / Software updates / Find and Install. * Search for new features to install * 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 click Next. © Eclipse will then guide you through the process. Now. vour aclince ic ready far the davelanmant of Ta

You might also like