Testing: An open source load testing tool

There is an easy way to load test simultaneous users in the application. The name of open source tool is called as Locust

Here is sample locust implementation of locust on my github repository (its pure python, no xml etc)


Framework link  https://locust.io/


Getting Started

Once installed, the first thing you need to do is write your test case, which is just a few lines of simple Python. Mine looks like this.

from locust import HttpLocust, TaskSet, task

json = """
  timestamp: 1339594030236,
  type: test_event,
  fields: [
    { foo: 'FOO' },
    { bar: 'BAR' },
  version: 1

class MyTaskSet(TaskSet):
    def send(l):
        l.client.post("/", json)                                                                                                              

class MyLocust(HttpLocust):
    task_set = MyTaskSet
    min_wait = 5000
    max_wait = 15000

I only have one URL to test, so it’s pretty simple. I make a POST request to this URL with some JSON.

To start Locust, simply run the locust tool, passing in the host you want to test.

locust -H http://localhost:37010

It’s not doing anything at the moment. To start the load testing, you need to open up the web interface at (if your’re running Locust locally) and choose the amount of users to simulate, and their hatch rate (i.e. how fast the users will spawn).


testNG: How to write time test


Some time performance is critical. Lets a repository call does return the response withing request time out units then it will cause the application to break. Just to keep those things and performance in mind, testNG tests can be calibrate on the measure of time in milli seconds.

The “Time Test” means if an unit test takes longer than the specified number of milliseconds to run, the test will terminated and mark as failed.

import org.testng.annotations.*;public class TestNGTest4 {

	@Test(timeOut = 1000)  
	public void infinity() {  
		while (true); 
              // userRepository.getAllUsersInEntireWorld(); 


In above example, the infinity() method will not return, so the TestNG engine will mark it as failed and throw an exception

FAILED: infinity
Method public void TestNGTest4.infinity() didn't finish within the time-out 1000
... Removed 18 stack frames


testNG : How to ignore or disable a test in testNG

Some times a failing test causes the build to fail. In that cases before the test is fixed a quick remedy is to disable the test to get the build passed. That can be done easily through annotation.


The TestNG engine will just bypass this method.

import org.testng.annotations.*;

public class TestNGTest3 {

	public void divisionWithException() {  
	  System.out.println("Method is not ready yet");


In above example, TestNG will not test the divisionWithException() method.

TestNG : Expected Exception Test

It is always good to test the working and output of your unit. But it is more important to test the negative behavior to make application robust. What if you are expecting an exception from a part of code which is not thrown. the point where application has to get terminated, it is moving smoothly to get a dead lock or a pitty crash. Just to save such situation Testing of exception generation is code is very important. There are annotation in TestNG frame work which helps to make is an easy task.


import org.testng.annotations.*;

public class TestNGTest2 {

	@Test(expectedExceptions = ArithmeticException.class)  
	public void divisionWithException() {  
	  int i = 1/0;


In above example, the divisionWithException() method will throw an ArithmeticException Exception, since this is an expected exception, so the unit test will pass.

More advanced example can be observed in the snap shot attached.
Where the scenarios to test says – “Calling this method with null argument throws Exception”.
The test will go green if exception is thrown, else it will go red.

Testing the expected exception

Testing the expected exception

TestNG : Basic Annotations used in testNG


This blog introduces the basic annotation supported in TestNG.

import java.util.*;
import org.testng.Assert;
import org.testng.annotations.*;

public class TestNGTest1 {

    private Collection collection;

    public void oneTimeSetUp() {
        // one-time initialization code   
    	System.out.println("@BeforeClass - oneTimeSetUp");

    public void oneTimeTearDown() {
        // one-time cleanup code
    	System.out.println("@AfterClass - oneTimeTearDown");

    public void setUp() {
        collection = new ArrayList();
        System.out.println("@BeforeMethod - setUp");

    public void tearDown() {
        System.out.println("@AfterMethod - tearDown");

    public void testEmptyCollection() {
        System.out.println("@Test - testEmptyCollection");

    public void testOneItemCollection() {
        System.out.println("@Test - testOneItemCollection");


@BeforeClass - oneTimeSetUp
@BeforeMethod - setUp
@Test - testEmptyCollection
@AfterMethod - tearDown
@BeforeMethod - setUp
@Test - testOneItemCollection
@AfterMethod - tearDown
@AfterClass - oneTimeTearDown
PASSED: testEmptyCollection
PASSED: testOneItemCollection