Pages

Monday, April 2, 2012

Parallel execution of Fitnesse tests

We often face the problem of selenium tests running so slow.If you are using fitnesse as your testcase manager and testrunner, probably running tests in Suite takes a long time for you.

We faced similar situations as we integrated our testsuite in continuous integration system. Slow running tests ends up slowing down the build and in turn shipping the features to production environment.

The solution we would recommend is to use the trinidad package shipped with version 20100103. More info at the following url

http://www.fitnesse.info/trinidad

We are using the trinidad package testrunner to run our tests in threads in parallel.The fitnesse tests are maintained in a txt file and reading that file as input we create threads to run tests.



Sample code


package travelQa;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import fit.Counts;
import fitnesse.trinidad.*;
import java.io.*;

public class InProcessRunner {
public static String testright, testwrong, testexceptions, testname,summary;
public static Counts cs;
public static int right, wrong, exceptions, totalright, totalwrong,
totalexceptions;
public static String str1, strhead , strsummary;
static UUID batchId = UUID.randomUUID();

public static void startProcessing(final List tests)
throws InterruptedException {

Thread t = new Thread() {

@Override
public void run() {
try{
for (String next : tests) {


String dbhost = "jdbc:mysql://localhost/automation?user=root";
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection(dbhost);

Statement stmt = conn.createStatement();
stmt.executeQuery("select * from environment");

ResultSet rs = stmt.getResultSet();

while(rs.next()){

System.setProperty("browser", rs.getString("browserCode"));



boolean status = new File("C:\\wamp\\www\\output\\"+ batchId +"\\"+ rs.getString("browserName") +"").mkdirs();
System.out.println(status);
TestRunner tdd = new TestRunner(new FitNesseRepository(
"C:\\root\\fitnesse"), new FitTestEngine(),
"C:\\wamp\\www\\output\\"+ batchId+"\\"+rs.getString("browserName")+"");

cs = tdd.runTest(next);
right = cs.right;
wrong = cs.wrong;
exceptions = cs.exceptions;
totalright = right + totalright;
totalwrong = wrong + totalwrong;
totalexceptions = exceptions + totalexceptions;


testname = tests.toString();
testname = testname.replace("[", "");
testname = testname.replace("]", "");
summary = cs.toString();


java.util.Date dt = new java.util.Date();
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String currentTime = sdf.format(dt);

String sql = "insert into report(testName,passed,failed,exception,createdTime,batchId,browser) values ('"+ testname +"','"+ right +"','"+ wrong +"','"+ exceptions +"','"+ currentTime + "','"+ batchId +"','"+ rs.getString("browserName") +"')";
System.out.println(sql);
PreparedStatement ps = conn.prepareStatement(sql);
System.out.println(sql);

ps.execute(sql);
ps.close ();


System.out.println(" Test Passed " + totalright);
System.out.println(" Test Failed : " + totalwrong);
System.out.println(" Test Exceptions : " + totalexceptions);
}

}

}catch(Exception e){
e.printStackTrace();
}
}

};
t.start();

}


public static void main(String[] args) throws IOException {

File file = new File("C:\\root\\fitnesse\\TestList.txt");


try {
BufferedReader bufRdr = new BufferedReader(new FileReader(file));
String csvline = null;

int c = 0;
List testList = new ArrayList();

while ((csvline = bufRdr.readLine()) != null) {
if (c == 1) {
startProcessing(testList);
testList = new ArrayList();
c = 0;
}
testList.add(csvline);
c++;
}
startProcessing(testList);

} catch (Exception e) {
e.printStackTrace();
}
}


}