testng

14
TestNG 1. What is TestNG? TestNG is an open source automation testing framework where NG of TestNg stands for Next Generation. TestNG is a testing framework inspired from JUnit and NUnit but introducing some new functionality that make it more powerful and easier to use. TestNG eliminates most of the limitations of JUnit and gives the developer the ability to develop more flexible and powerful tests with the help of easy annotations, grouping, sequencing & parameterising. 2. Why do we need TestNG in Selenium? Because Selenium has no native mechanism for generating test reports. TestNG can generate test reports based on Selenium test results and these reports are in readable format. Uncaught exceptions are automatically handled by TestNG without terminating the tests, these exceptions are reported as failed steps in the report. There is no more need for a static main method in our tests. The sequence of actions is regulated by easy-to-understand annotations that do not require methods to be static. 3. What are the features of TestNG? Annotations are easier to understand. Test cases can be grouped more easily. Parallel testing is possible. TestNG has built in support for report generation (HTML and XML Reports). TestNG has built in logging facility (by using Reporter class). TestNG uses more Java and OO features. Supports Dependent test methods. Load testing is possible using TestNG.

Upload: chakri406k

Post on 22-Nov-2015

23 views

Category:

Documents


0 download

DESCRIPTION

TestNG questions

TRANSCRIPT

TestNG

1. What is TestNG? TestNG is an open source automation testing framework whereNGof TestNg stands for Next Generation. TestNG is a testing framework inspired from JUnit and NUnit but introducing some new functionality that make it more powerful and easier to use. TestNG eliminates most of the limitations of JUnit and gives the developer the ability to develop more flexible and powerful tests with the help of easy annotations, grouping, sequencing & parameterising.

2. Why do we need TestNG in Selenium? Because Selenium has no native mechanism for generating test reports. TestNG can generate test reports based on Selenium test results and these reports are in readable format. Uncaught exceptions are automatically handled by TestNG without terminating the tests, these exceptions are reported as failed steps in the report. There is no more need for a static main method in our tests. The sequence of actions is regulated by easy-to-understand annotations that do not require methods to be static.

3. What are the features of TestNG? Annotations are easier to understand. Test cases can be grouped more easily. Parallel testing is possible. TestNG has built in support for report generation (HTML and XML Reports). TestNG has built in logging facility (by using Reporter class). TestNG uses more Java and OO features. Supports Dependent test methods. Load testing is possible using TestNG. Flexible plug-in API. Support for multi threaded testing. Supports testing integrated classes (e.g., by default, no need to create a new test class instance for every test method). Separate compile-time test code from run-time configuration/data info. Flexible runtime configuration.

4. What are the advantages of TestNG over JUnit? In JUnit we have to declare @BeforeClass and @AfterClass as static methods, which is a constraint where as in TestNG there is no such constraint like this. Additional Levels of annotations setUp/tearDown level are available in TestNG like @BeforeSuite/AfterSuite, @BeforeTest/AfterTest and @BeforeGroup/AfterGroup. No Need to extend any class in TestNG. In JUnit 4, the annotation naming convention is a bit confusing, e.g Before, After and Expected, we do not really understand what is Before and After do, and what we Expected from test method? TestNG is easier to understand, it uses BeforeMethod, AfterMethod and ExpectedException instead. There is no method name constraint in TestNG as in JUnit. You can give any name to the test methods in TestNG. In TestNG we can tell the test that one method is dependent on another method where as in JUnit this is not possible. In JUnit each test is independent of another test. Grouping of test cases is available in TestNG whereas the same is not available in JUnit. Execution can be done based on Groups. For ex. If you have defined many cases and segregated them by defining 2 groups as Sanity and Regression. Then if you only want to execute the Sanity cases then just tell TestNG to execute the Sanity and TestNG will automatically execute the cases belonging to the Sanity group.

5. What are the benefits of using TestNG? TestNG allows us to execute test cases based on group. In TestNG Annotations are easy to understand. Parallel execution of Selenium test cases is possible in TestNG. Three kinds of report generated Order of execution can be changed Failed test cases can be executed Without having main function we can execute the test method. An xml file can be generated to execute the entire test suite. In that xml file we can rearrange our execution order and we can also skip the execution of particular test case.

6. What are annotations?Annotations are like meta-tags that you can add to your code and apply them to methods or in class. The annotation gives us information about test methods, which methods are going to run before & after test methods, which methods run before & after all the methods, which methods or class will be ignore during execution.

7. What are the Annotations available in TestNG?@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.@Test: The annotated method is a part of a test case.

8. What are the benefits of using annotations in TestNG? 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 (such as Test Case, for JUnit 3).

9. What are the different ways in which TestNG can be invoked? You can invoke using Eclipse IDE. Invoke with ANT build tool. Invoke from the command line.

10. Give an example to invoke TestNG from command line.Assume 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 ...]

11. What are the basic steps involved in writing TestNG tests?Writing a test in TestNG basically involves following steps: 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.

12. What is testng.xml file used for? testng.xml file captures entire testing in XML. This file makes it easy to describe all your test suites and their parameters in one file, which you can check in your code repository or e-mail to coworkers. It also makes it easy to extract subsets of your tests or split several runtime configurations (e.g., testng-database.xml would run only tests that exercise your database).

In TestNG framework, we need to createtestng.xmlfile to create and handle multiple test classes. This is thexml file where you will configure your test run, set test dependency, include or exclude any test, method, class or package and set priority etc.

13. What is Test Suite? Test suite is a collection of unit test cases that are intended to test a behavior or set of behaviors of software program and run it together. Running a set of test cases together is call executingaTest Suite. Those test cases can be dependent to each other or may have to be executed in a certain order. 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.

14. How can you disable/ignore a test in TestNG?By using @Test(enabled = false) annotation, it helps to disable the test case which you want to ignore.To use two or more parameters in a single annotation, separate them with a comma:@Test(priority = 3, enabled = false)

15. How can you Sequencing & Prioritizing test cases in TestNG?You need to use the priorityparameter, if you want the methods to be executed in your order.Parametersare keywords that modify the annotations function.TestNG will execute the @Test annotation with the lowest priority value up to the largest.Ex: @Test(priority = 0)public void One() {System.out.println("This is the Test Case number One");}@Test(priority = 1)public void Two() {System.out.println("This is the Test Case number Two");}@Test(priority = 2)public void Three() {System.out.println("This is the Test Case number Three");}@Test(priority = 3)public void Four() { System.out.println("This is the Test Case number Four");}

16. What is group test?Groups are one more annotation of TestNG which can be used in the execution of multiple tests.Ex: Lets say you have hundred tests of class vehicle and in it ten method of car, ten method of scooter and so on. You probably like to run all the scooter tests together in a batch. And you want all to be in a single test suite. With the help of grouping you can easily overcome this situation.Group them separately with using (groups = { Group Name })

17. How to you specify a group in testng.xml?- 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.

18. What is exception test in TestNG?- @Test Annotation provides an attribute expectedExceptions allowing the user to specify the type of exceptions that are expected to be thrown by a test method during execution.- expectedExceptions supports multiple values so you can verify different exceptions.- If the exception thrown by the test method is not part of the user entered list, the test method will be marked as failed.- The expectedExceptions parameter is used along with @Test annotation. Now, let's see @Test(expectedExceptions = exception class) in action.

19. What are Parameters in TestNG?- Parameters are keywords that modify the annotation's function.- Parameters require you to assign a value to them. You do this by placing a "=" next to them, and then followed by the value.- Parameters are enclosed in a pair of parentheses which are placed right after the annotation like the code snippet shown below.

20. What is 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 ORUsing attributes dependsOnGroups in @Test annotations.

21. What is difference between dependsOnGroups and dependsOnMethods?Following are the differences: On using groups, we are no longer exposed to refactoring problems. As long as we dont 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 dont need to modify any other method.

22. What 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 developers to run the same test over and over again using different values.- Everybody knows the importance of Parameterization in testing and in automation testing. It allows us to automatically run a test case multiple times with different input and validation values.- TestNG again gives us another interesting feature called TestNG Parameters. TestNG lets you pass parameters directly to your test methods with your testng.xml.- TestNG lets you pass parameters directly to your test methods in two different ways: With testng.xml With Data Providers

23. How do you pass parameters with testng.xml?We define the simple parameters in the testng.xml file and then reference those parameters in source files. @Test @Parameters({ "sUsername", "sPassword" }) public void test(String sUsername, String sPassword) { driver.findElement(By.id("log")).sendKeys(sUsername); driver.findElement(By.id("pwd")).sendKeys(sPassword); }In XML

24. What does it mean to pass parameters using DataProviders?- When 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 such cases parameters can be passed using Dataproviders. - A Data Provider is a method annotated with @DataProvider(name = dataprovider name).- This annotation has only one string attribute: its name. If the name is not supplied, the Data Providers name automatically defaults to the methods name. - A DataProvider method returns array of object array.

25. What are Reporters in TestNG? How logging performed in TestNG?- TestNG also gives us the logging facility for the test. - We need the information which helps the User to understand the test steps or any failure during the selenium test case execution. - With the help of TestNG Logs it is possible to enable logging during the Selenium test case execution.- In selenium there are two types of logging. High level logging and Low level logging.- In low level logging you try to produce logs for the every step you take or every action you make in your automation script.- In high level logging you just try to capture main events of your test.

26. What are Asserts in TestNG? Asserts are the most popular and frequently used methods while creating Selenium Scripts. With this Asserts we can put various checkpoints in the test. In selenium there will be many situations in the test where you just like to check the presence of an element. All you need to do is to put an assert statement on to it to verify its existence. Note: Assert true statement fails the test and stop the execution of the test, if the actual output is false. Assert.assertFalse() works opposite of Assert.assertTrue(). It means that if you want your test to continue only if when some certain element is not present on the page. You will use Assert false, so it will fail the test in case of the element present on the page.Assert.assertEquals() - It also works the same way like assert true and assert fail. It will also stop the execution, if the value is not equal and carry on the execution, if the value is equal.

27. How Multi Browser testing performed using Selenium TestNG? Testing your website with multiple combinations of browsers is known as Cross Browser testing or multi Browser testing. It takes a considerable time to test everything on every browser and when we have used automation to reduce the testing efforts then why dont we perform the multi-browser testing using automation. TestNG gives us functionality to perform same test on different browsers in a simple and easy way. TestNg will execute the tests one by one.In Test method @Parameters("browser") @BeforeClass // Passing Browser parameter from TestNG xml public void beforeTest(String browser) { }In testng.xml file

28. How parallel testing performed using Selenium TestNG?- By using the feature provided by TestNG for Parallel Executions we will execute test in multiple browsers simultaneously.- By changing parallel attribute to tests in the above used xml and give a run again. This time you will notice that your both browsers will open almost simultaneously and your test will run in parallel.

29. How can you run the JUnit tests using TestNG?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. Also define property junit="true" in the tag of the testng.xml.

30. What are different ways in which you can generate the reports of TestNg results?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.ITestListener interface. These classes are notified at runtime by TestNG when the test starts, finishes, fails, skips, or passes.

Types of ListenersThere are many types of listeners available in TestNG for exampleIAnnotationTransformer, IAnnotationTransformer2,IConfigurable, IConfigurationListener, IConfigurationListener2, IExecutionListener, IHookable, IInvokedMethodListener, IInvokedMethodListener2, IMethodInterceptor, IReporter,ISuiteListener, ITestListener.ISuiteListener: It has two methods in it onStart() & onFinish().Whenever a class implements this listener, TestNG guarantees the end-user that it will invoke the methods onStart() and onFinish() before and after running a TestNG Suite. So before TestNG picks up your suite for execution, it first makes a call to onStart() method and runs whatever has been scripted in this method. In a similar way, it again makes a call to onFinish() method after a suite has been run.ITestListener: The working of this listener is also exactly the same as ISuiteListerner but the only difference is that it makes the call before and after the Test not the Suite. It has seven methods in it.onFinish(): Invoked after all the tests have run and all their Configuration methods have been called.onStart(): Invoked after the test class is instantiated and before any configuration method is called.onTestFailure(ITestResult result): Invoked each time a test fails.onTestSkipped(ITestResult result): Invoked each time a test is skippedonTestStart(ITestResult result): Invoked each time before a test will be invoked.onTestSuccess(ITestResult result): Invoked each time a test succeeds. IInvokedMethodListener: The working of this listener is also exactly the same as ISuiteListerner & ITestListerner and the only difference is that it makes the call before and after every Method. It has only two methods in it.afterInvocattion(): Invoke after each methodbeforeInvocation(): Invoke before each methodIReporter: Implement this listener within your class, if you want to customize the TestNG reports (for e.g., you might want your test reports to be available as an excel sheet or a word document or even a pdf for that matter). This would be the last of the calls that TestNG makes before closing your execution.

31. In TestNG I have some tests Test1-Test2-Test3-Test4-Test5I want to run my executionorder is Test5-Test1-Test3-Test2-Test4.Howdo you set the execution order can you explainfor that?Use priority parameter in @test annotation or TestNG annotations.

32. Detail about TestNG Test Output folder.It is the directory where reports are generated. Every time tests run in a suite, TestNGcreates index.html andother files in the output directory.

33. What is Error Collector in TestNG?What is its use?This class allows the collection of errors during the process of retrieving thetest data for the test method parameters