PAGE 2 : WRITING FIRST TESTS IN TestNG

Writing a test in TestNG basically involves following steps:
  1. Create a class and write the business logic of your test and insert TestNG annotations in your code.
  2. 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.
  3. Run TestNG.
Step-1: Create a class and write the business logic of your test and insert TestNG annotations in your code.















You will see an error message as : Test cannot be resolved to a type when you hover to the error message.
So how to resolve this issue?
Follow the steps as under:-



Hover and click on  to the 'Add TestNG Library'.TestNG library will be added to your project.
Note for each project TestNG library needs to be added to use the annotation of TestNG.


























Again hover to the Test annotation , you will find an option as 'Import Test' {TestNG annotations}

Note: For use of TestNG annotations always use (org.testng.annotations)
Click on to the 'Import Test' option of TestNG and save the class .
You will find that the error is disappeared.




















Step-2: Create a TestNG.xml in your project.
  • Steps to create a TestNg.xml 
  • Right click on to your package ,move to new and then select option as 'Other'.





















  • After clicking on to the 'Other' option a 'Select a Wizard' dialog will open .
  • Enter search text as : xml
  • Entering search text will display options to user related to xml .
  • Select option as : XML File.

  • Double click on to the displayed XML File.It will open a 'New XML File' dialog.
  • Update the File Name / we can keep the same name too.






























Click on to the 'Finish' button.
Clicking on to the Finish Button will create an xml file.















Make sure the name of the package and class should be exact as entered in the <class name="com.package1.TestNg1" /> ,else would throw an exception as "class not found" 





















Step-3: To run TestNG.xml 
Go to the created TestNg.xml , right click hover to "Run As" option and click on to the option "TestNg Suite".































Clicking on to the TestNG Suite will execute the test and will display the output.
We can verify the Output both in TestNG: Results of running suite and in Console.

Results of running suite:-



















In Console:-
















  • Now right click on to the Project and go to properties(last option) and click on to it it.
































You will find the location of your Project























Copy and location.
Go to Start and Paste the location there .
Click on to the test output folder 


    • Click on to the index.html














    • Double click on to the index.html .
    • Report will be opened in a browser which will display the current status of the report.
    • Make sure the result gets refreshed each time when you execute your test.
    • If we want to have all the reports of the test run then save the index.html in a separate folder.



















    TEST SUITE : A test suite is a collection of test cases intended to test a behavior or a 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. It also allows flexible configuration of the tests to be run. A suite can contain one or more tests and is defined by the <suite> tag.

    <suite> is the root tag of your testng.xml. It describes a test suite, which in turn is made of several <test> sections.

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

    <suite name="Suite1">
      <test name="exampletest1">
        <classes>
           <class name="Test1" />
        </classes>
      </test>
      
      <test name="exampletest2">
        <classes>
           <class name="Test2" />
        </classes>
      </test>


    </suite>  




    No comments:

    Post a Comment

    About Me

    My photo
    You can reach me out at : jimmiamrit@gmail.com

    Total Pageviews