Choosing a Test Framework

There are many options out there when it comes to test frameworks. When working with Java, JUnit and TestNG are the most used ones. We recommend TestNG over JUnit because of the following reasons:

  • TestNG is inspired by JUnit, which makes it a better and improved version of it (that is why NG stands for Next Generation)
  • TestNG overcomes some of the limitations of JUnit and provides you with a wider set of features than JUnit
  • TestNG is a little more user-friendly than JUnit
  • Starting on IntelliJ version 7, TestNG is bundled in the IDE, so no extra plugins are needed

With this in mind, let's take a look at TestNG's most important features:

  • Annotations: TestNG provides annotations that can be added to a test. These annotations can establish things like order execution of tests, dependencies between tests, and enabling/disabling tests, among others. We will take a deeper look at this feature later in this chapter.
  • testng.xml: TestNG is mainly configured through the testng.xml file (this is its name by default, but it can be changed). The testng.xml file is used to:
    • Create tests suites
    • Pass parameters to test methods or classes
  • Groups: A test can be conformed by many methods. With groups, TestNG allows you to organize methods into groups to which we can also apply annotations.
  • Test Units and Suite Tests: Besides groups, TestNG also allows us to create and execute Test Units (the testing of a single functionality or unit case) and Suite Tests (a combination of Test Units, even from different systems).
  • Dependent methods and groups: Unlike JUnit, TestNG allows us to execute a
    test method or a group after any given test method.
  • Multithreaded execution: TestNG allows parallel test execution.
  • In-built reporting: By default, TestNG creates HTML and XML reports after the execution of a test. There is also the possibility of creating customized reports.

The TestNG execution process follows very simple steps:

    • Test cases are composed by using Selenium code, just as we have learned so far in this book. We must be careful in abstracting the business logic of the test with the right methods.
    • TestNG annotations are inserted in the test cases.
    • Configuration of the test is set up on the testng.xml file.
    • The TestNG engine executes the test cases and outputs HTML reports with the results of the tests.

We are now able to understand what TestNG is and its main advantages over other test frameworks, such as JUnit.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.157.186