Showing posts with label TestNG. Show all posts
Showing posts with label TestNG. Show all posts

Tuesday, June 3, 2014

How to add TestNG to the project.

1) Go to eclipse.
2) Help
3) Eclipse Marketplace
4) In Find, search for TestNG
5) Install 'TestNG for Eclipse'
6) Then Right click on your project and Navigate to Build Path then click on Add Libraries
7) Select TestNG and click on next and finish it.
8) That's all.

Thursday, May 15, 2014

How to do the performance or load testing using selenium.

Note- Use threadPoolSize and invocationCount attribute.
The threadPoolSize attribute tells TestNG to create a thread pool to run the test method via multiple threads. With thread pool, it will greatly decrease the running time of the test method.


Ex- Start a thread pool, which contains 4 threads, and run the test method 12 times.

import org.testng.annotations.Test;

public class PerformanceTesting {
    @Test(invocationCount=12, threadPoolSize=4)
    public void runTest(){
        System.out.println("Thread Id: "+Thread.currentThread().getId());
    }
}

How to execute a test case multiple times or use of 'invocationCount'

Note- invocationCount , this determined how many times the test case should execute.

ex- This will execute 5 times.

import org.testng.annotations.Test;

public class RunTestMultipleTimes {
    @Test(invocationCount=5)
    public void runMultipleTimes(){
        System.out.println("run this test");
    }
}

Output
---------

run this test
run this test
run this test
run this test
run this test
PASSED: runMultipleTimes
PASSED: runMultipleTimes
PASSED: runMultipleTimes
PASSED: runMultipleTimes
PASSED: runMultipleTimes

How to use 'Groups' in TestNG



import org.testng.annotations.AfterGroups;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test;

public class GroupsInTestNG {
    @BeforeGroups({"secondGroup","firstGroup"})
    public void executeBeforeGroup(){
        System.out.println("executeBefore both Group");
    }
    @AfterGroups("firstGroup")
    public void executeAfterGroup(){
        System.out.println("executeAfter firstGroup");
    }
   
    @Test(groups="firstGroup")
    public void method1(){
        System.out.println("method1");
    }
    @Test(groups="firstGroup")
    public void method2(){
        System.out.println("method2");
    }
    @Test(groups="secondGroup")
    public void method3(){
        System.out.println("method3");
    }
    @Test(groups="secondGroup")
    public void method4(){
        System.out.println("method4");
    }
    @Test(dependsOnGroups={"secondGroup","firstGroup"})
    public void method5(){
        System.out.println("if both group passed then method5 test will execute");
    }
}




testng.xml
-------------

<suite name="Suite" parallel="none">
  <test name="Test1">
    <classes>
      <class name="advancedSelenium.GroupsInTestNG"/>
    </classes>
  </test>
  <test name="Test2">
      <groups>
          <run>
          <include name="secondGroup"></include>
          </run>
      </groups>
    <classes>
      <class name="advancedSelenium.GroupsInTestNG"/>
    </classes>
  </test>
</suite>


Output

----------

executeBefore both Group
method1
method2
executeAfter firstGroup
executeBefore both Group
method3
method4
if both group passed then method5 test will execute
method3
method4

How to abort a test case at runtime if it is taking more time than expectaion or how to use 'timeout' parameter in TestNG.

Note- The 'timeout' means if test case is taking longer than a specified number of milliseconds to finish, TestNG will abort it and market it as failed.
'timeout' can also be used for performance testing, to ensure the method is returned within a reasonable time.

ex-

import org.testng.annotations.Test;

public class TimeOut {
    @Test(timeOut=3000) //time is in milliseconds, here this test case will execute for max 3sec if it will not complete in 3sec then it will be failed.
    public void thisTestShouldPass() throws InterruptedException{
        System.out.println("This test case should pass");
        Thread.sleep(1000); //wait for 1 sec
    }
   
    @Test(timeOut=5000)  //here this test case will execute for max 5sec if it will not complete in 5sec then it will be failed.
    public void thisTestShouldFail(){
        System.out.println("This test case should fail");
        while(true); //this loop is never going to end as condition is always true
    }
}

How to skip a test case in TestNG or use of parameter 'enabled'

Note- By default every @Test has enabled=true.

ex-

import org.testng.annotations.Test;

public class Enabled {
    @Test                            
    public void defaultEnabled(){
        System.out.println("by default enabled=true");
    }
    @Test(enabled=true)
    public void trueEnabled(){
        System.out.println("hard coded enabled=true");
    }
    @Test(enabled=false)
    public void falseEnabled(){
        System.out.println("this test case will be skipped because enabled=false");
    }
}

How to handle runtime exception in TestNG or how to use 'expectedExceptions' with @Test

Note- This example shows how to test a runtime exception.
If the method withException() throws any runtime Exception then it will be passed.

ex-
1)

import org.testng.annotations.Test;

public class RunTimeExceptions {
    @Test(expectedExceptions = Exception.class)
    public void withException(){
        int a = 5/0;
    }
}

2) This test case will fail-

import org.testng.annotations.Test;

public class RunTimeExceptions {
    @Test()
    public void withException(){
        int a = 5/0;
    }
}

Wednesday, May 14, 2014

How to execute the testcases parallely in TestNG

Test cases can be run parallel by updating testng.xml.

ex- Here two browsers will open as thread-count = "2" (you can change it according to your requirement). In one browser all the classes will be executed which are under 1st test and in 2nd browser, all the classes will be executed which are under 2nd test.

<suite name="Suite" parallel="tests" thread-count="2">
  <test name="Test1">
    <classes>
      <class name="Library.Module1"/>
    </classes>
  </test>
  <test name="Test2">
    <classes>
      <class name="Library.Module2"/>
    </classes>
  </test>
</suite>


Go back to Interview Ques-Ans for Automation Tester

Go to Example with Real Scenario