Thursday, 24 October 2013

Advantages of TestNG over Junit

  1. In Junit we have to declare @BeforeClass and @AfterClass which is a constraint where as in TestNG there is no constraint like this.
  2. Additional Levels of setUp/tearDown level are available in TestNG like @Before/AfterSuite,@Before/AfterTest and @Before/AfterGroup
  3. No Need to extend any class in TestNG.
  4. There is no method name constraint in TestNG as in Junit. You can give any name to the test methods in TestNG
  5. In TestNG we can tell the test that one method is dependent on another method where as in Junit this is not possible. In Junit each test is independent of another test.
  6. Grouping of testcases is available in TestNG where as the same is not available in Junit.
  7. Execution can be done based on Groups. For ex. If you have defined many cases and segregated them by defining 2 groups as Sanity and Regression. Then if you only want to execute the “Sanity” cases then just tell TestNG to execute the “Sanity” and TestNG will automatically execute the cases belonging to the “Sanity” group.
  8. Also using TestNG your selenium testcase execution can be done in parallel.

Tuesday, 15 October 2013

JMeter Tutorials

How to use "loop controller" in jmeter with example

As you know, Apache jmeter has many logic controllers and loop controller is one of the mostly used controller in performance testing using jmeter. Apache jmeter's thread group is it self loop controller and you can read about 'Loop Count' property of thread group in my previous post. So now your question is 'If
thread group has ability to generate a loop then why require loop controller?'. Answer is - group's 'Loop Count' property forces all sub requests execution multiple time while loop controller will force only specified requests's execution multiple time. Another feature of loop controller is - Apache jmeter will force multiple execution of all sub requests of loop controller before going to next requests of thread group. Let we take one simple example.
Created sample test of apache jmeter using loop controller as bellow. If you don't know how to create test plan in jmeter then you have to read my post about apache jmeter sample test plan recording steps. Set Number of Threads (users) = 1 and Loop Count = 1 in thread group property. Added Loop Controller in between and moved 'My Account Page' HTTP request under loop controller as shown bellow.


As you see, i set Loop Count = 3. It means all sub requests under loop controller will be executed 3 time. In above case, 'My Account Page' request will be executed 3 time. When i run this test, view result in tree listener shows me requests execution sequence as bellow.

Look here, 'Login' and 'My Orders Page' requests are executed only 1 time while My Account Page's requests is executed 3 time because it is under loop controller and i set Loop Count = 3. Note one thing more is My Orders Page request is executed after completion 3 time execution of My Account Page.
This way if you want to force only few sub request's execution multiple time in your software application performance testing plan then you can use loop controller in your test plan.



How to use Apache JMeter "Transaction Controller" to get total time of execution

As you know, Apache jmeter is performance testing tool and supporting many operation systems like FreeBSD,  Linux, Mac, Windows, Solaris Sparc, OpenVMS Alpha etc.. We can say that controllers are the main part of jmeter and they are very useful to control execution of jmeter script for load testing. Let me
share use of "Transaction Controller" in java jmeter testing.
If you performed load testing using jmeter then you knows that jmeter is recording all sub requests with main request during script recording. If there is any jpg, gif, png image or js on page then it will be recorded as a new request in your script with main page request. Generally we are excluding this kind of requests during recording of script to get correct and better result of main request of real time scenario. But supposing if you want to measure performance of main page HTTP request including it's sub requests and want to calculate overall load time of page including sub requests then how you will do it? "Transaction Controller" will help you to handle this scenario.
Let we take simple example to get it better in mind.
Look in to above example, There are 2 main HTTP requests. 1. Home Page and 2. FAQ Page. .jpg request, .gif request, .png request and .js request are the sub requests of Home Page. On completion of recording, i added Transaction Controller and moved all requests of home page under it. I set 2 virtual users in thread group and added Aggregate Report Listener to view test results.
Now i run this script. Apache jmeter shows me results as bellow in Aggregate Report.
Look in above image, Jmeter has added one extra result line as "Transaction Controller" in result. It is the sum of all the sub requests and main request(Home Page, .jpg request, .gif request, .png request, .js request) in all result columns. Sum of all requests will be not exact same as Transaction Controller value but it will be nearest to it.
What is the Use of "Generate parent sample" flag in Transaction Controller?
If "Generate parent sample" is checked in transaction controller then you can view only Transaction Controller line in Aggregate Report as bellow. Transaction Controller's sub requests will be not displayed in report.
What is the Use of "Include timer duration in generated sample" flag in Transaction Controller?
If You have added any timer under transaction controller and if "Include timer duration in generated sample" is 1 then Transaction Controller result line will show you time including timer duration. If it is 0 then result will be excluded of timer time.
 
 
 

Apache Jmeter "Switch Controller" With Example

"Switch Controller" is one of the Logic Controller of Apache Jmeter. As name suggest, You can switch your targeted software application page request to run using Switch Controller. You can set your required request id in Switch Controller to execute it so all other requests under Switch Controller will be neglected
during execution. Let we look at practical scenario for better understanding.
Suppose i recorded apache jmeter testing script for 4 pages -> 1. Home Page, 2. My Account Page, 3. Login Page and 4. FAQ Page. Now supposing temporarily i do not want to perform load testing on 'My Account Page'. Then what i can do? 1st idea is i will remove that request from load test script and then run test. 2nd idea is i can use switch controller to exclude that request. Let me show you that jmeter example script with screenshots.


I set Number of Thread = 3 and Loop Count = 2 in thread group property and set Switch Value = 0 in Switch Controller property. I run this test and viewed results in View Results Tree listener as bellow.

See in above image, any request of 'My Account Page' is not display in result because i set Switch Value = 0 in switch controller so jmeter will consider to execute only 1st request from all requests under switch controller. If i set Switch Value =1 then all 3 virtual users of Apache jmeter will execute only My Account Page request. Create script with multiple pages under Switch Controller for your software application and try above example with different Switch Values.
 
 

Tutorials To Use "Runtime Controller" In Windows Using Apache Jmeter

I described "Interleave Controller" and "Once Only Controller" of Apache Jmeter in my previous posts. There are nearest 16 different logic controllers available in Apache JMeter and i will try to describe each of them in my upcoming posts. Let we look at "Runtime Controller" to see how it can help us for our software
application performance or load testing activity.
Runtime Controller's function is same as its name. You can set time in Runtime Controller to stop jmeter requests execution after defined time. Let we take one example to get better understanding. Supposing i want to perform load testing on 4 page requests using Runtime Controller. My Jmeter test design will be as shown in bellow images.




Look at above images. I set Runtime (Seconds) = 3 in Runtime controller and i am running test. View Result in tree listener shows me only 2 executed requests on completion of execution as bellow image.
.
Result of Runtime (Seconds) = 3
Now i am increasing Runtime (Seconds) = 15 in Runtime controller then View Result in tree listener shows me more executed requests as bellow.
Result of Runtime (Seconds) = 15
As per above 2 example results, runtime controller will execute requests till definer runtime in Runtime controller without considering loop count of thread group. I think now Runtime controller's function is clear for you. Design and run same test case on your own software application to get better idea.
 
 

"Once Only Controller" - jmeter tutorial with example for windows

You can read my post about jmeter downloading and installation before learning Once Only Controller controller in Apache jmeter. I hope you already knows that Apache jmeter is load or i can say performance testing tool and during software performance testing, you need to handle multiple kind of scenarios and
conditions. For that Apache jmeter has many tools like Logic Controllers, Timers, Preprocessors etc. Once Only Controller is one of the Logic Controller.
During your software application's performance testing, you will face scenario like - user should login in to application only 1 time and then he should access inner pages(after login pages) 2 or more than 2 times. How will you do this? Now Once Only Controller will comes in to picture. You can handle this scenario using once only controller.
Let we look at simple example script. I recorded jmeter testing script for login, View My Account Page and then view My Orders Page. Then added Once Only Controller under thread group and moved login page request inside once only controller. You can read, how to add controller, how to move requests and hot to add listener in my previous post. Then set Number of Threads (users) = 2 and Loop Count = 3 in your thread groups. Now your Apache jmeter load testing script will looks like bellow.


Now run above given example script and run it and look at results on View Results Tree. It will looks like bellow.


As shown in above image, 

  • Login page request is executed only 2 times because we set 2 users in thread group so 1 time for each user.
  • Remaining both requests are executed 6 times each because we set Threads (users) = 2 and Loop Count = 3 in thread group so 3 time for each user.
  • So conclusion is - requests under Once Only Controller will be executed only once per user without considering loops.
You can experiment same Once Only Controller scenario for your on test software application to get better idea.
 
 

Using "Interleave Controller" In Apache Jmeter windows Example

Before using interleave controller in performance testing tool Apache Jmeter, You must be aware about how to Record and Run first test plan in jmeter load testing tool in windows OS. There are many controllers available in apache jmeter performance testing tool and one of them is Interleave controller. Interleave
controller in apache jmeter is basically useful for providing some more load on specific page compared to other pages of software web application. Now let me describe here one scenario for better understanding.
Supposing i have a software web application with 4 pages. 1. Home, 2. News, 3. FAQ and 4. About Us Page and obviously home page has to face more load as compared to other pages in real world scenario. So let we consider this scenario in our example. My recorded jmeter example test case is as bellow.


Adding interleave controller in jmeter performance testing tool
To add Interleave controller in right click on your thread group (Testing Thread in this case)

  • Go to Add -> Logic Controller -> Select 'Interleave Controller'. Now Interleave Controller is there in your jmeter test plan. 
  • Cut "News Page" HTTP request from thread group by right clicking on "News Page" and paste under "Interleave Controller" by right clicking on "Interleave Controller" as shown bellow
Add View Results Tree listener and set thread group parameters for jmeter load testing
  • Right click on Thread group and Go to Add -> Listeners -> Select 'View Results Tree'. Now listener is there in your test plan
  • Set Number of Threads(users) = 1 and Loop Count = 5 in Thread group. You can read more about thread group parameters in my this post.
  • Now your Apache Jmeter test plan will looks like bellow.
Run above test and look at View Results Tree results during test running. results will be as bellow.
Look at above results of View Results Tree listener.
  • First of all, Home Page request is executed.
  • 2nd is executed News Page request.
  • Once more Home Page request is executed.
  • 4th request executed is FAQ Page.
  • Once more, 5th executed request is Home Page.
This way, request which is located outside of Interleave controller will be executed alternatively to each of request inside Interleave controller. You can try above test case with 2 users for better your practice.
 
 
 

Apache Jmeter - Running Your First Web Test Plan Steps

You can read step by step process of recording web test plan in jmeter in my post about how to record test plan in Jmeter. After completion of test plan recording, you need to run your test plan to measure performance of application and how it behaves when multiple concurrent users accessing specific page or request. Before running your test, you need Set properties in Thread Group element (Load Profile
Settings) and need to insert some new elements in your test plan to perform load testing and recording its results.
Adding Listeners in Apache Jmeter Test Plan
Listeners in Jmeter are very useful elements and are used for showing results of your executed test plan samples. There are many different types of listeners available to show results in Table, Tree, Graph or only log file and you can add any of them as per your requirement. After Recording and Thread Group property setting in Jmeter, You can add listeners into your web test plan as bellow.
Right click on Thread Group and select Add -> Listener -> Aggregate Report 
Right click on Thread Group and select Add -> Listener -> View Results Tree
In bellow given image, i have added "Aggregate Report" and "View Results Tree" listeners. You can add any other too as per your requirement.


Run your web test plan
After setting thread group properties and adding listeners, You can Run your test plan.
To run your test plan, Select Run > Start from Jmeter main menu as bellow.


Once you click on start button, Jmeter will start loading users(Specified in Thread group properties) to execute test plan. See Bellow Image. In Top-Right corner, it is showing 2/7 with green signal. Green signal indicates that your test is in process and 2/7 indicates that currently 2 users are executing test plan from total 7 users. When test plan execution will finish, Green signal will disappear and it will show 0/7.


View Results Tree Listener
In "View Results Tree" section, Some requests are display with green color and some are display will red color. Green requests indicates that request is executed successfully and becomes pass. Red requests indicates that there was appear some error during execution of request and it becomes fail. 
Aggregate Report Listener
Same way you can see aggregate report in table view as bellow. There are many parameters available in aggregate report like No of samples executed, Average time of execution, Median time of execution, 90% line, Minimum time of execution, Maximum time of execution, etc..

 
 
 
 

Apache Jmeter - Introduction of Thread Group properties - Number of users and Ramp-Up Period

Thread group is very important element in jmeter where you can set number of users and its ramp up time. Click here to read how to add Thread group in your web test plan. Let me describe you all required properties of thread group with detail.

Thread Group Properties settings

Number of Threads(users) :
Described the total number of threads or users used to execute test plan. Each and every user will execute full test plan.


Ramp-Up Period (In Seconds) :
Describes time to load all users given in "Number of Threads(users)" Property.

Number of Threads VS Ramp-Up Period Example 1: 
If you will set
Number of Threads(users) = 10
and
Ramp-Up Period (In Seconds) = 100

then Jmeter will load all 10 users in 100 seconds means every 1 user will be loaded after every 10 seconds (100(seconds)/10(Users) = 10 seconds).

Number of Threads VS Ramp-Up Period Example 2: 
If you will set
Number of Threads(users) = 100
and
Ramp-Up Period (In Seconds) = 10

then Jmeter will load all 100 users in 10 seconds means every 10 user will be loaded after every 1 seconds (10(seconds)/100(Users) = 0.1 seconds).

You can set Number of Threads VS Ramp-Up Period ratio based on your requirement. In bellow given image, I have set Number of Threads (users) = 5 and Ramp-Up Period (In Seconds) = 5 so it jmeter will load 1 user after every 1 second.


Loop Count :
Describes how many times your test plan will be executed. If you will set it 5 then full test plan will be executed 5 time. If you will select "Forever" check box then your test plan will run your test plan forever time. You have to stop its execution manually if you selected "Forever" check box. In above given figure, i have set Loop Count = 2 So jmeter will run test plan only 2 times.

Scheduler :
You can configure test start time, end time, duration and start up delay of your load test plan using Scheduler Configuration section. Click on Scheduler check box then it will show you related elements as shown in above figure.

Start Time :
Describes when to start test. In above given figure, i have set it "2013/06/29 16:38:53". So your test will be started at "16(Hrs):38(Mins):53(Secs)" on 29th June, 2013. (Note : Your jmeter should be running on given date and time in "Start Time" field).

End Time :
Describes when to End test. In above given figure, i have set it "2013/06/29 16:45:70". So your test will be ended at "16(Hrs):45(Mins):70(Secs)" on 29th June, 2013. Here please note one thing - End Time is maximum allowed time to finish execution of your test plan means if your all threads has not completed its test execution on given "End Time" then jmeter will stop all pending threads execution immediately. On other end, All threads can also completes its test execution before specified "End Time".

Duration (seconds) : 
You can set duration of test to execute. For Example, If you will set Duration (seconds) = 2 then your test will be executed only for 2 seconds without considering (1.) End time and (2.) All threads has completed its test or not. Jmeter will stop test in 2 seconds.

Startup delay (seconds) :
You can set delay on start up of test. If you will set it 5, then jmeter will not load any user in 1st 5 seconds when you start running your test and as soon as completion of 5 seconds, jmeter will start loading users as per given load profile settings.
 
 

Performance testing tool jmeter load testing sample test plan recording steps

Steps to Recording First test plan in Jmeter
If you are not aware about how to download and install apache jmeter then you can read my previous post where i have described everything about jmeter installation process. In this post i have described how jmeter record your first test plan using jmeter proxy server.

Changing Firefox browser settings for recording jmeter first test plan
First of all, you need to change bellow given settings of Firefox browser for recording script in apache jmeter.


Open Firefox Browser
In Firefox Browser,

  • Open Tools -> Options -> Advanced tab -> Network tab -> Settings. It will open connections setting popup.
  • Select "Manual Proxy Configuration" radio button.
  • Set HTTP Proxy = 'localhost' and Port = '90' 

(Note : You can use any other port id at place of '90' if it is being used by any other instance. Other ports Example : 8080, 4455, 4445, etc..)

Now your browser connection settings will be looks like bellow.


Jmeter load testing tool settings for recording your first test plan
Start Jmeter tool by running jmeter.bat file. It will open apache jmeter GUI.

  • Right click on "Test Plan" element and select Add -> Threads(Users) -> Thread Group. It will add "Thread Group" under "Test Plan".
  • Right click on "Thread Group" element and select Add -> Logic Controller -> Simple Controller
  • Right click on "WorkBench" element and select Add -> Non-Test Elements -> HTTP Proxy Server. It will add "HTTP Proxy Server" under "WorkBench". It will add jmeter proxy server in your test plan.
  • Click on "HTTP Proxy Server" element and Set Port = '90' and select Target Controller = Thread Group> Simple Controller. Now your jmeter proxy server's settings are done.

(Note : Here Port id must be same as browser connection setting. We have set Port = '90' in browser connection setting so need to set same port in "HTTP Proxy Server" settings as bellow image)

Now Your Jmeter GUI settings and other configurations will looks like bellow.


If your all settings are correct as shown in above figure, you are ready for recording your first sample test plan script in apache jmeter. Click on "Start" button as shown in above image and then open your application URL in Firefox browser and perform required navigation on it. All your requests will be recorded under "Simple Controller" as bellow.


Click here to read about Running Your First Web Test Plan Steps in apache jmeter.
 
 
 
 

Friday, 5 April 2013

JUnit 4 Annotations


JUnit 4 Annotations : Overview
Following is a list of frequently used Annotations , which is available when you include junit4.jar in your Classpath:

@Before
@BeforeClass
@After
@AfterClass
@Test
@Ignore
@Test
(timeout=500)
@Test
(expected=IllegalArgumentException.class)


@Before and @After
In Junit4 there is no setup() or tearDown() method and instead of that we have @Before and @After annotations.
By using @Before you can make any method as setup() and by using @After you can make any method as teardown(). What is most important point to remember is @Before and @After annotated method will be invoked before and after each test case. So in case you have five test cases in your JUnit test file than just like setup() and tearDown() method annotated with @Before and @After will be called five times. Here is an example of using
@Before and @After Annotation :

    @Before
   
 public void setUp() {
       
 System.out.println("@Before method will execute before every JUnit4 test");
   
 }
 
 
    @After
   
 public void tearDown() {
       
 System.out.println("@After method will execute after every JUnit4 test");
   
 }


@BeforeClass and @AfterClass
@BeforeClass and @AfterClass JUnit4 Annotations are similar to @After and @Before with only exception that they
are called on per TestClass basis and not on per test basis. They can be used as one time setup and tearDown
method and can be used to initialize class level resources. here is an example of using @BeforeClass and @AfterClass Annotations in JUnit4, here is an example of @BeforeClass and @AfterClass Junit 4 annotation

    @BeforeClass
   
 public static void setUpClass() throws Exception {
       
 System.out.println("@BeforeClass method will be executed before JUnit test for"
                +
 "a Class starts");
   
 }

    @AfterClass
   
 public static void tearDownClass() throws Exception {
         System.
out.println("@AfterClass method will be executed after JUnit test for"
                +
 "a Class Completed");
   
 }


@Test
@Test is a replacement of both TestCase class and convention "test" which we prefix to every test method. for example to test a method  called calculateInterest() we used to create method testCalcuatedInterest() and our class needs to be extended from org.junit.TestCase class. Now with @Test annotation that is not required any more. You just need to annotate your test method with @Test Junit4 annotation and done. no need to extend from TestCase class and no need to prefix "test" to your method, here is an example of  JUnit 4 @Test annotation

 @Test
   
 public void testCalculateInterest() {
       
 System.out.println("calculateInterest");
        fail
("An Example of @Test JUnit4 annotation");
   
 }


@Ignore
JUnit 4 Annotations examples list meaningsSome time we add test method in JUnit test class but hasn't implemented that is causing your build to fail if JUnit testcase are integrated or embedded into build process. you can avoid that problem by marking your test method as @Ignore in Junit4. JUnit4 ignores method annotated with @Ignore and doesn't run during test. Here is an example of using @Ignore annotation in JUnit4 to exclude a particular Test from running:


 @Ignore("Not yet implemented")
    @Test
   
 public void testGetAmount() {
       
 System.out.println("getAmount");
        fail
("@Ignore method will not run by JUnit4");
   
 }


@Test(timeout=500)
Now with JUnit4 writing testcases based on timeout is extremely easy. You just need to pass a parameter timeout with value in millisecond to @Test annotation. remember timeout values are specified in millisecond and your JUnit4 timeout test case will help if it doesn't complete before timeout period. This works great if you have SLA(Service Level Agreement)  and an operation need to complete before predefined timeout.

  @Test(timeout = 500)
   
 public void testTimeout() {
       
 System.out.println("@Test(timeout) can be used to enforce timeout in JUnit4 test case");
       
 while (1 == 1) {
         
 
       
 }
   
 }

This JUnit4 test will fail after 500 millisecond.

@Test(expected=IllegalArgumentException.class)
Another useful enhancement is Exception handling testcases of JUnit4. Now to test Exception is become very easy and you just need to specify Exception class inside @Test annotation to check whether a method throws a particular exception or not. here is an example which test behavior of a method to verify whether it throws Exception or not,  when run with invalid input:

    @Test(expected=IllegalArgumentException.class)
   
 public void testException(int input) {
       
 System.out.println("@Test(expected) will check for specified exception during its run");
     
 
   
 }


These were list of frequently used JUnit 4 annotations and there meanings. In the course we have also learn how to use @Before , @After in place of setup() and teardown(). Code review and Unit testing is one of the best development practices to follow and we must try our best to incorporate that in our daily coding and development cycle.