top of page

Group

Public·68 members

Testng Tutorial Pdf



  • Writing a test is typically a three-step process:Write the business logic of your test and insert TestNG annotations in your code.

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

  • Run TestNG.

  • You can find a quick example on the Welcome page.The concepts used in this documentation are as follows:A suite is represented by one XML file. It can contain one or more tests and is defined by the tag.

  • A test is represented by and can contain one or more TestNG classes.

  • A TestNG class is a Java class that contains at least one TestNG annotation. It is represented by the tag and can contain one or more test methods.

  • A test method is a Java method annotated by @Test in your source.

  • A TestNG test can be configured by @BeforeXXX and @AfterXXX annotations which allows to perform some Java logic before and after a certain point, these points being either of the items listed above.The rest of this manual will explain the following:A list of all the annotations with a brief explanation. This will give you an idea of the various functionalities offered by TestNG but you will probably want to consult the section dedicated to each of these annotations to learn the details.

  • A description of the testng.xml file, its syntax and what you can specify in it.

  • A detailed list of the various features and how to use them with a combination of annotations and testng.xml.

AnnotationsHere is a quick overview of the annotations available in TestNG along with their attributes.@BeforeSuite@AfterSuite@BeforeTest@AfterTest@BeforeGroups@AfterGroups@BeforeClass@AfterClass@BeforeMethod@AfterMethodConfiguration information for a TestNG class:@BeforeSuite: The annotated method will be run before all tests in this suite have run.@AfterSuite: The annotated method will be run after all tests in this suite have run. @BeforeTest: The annotated method will be run before any test method belonging to the classes inside the tag is run.@AfterTest: The annotated method will be run after all the test methods belonging to the classes inside the tag have run.@BeforeGroups: 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.@AfterGroups: 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.@BeforeClass: The annotated method will be run before the first test method in the current class is invoked.@AfterClass: The annotated method will be run after all the test methods in the current class have been run. @BeforeMethod: The annotated method will be run before each test method.@AfterMethod: The annotated method will be run after each test method.Behaviour of annotations in superclass of a TestNG class




testng tutorial pdf



testng.xml You can specify package names instead of class names:testng.xml In this example, TestNG will look at all the classes in the package test.sample and will retain only classes that have TestNG annotations.


You can also define new groups inside testng.xml and specify additional details in attributes, such as whether to run the tests in parallel, how many threads to use, whether you are running JUnit tests, etc... By default, TestNG will run your tests in the order they are found in the XMLfile. If you want the classes and methods listed in this file to berun in an unpredictable order, set the preserve-orderattribute to falsetestng.xml Please see the DTD for a complete list of the features, or read on.


Assuming that you have TestNG in your class path, the simplest way to invoke TestNG is as follows:java org.testng.TestNG testng1.xml [testng2.xml testng3.xml ...]You need to specify at least one XML file describing the TestNG suite you are trying to run. Additionally, the following command-line switches are available:


This documentation can be obtained by invoking TestNG without any arguments.You can also put the command line switches in a text file, say c:\command.txt, and tell TestNG to use that file to retrieve its parameters: C:> more c:\command.txt -d test-output testng.xml C:> java org.testng.TestNG @c:\command.txtAdditionally, TestNG can be passed properties on the command line of the Java Virtual Machine, for examplejava -Dtestng.test.classpath="c:/build;c:/java/classes;" org.testng.TestNG testng.xmlHere are the properties that TestNG understands: System properties Property Type Documentation testng.test.classpath A semi-colon separated series of directories that contain your test classes. If this property is set, TestNG will use it to look for your test classes instead of the class path. This is convenient if you are using the package tag in your XML file and you have a lot of classes in your classpath, most of them not being test classes. Example:java org.testng.TestNG -groups windows,linux -testclass org.test.MyTestThe ant task and testng.xml allow you to launch TestNG with more parameters (methods to include, specifying parameters, etc...), so you should consider using the command line only when you are trying to learn about TestNG and you want to get up and running quickly.Important: The command line flags that specify what tests should be run will be ignored if you also specify a testng.xml file, with the exception of -includedgroups and -excludedgroups, which will override all the group inclusions/exclusions found in testng.xml.Test methods, Test classes and Test groupsTest methodsTest methods are annotated with @Test. Methods annotated with @Test that happen to return a value will be ignored, unless you set allow-return-values to true in your testng.xml:orTest groupsTestNG allows you to perform sophisticated groupings of test methods. Not only can you declare that methods 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 (or regular expressions) 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 and can be found either under the or tag. Groups specified in the tag apply to all the tags underneath. Note that groups are accumulative in these tags: if you specify group "a" in and "b" in , then both "a" and "b" will be included.For example, it is quite common to have at least two categories of tests


A simple way to solve this problem is to create a group called "broken" and make these test methods belong to it. For example, in the above example, I know that testMethod2() is now broken so I want to disable it:Java@Test(groups = "checkintest", "broken" )public void testMethod2() All I need to do now is to exclude this group from the run:testng.xml This way, I will get a clean test run while keeping track of what tests are broken and need to be fixed later.


  • There are three ways to set these parameters The testng.xml file

  • Programmatically

  • Java system properties

Parameters from testng.xmlIf you are using simple values for your parameters, you can specify them in your testng.xml:Java@Parameters( "first-name" )@Testpublic void testSingleString(String firstName) System.out.println("Invoked testString " + firstName); assert "Cedric".equals(firstName);In this code, we specify that the parameter firstName of your Java method should receive the value of the XML parameter called first-name. This XML parameter is defined in testng.xml:testng.xml The same technique can be used for @Before/After and @Factory annotations:@Parameters( "datasource", "jdbcDriver" )@BeforeMethodpublic void beforeTest(String ds, String driver) m_dataSource = ...; // look up the value of datasource m_jdbcDriver = driver;This time, the two Java parameter ds and driver will receive the value given to the properties datasource and jdbc-driver respectively. Parameters can be declared optional with the Optional annotation:@Parameters("db")@Testpublic void testNonExistentParameter(@Optional("mysql") String db) ... If no parameter named "db" is found in your testng.xml file, your test method will receive the default value specified inside the @Optional annotation: "mysql".The @Parameters annotation can be placed at the following locations:


Notes:The XML parameters are mapped to the Java parameters in the same order as they are found in the annotation, and TestNG will issue an error if the numbers don't match. Parameters are scoped. In testng.xml, you can declare them either under a tag or under . If two parameters have the same name, it's the one defined in that has precedence. This is convenient if you need to specify a parameter applicable to all your tests and override its value only for certain tests.


  • Specifying parameters in testng.xml might not be sufficient if you need to pass complex parameters, or parameters that need to be created from Java (complex objects, objects read from a property file or a database, etc...). In this case, you can use a Data Provider to supply the values you need to test. A Data Provider is a method on your class that returns an array of array of objects. This method is annotated with @DataProvider:Java//This method will provide data to any test method that declares that its Data Provider//is named "test1"@DataProvider(name = "test1")public Object[][] createData1() return new Object[][] "Cedric", new Integer(36) , "Anne", new Integer(37), ;//This test method declares that its data should be supplied by the Data Provider//named "test1"@Test(dataProvider = "test1")public void verifyData1(String n1, Integer n2) System.out.println(n1 + " " + n2); will printCedric 36Anne 37A @Test method specifies its Data Provider with the dataProvider attribute. This name must correspond to a method on the same class annotated with @DataProvider(name="...") with a matching name.By default, the data provider will be looked for in the current test class or one of its base classes. If you want to put your data provider in a different class, it needs to be a static method or a class with a non-arg constructor, and you specify the class where it can be found in the dataProviderClass attribute:StaticProvider.javapublic class StaticProvider @DataProvider(name = "create") public static Object[][] createData() return new Object[][] new Object[] new Integer(42) ; public class MyTest @Test(dataProvider = "create", dataProviderClass = StaticProvider.class) public void test(Integer n) // ... The data provider supports injection too. TestNG will use the test context for the injection.The Data Provider method can return one of the following types:An array of array of objects (Object[][]) where the first dimension's size is the number of times the test method will be invoked and the second dimension size contains an array of objects that must be compatible with the parameter types of the test method. This is the case illustrated by the example above.

  • An Iterator. The only difference with Object[][] is that an Iterator lets you create your test data lazily. TestNG will invoke the iterator and then the test method with the parameters returned by this iterator one by one. This is particularly useful if you have a lot of parameter sets to pass to the method and you don't want to create all of them upfront.An array of objects (Object[]). This is similar to Iterator but causes the test method to be invoked once for each element of the source array.

  • An Iterator>. Lazy alternative of Object[]. Causes the test method to be invoked once for each element of the iterator.

It must be said that return type is not limited to Object only thus MyCustomData[][] or Iteratorare also possible. The only limitation is that in case of iterator its parameter type can't be explicitly parametrized itself.Here is an example of this feature:@DataProvider(name = "test1")public Iterator createData() return new MyIterator(DATA); Using MyCustomData[] as a return type@DataProvider(name = "test1")public MyCustomData[] createData() return new MyCustomData[] new MyCustomData() ;Or its lazy option with Iterator@DataProvider(name = "test1")public Iterator createData() return Arrays.asList(new MyCustomData()).iterator();Parameter type (Stream) of Iterator can't be explicitly parametrized@DataProvider(name = "test1")public Iterator createData() return Arrays.asList(Stream.of("a", "b", "c")).iterator();If you declare your @DataProvider as taking a java.lang.reflect.Methodas first parameter, TestNG will pass the current test method for thisfirst parameter. This is particularly useful when several test methodsuse the same @DataProvider and you want it to return differentvalues depending on which test method it is supplying data for.For example, the following code prints the name of the test method inside its @DataProvider:@DataProvider(name = "dp")public Object[][] createData(Method m) System.out.println(m.getName()); // print test method name return new Object[][] new Object[] "Cedric" ;@Test(dataProvider = "dp")public void test1(String s) @Test(dataProvider = "dp")public void test2(String s) and will therefore display:test1test2Data providers can run in parallel with the attribute parallel:@DataProvider(parallel = true)// ...Parallel data providers running from an XML file share the same pool of threads, which has a size of 10 by default. You can modify this value in the tag of your XML file:... If you want to run a few specific data providers in a different thread pool, you need to run them from a different XML file.Parameters from System PropertiesTestNG can be passed parameters on the command line of the Java Virtual Machine using system properties (-D). Parameterspassed in this way are not required to be pre-defined in testng.xml, but will override any parameters defined there.java -Dfirst-name=Cedrick -Dlast-name="von Braun" org.testng.TestNG testng.xmlThe Java system property variable is a string with no spaces that represents the name of the property. The value variable isa string that represents the value of the property. If the value is a string with spaces, then enclose it in quotation marks.Note: In TestNG 6.x parameters defined in testng.xml could not be overwritten by system properties


About

Welcome to the group! You can connect with other members, ge...
Group Page: Groups_SingleGroup
bottom of page