Tuesday, March 18, 2014

Some TestNG Annotations with Attributes

In TestNg have so many attribute with the annotation we will try to learn some of them, here almost everything with example.......

enabled =Boolean value


If we set it false then method will not execute means it is not prepared for test or method not to be tested.
     
Example Code:
 
@Test(enabled = false)
    public void testMethod() {
        System.out.println("TestMethod()");
    }
@Test
    public void anotherTestMethod() {
        System.out.println("AnotherTestMethod()");
    }

OutPut:

 [TestNG] Running:
  Command line suite

AnotherTestMethod()From Group1

===============================================
Command line suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================

 

timeOut  = Integer value


It is required if we set the value for time to executed if it required more time than defined then test failed elase passed.
Example Code:
    
@Test(timeOut = 1)
    public void testMethod() throws InterruptedException {
        Thread.sleep(2000);
        System.out.println("TestMethod()");
    }
@Test
    public void anotherTestMethod() {
        System.out.println("AnotherTestMethod()");
    }


OutPut:

[TestNG] Running:
  Command line suite

AnotherTestMethod()From Group1 and Group2

===============================================
Command line suite
Total tests run: 2, Failures: 1, Skips: 0
===============================================

Java Result: 1

 

groups ={"GroupName1","GroupName2",.......}


Groups attribute is used to grouping the methods.

Example Code:

@Test(groups = {"group2"})
    public void testMethod() {
        System.out.println("TestMethod() From Group1");
    }
@Test(groups = {"group1","group2"})
    public void anotherTestMethod() {
        System.out.println("AnotherTestMethod()From Group1 and Group2");
    }


OutPut:

[TestNG] Running:
  Command line suite

AnotherTestMethod()From Group1 and Group2
TestMethod() From Group1

===============================================
Command line suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================

 dependsOnGroups={"GroupName1","GroupName2" ,.....}


It's used to imply the dependency of a another group(groups). If all the methods of this group(groups) is not passed then it is skipped.

Example Code:

    /*
     * This @Test have timeout limit 1 Sec
     * but it required 2 Sec becausing using of this
     * Thread.sleep(2000);
     * So it will be failed due to timeOut
     */


    @Test(groups = {"group1"}, timeOut = 1)
    public void testMethod() throws InterruptedException {
        Thread.sleep(2000);
        System.out.println("TestMethod() From Group1");
    }

   

 /*
     * This @Test dependes on "group1"
     * and "group1" failed due to timed-out
     * So This test should be Skipped though
     * "group2" Passed.
     */


    @Test(dependsOnGroups = {"group1", "group2"})
    public void anotherTestMethod() {
        System.out.println("AnotherTestMethod()From Group1 and Group2");
    }

    @Test(groups = {"group2"})
    public void testMethodFromGP2() {
        System.out.println("TestMethod() From Group2");
    }


OutPut:

[TestNG] Running:
  Command line suite

TestMethod() From Group2

===============================================
Command line suite
Total tests run: 3, Failures: 1, Skips: 1
===============================================

Java Result: 3

 

dependsOnMethods = {"methodName1","methodName2",........}


It's used to imply the dependency of a another method(methods). If all the method/methods is not passed then it is skipped.

Example Code:
 
     /*
     * This @Test have timeOut limit 1 Sec
     * but it required 2 Sec becausing using of this
     * Thread.sleep(2000);
     * So it will be failed due to timeOut
     */

    @Test(timeOut = 1)
    public void testMethod() throws InterruptedException {
        Thread.sleep(2000);
        System.out.println("TestMethod() From Group1");
    }
    /*
     * This @Test dependes on "testMethod"
     * and "testMethod" failed due to timed-out
     * So This test should be Skipped though
     * "testMethodFromGP2" Passed.
     */


    @Test(dependsOnMethods = {"testMethod", "testMethodFromGP2"})
    public void anotherTestMethod() {
        System.out.println("AnotherTestMethod()From Group1 and Group2");
    }

    @Test
    public void testMethodFromGP2() {
        System.out.println("TestMethod() From Group2");
    }


OutPut:

[TestNG] Running:
  Command line suite

TestMethod() From Group2

===============================================
Command line suite
Total tests run: 3, Failures: 1, Skips: 1
===============================================

Java Result: 3 


priority =Integer value


It is used to maintain the sequence means which method we want to execute first we set low value (Set 0).
One thing keep in mind if a method depends on a lower priority method or depends on a group, that may hold lower priority methods then this method or methods should be run first then higher priority method.

Example Code:   

    /*
     * This Method should be executed
     * brfore anotherTestMethod()though
     * testMethod()have lower priority
     * than it but anotherTestMethod()
     * depends on testMethod()
     */


    @Test(priority = 2)
    public void testMethod() throws InterruptedException {
        Thread.sleep(2000);
        System.out.println("TestMethod() From Group1");
    }

    
    /*
     * Though it have the Higher Priority
     * than "testMethod()" but it dependes
     * on this lower priority method it
     * should be executed after "testMethod()"
     */


    @Test(dependsOnMethods = {"testMethod"},priority = 1)
    public void anotherTestMethod() {
        System.out.println("AnotherTestMethod()From Group1 and Group2");
    }


  /*
   * This TestMethod will execute First
   * Because it have the Highest Priority
   */

 
    @Test(priority = 0)
    public void testMethodFromGP2() {
        System.out.println("TestMethod() From Group2");
    }



OutPut:

[TestNG] Running:
  Command line suite

TestMethod() From Group2
TestMethod() From Group1
AnotherTestMethod()From Group1 and Group2

===============================================
Command line suite
Total tests run: 3, Failures: 0, Skips: 0
===============================================




alwaysRun = Boolean value


If it is set then it should be run in any condition it does not depends on any group or methods.

Example Code:
    /*
     * This @Test have timeout limit 1 Sec
     * but it required 2 Sec becausing using of this
     * Thread.sleep(2000);
     * So it will be failed due to timeOut
     */


    @Test(timeOut = 1)
    public void testMethod() throws InterruptedException {
        Thread.sleep(2000);
        System.out.println("TestMethod() From Group1");
    }
   

    /*
     * This @Test dependes on "testMethod"
     * and "testMethod" failed due to timed-out
     * So This test should be Skipped but we
     * set alwaysRun = true so it should be executed
     */


    @Test(dependsOnMethods = {"testMethod"},alwaysRun = true)
    public void anotherTestMethod() {
        System.out.println("AnotherTestMethod()From Group1 and Group2");
    }



OutPut:

[TestNG] Running:
  Command line suite

AnotherTestMethod()From Group1 and Group2

===============================================
Command line suite
Total tests run: 2, Failures: 1, Skips: 0
===============================================

Java Result: 1


No comments:

Post a Comment