Wednesday, February 27, 2013

Integrating TestNG with Eclipse

Integrating TestNG with Eclipse


  1. You should have installed Eclipse .
  2. Goto help>Install New Software
  3. Open Browser
  4. Goto Google >type install testNG  or Goto http://testng.org/doc/download.html
  5. Copy the link corresponding to Eclipse Version .This should look something like below .
Example
For Eclipse 3.4 and above, enter http://beust.com/eclipse.
For Eclipse 3.3 and below, enter http://beust.com/eclipse1.

6.  Paste the same in "Work With" edit field on opened "Avaliable Software"window .Press Add button.
7. Check the Check box TestNg
8. Click next....next blah!blah! ...
9.  Restart Eclipse and TestNg will be installed

---------------------------------------------------

Running TestNG


1.      Eclipse > New>Project>Java Project >Enter name ...  blah,blah ..(how you create your normal Java projects)
2.      Project created
3.      Go to project > src > Delete your default package
4.      Create a new package in src (Ex : tester)
5.      Create a new class (Ex:"starter") inside "tester" package
6.      Add below code inside tester.java
     package starter;
public class starter {



@Test

public void testing()

{

System.out.print("hi");

}



}
//Note: All Classes and Methods has to be of "Public" access identifier.

7.      Add all dependent libraries
8.      RtClick on src folder
9.      New>Other>XML>XML File
10.  Create file “Testng.xml”
11.  Open Testng.xml, Below screen click on “Source”
12.  Add following Code:

<suite name="some name">

<test name = "other name">



<parameter name ="var" value="hi"/>



<classes>

<class name = "tester.starter" />

</classes>



</test>

</suite>  

Now Right Click on TestNg.xml file and Runas "TestNG Suite"

Class File





XML File





Examples :
If your class is just inside a default pacjake you can just write following in Testng.xml:
<suite name="TestNg_test">       
       <test name="assert1">>        
              <classes>
                     <class name="your_class" />                     
              </classes>
       </test>       
</suite>





Following are the links which give you know how on integration of Selenium with TestNG:-


http://qtpselenium.com/selenium-tutorial
http://selftechy.com/2012/01/09/setting-up-testng-with-eclipse

Selenium :Assersions (Skipping @Test after a Step Fails )

 

Assersions

 

  • assertTrue will fail if the second parameter evaluates to false (in other words, it ensures that the value is true). 
  • assertFalse does the opposite.
Example:
assertTrue("This will succeed.", true);
assertTrue("This will fail!", false);

assertFalse("This will succeed.", false);
assertFalse("This will fail!", true);









Important Types :
  1.     Assert.assertequals("good","god");         o/p: Failed
  2.     Assert.assertTrue("Message " ,4>8 );     o/p-Failed     some message
  3.     Assert.assertFalse("Message " ,8<9 );    o/p-Failed     some message

Note : RunAs>TestNg

Advantages :
Used to report Pass and Fail

Disadvantage:
I have illustrated with the example below.

The file contains 2 @Test ,one having "Assert" statement .If the control encounters failure from Assert statement,then the control will report failure, Skip all the following lines in the current test and jump to the next test.

Overcome :
Use Listerners


//------------------------------------------------------------------------------------------------
package package_name;

import org.testng.Assert;

import org.testng.SkipException;

import org.testng.annotations.BeforeMethod;

public class Test

{

@org.testng.annotations.Test

public void test1()

{

Assert.assertEquals(1, 2);

System.out.println("Testing 1");

}

@org.testng.annotations.Test

public void test2()

{

System.out.println("Testing 2");

}

}

Selenium : Throw new SkipException


Program to skip all the tests in a file using "Throw new SkipException". The below file contains 2 @Test which is test1 ,test2 and we have @BeforeMethod. By inserting "Throw new SkipException" statement in the @BeforeMethod all the test can be skipped while executing through TestNG.


Example :

package package_name;

import org.testng.SkipException;
import org.testng.annotations.BeforeMethod;


public class Test
{

@BeforeMethod
public void Method()
{
System.out.println("BeforeMethod");
throw new SkipException ("Skipping test");//Instruction to Skip
}

@org.testng.annotations.Test
public void test1()
{
System.out.println("Testing 1");
}

@org.testng.annotations.Test
public void test2()
{
System.out.println("Testing 2");
}

}