TestNG Introduction & Setup

What is TestNG?
Definition of TestNG as per its documentation is as follows:
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 means Next Generation. TestNG is similar to JUnit (especially JUnit 4), but it is not a JUnit extension. It is inspired by JUnit. It is designed to be better than JUnit, especially when testing integrated classes. The creator of TestNG is Cedric Beust.
TestNG Features
  • Supports annotations.
  • TestNG uses more Java and OO features.
  • Supports testing integrated classes (e.g., by default, no need to create a new test class instance for every test method).
  • Separates compile-time test code from run-time configuration/data info.
  • Flexible runtime configuration.
  • Introduces ‘test groups’.
  • Supports Dependent test methods, parallel testing, load testing, and partial failure.

    Benefits of TestNG

    There are number of benefits but from Selenium perspective, major advantages of TestNG;
    1. It gives the ability to produce HTML Reports of execution
    2. Annotations made testers life easy
    3. Test cases can be Grouped & Prioritized more easily
    4. Parallel testing is possible
    5. Generates Logs
    6. Data Parameterization with DP provider
    TestNG Environment :
      Make sure that java version about 1.6 installed in your machine if not install from here
    http://www.oracle.com/technetwork/java/javase/downloads/index.html
     Java Environment :
    Next we need to set the JAVA environment that point to the base directory where JAVA installed
    OSOutput
    WindowsSet the environment variable JAVA_HOME to C:\Program Files\Java\jdk1.8.0_25.
    LinuxExport JAVA_HOME=/usr/local/java-current.
    MacExport JAVA_HOME=/Library/Java/Home.
    Eclipse :
    Make sure that eclipse is installed in your machine else download from here https://eclipse.org/downloads/

     Once installed eclipse now download / Configure TestNG
    Open eclipse and go to help->Install new software 

    In install new software popup enter the url( http://beust.com/eclipse) and install TestNG as instructed 
    Once done restart eclipse and now check whether TestNG installed or not 
    Within eclipse right click and should be able to view TestNG option as shown

    How to write Test Cases using TestNG.

    Writing a test in TestNG is quite simple and basically involves following steps:
    Step 1 – Write the business logic of the test
    Step 2 – Insert TestNG annotations in the code
    Step 3 – Add the information about your test (e.g. the class names, methods names, groups names etc…) in a testng.xml file
    Step 4 – Run TestNG

    Before this we need to understand what are annotations;
    1. It identifies the methods it is interested in by looking up annotations. Hence method names are not restricted to any pattern or format.
    2. We can pass additional parameters to annotations like (enabled=true etc..)
    3. Annotations are strongly typed, so the compiler will flag any mistakes right away.
    Annotations 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.

      

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.