Running single test or class using TestNG and Ant
I have been using TestNG for a while now in the latest project that I am working on. We have developed a whole suite of tests for an application and TestNG has really served us well.
When it comes to running tests, we find that people (developer, testers and others) usually are hesitant to use something that involves much learning or is different from how they are used to doing things currently. So, we found it very useful to provide utilities that made it easy for anyone to be able to pick up test artifacts and with-in a few steps be able to run tests to reproduce bugs. Because we are building our project using ant, it was easy to provide a build.xml with the test distribution that will able to run the TestNG tests. This keeps things simple and uniform.
The TestNG ant task uses TestNG suites defined in XML files to run tests. It doesn’t provide an easy way to run a single test method or all the test methods in a single class. Basically, anytime someone has to run a test using this task, they need to create an XML file specifying the class and method for that test in an XML file and then execute the ant task. This can be simplified by letting ant targets take care of setting up the XML file with the require test and then executing it.
It’s quite simple. You just need to create a TestNG suite XML template, modify it with the parameters that the user passes in on command line and then execute the TestNG ant task. Here’s how this can be achieved. First, create a template XML file like the following at ${testng.templates}/testfn.xml:
< !DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name="Single Method Suite"> <test name="Single Method Test"> <classes> <class name="@CLASS@"> <methods> <exclude name=".*" /> <include name="@TEST@" /> </methods> </class> </classes> </test> </suite>
Now, you can add the following target to the build file:
<target name="run-single-test" description="run a specific test. Requires class.name property set to fully qualified name of test class and test.name property set to method name"> <condition property="propsSpecified"> <and> <isset property="class.name" /> <isset property="test.name" /> </and> </condition> <fail unless="propsSpecified" message="class.name and/or test.name property not specified."/> <copy todir="${tmp.dir}" file="${testng.templates}/testfn.xml" overwrite="true"> <filterset> <filter token="CLASS" value="${class.name}"/> <filter token="TEST" value="${test.name}"/> </filterset> </copy> <testng classpathref="lib.path" outputDir="${results.dir}/${DSTAMP}.${TSTAMP}-single-test-${class.name}-${test.name}" workingDir="${results.dir}/${DSTAMP}.${TSTAMP}-single-test-${class.name}-${test.name}" verbose="2" useDefaultListeners="false" listeners="${testng.listeners}"> <xmlfileset dir="${tmp.dir}" includes="testfn.xml" /> <jvmarg value="-Xmx1024m"/> </testng> </target>
The target picks the testfn.xml file, replaces the tokens with the specified input and copies it to the temp location. Now, the TestNG task can use this updated XML file to run tests. This target is invoked as:
ant run-single-test -Dclass.name=com.nalinmakar.testng.ant.Demo -Dtest.name=Test1
for the following class:
package com.nalinmakar.testng.ant; public class Demo { @Test public void Test1() { //do something } @Test public void Test2() { //do something } }
Similarly, you can also create a template and another ant target for running all the test methods in a class:
< !DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name="Single Class Suite"> <test name="Class Test"> <classes> <class name="@CLASS@" /> </classes> </test> </suite>
<target name="run-class" description="run all methods in a specific test class. Requires class.name property to be set to fully qualified name of class"> <condition property="classNameSpecified"> <isset property="class.name" /> </condition> <fail unless="classNameSpecified" message="class.name property not specified. Don't know which test class to run."/> <copy todir="${tmp.dir}" file="${testng.templates}/class.xml" overwrite="true"> <filterset> <filter token="CLASS" value="${class.name}"/> </filterset> </copy> <testng classpathref="lib.path" outputDir="${results.dir}/${DSTAMP}.${TSTAMP}-class" workingDir="${results.dir}/${DSTAMP}.${TSTAMP}-class" verbose="2" useDefaultListeners="false" listeners="${testng.listeners}"> <jvmarg value="-Xmx1024m"/> <xmlfileset dir="${tmp.dir}" includes="class.xml" /> </testng> </target>
and can invoke this as
ant run-single-test -Dclass.name=com.nalinmakar.testng.ant.Demo
Fix “500 Internal Error” in Wordpress
I recently upgraded my Wordpress installation to the latest version (2.8.6) and started running into issues with uploading images. Every time I tried using the Flash Uploader I would see
HTTP Error.
On trying to use the non-flash browser based uploader, I saw the following:
Error 500 – Internal server error
An internal server error has occured!
Please try again later.
I googled quite a bit and finally found a solution. This error can happen because of quite a few different reasons:
- Incompatible plugins
- Incorrect .htaccess file contents
- PHP running out of memory
- Incorrect file permissions
- Not using PHP5
Incompatible plugins
Easiest way to find if this is the issue is to deactivate all plug-ins and see if the issue still persists. If it goes away you can activate each plug-in one by one to find the culprit. You can read more on managing plug-ins here.
Incorrect .htaccess file contents
For some people, the issue went away after fixing the .htaccess file contents. You need to ensure that the .htaccess file at root of your website has the following contents:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
You can read more about it here.
PHP running out of Memory
This can happen if you are using some memory hungry plug-ins. You can fix this issue by adding a php.ini file in <wordpresss_root>/wp-admin folder with the following contents:
memory=20MB
You can read more about this at codedifferent.com, where this is explained in more details.
Incorrect file permissions
Wordpress can start acting up if the permissions on directories and files in Wordpress installation aren’t set up properly. You can ensure that this is fixed by logging into your hosting provider and making sure all files and directories have permissions set to 755.
Another issue that might lead to errors while uploading is certain security issues with files used for uploading. This can be fixed by using Image Upload HTTP Error Fix plug-in. Otherwise, you could edit .htaccess to add the following lines:
#BEGIN Image Upload HTTP Error Fix <IfModule mod_security.c> <Files async-upload.php> SecFilterEngine Off SecFilterScanPOST Off </Files> </IfModule> <IfModule security_module> <Files async-upload.php> SecFilterEngine Off SecFilterScanPOST Off </Files> </IfModule> <IfModule security2_module> <Files async-upload.php> SecFilterEngine Off SecFilterScanPOST Off </Files> </IfModule> #END Image Upload HTTP Error Fix
Not using PHP5
Even after doing all the above, things weren’t working for me. That is when I came across this post, which explains that the issue could be because PHP5 wasn’t enabled. According to a help article on 1and1.com, fix is to just add the following line in .htaccess file:
#enable php5 AddType x-mapp-php5 .php
This fixed the issue for me!
Hope this helps.
Tropical Cyclone Nisha
Came across this last year while checking on Hyderabad’s weather in December. They actually named a tropical cyclone after my wife’s name !!
The cyclone went over the southern regions of India and has now settled in my life
Desi Habits
Got the following list as a forward. Being quite a typical desi, I do have to agree with quite a few of them
- The only reason you go to a temple on festivals is because there is free food.
- You keep comparing prices at electronics store for the phone you bought six months ago.
- You bought a Toyota or Honda car only because it has better resale value.
- You ask for a small drink at fast food restaurant because the refill is free.
- You spent 2 days cleaning your apartment before leaving so you can get full security refund from your landlord.
- You don’t know any American outside your work.
- You try to ignore all other unknown desis around you.
- You talk to Americans as if you represent your whole country.
- You frequent to yard sales every week.
- You use grocery bags as garbage bags.
- Office supplies mysteriously find their way in your house.
- You don’t want to buy a printer because you can always use the office printer.
- You decide to marry a girl/guy that your parents fixed you up with.
- You split the tax from your common grocery bill.
- You know more than one plans offered by long distance companies.
- You take plain water instead of Coke for lunch.
- You take any drink with no ice because you can’t drink ice.
- You ask before eating any meat “Is this beef?”
- You know all the facilities available at public library.
- You find taco bell sauce packets in your kitchen drawer.
- You take off your shoes before stepping foot in your living room.
- You like onion rings at Burger King.
- You are looking for dual voltage appliances and GSM cellphones
- The number of long distance calls is more than domestic calls.
- Your first name ends with kumar, bhai or ben
- You have at least one India made pressure cooker in your kitchen.
- You know how much a 7 layer burrito costs at Taco Bell.
- Put oil in your hair.
- You have a picture of Indian Deity on the dashboard of your car.
- This thought comes to you “Oh Shit I just saw another desis” when you are window shopping at a local mall.
- You are compelled to visit every major city in U.S. just so as to say that “Yes, I have been there”.
- You pay your bills the day they come in mail.
- You buy rice in the 20lb bags or larger.
- You have postponed buying answering machine because the phone you are planning to buy six months later has built-in answering machine.
- You start spelling your name to the operator like A as in Apple, B as in boy, T as in train… well you get the idea.
- You bring over the counter medicine like Iodex and Vicks from India
- You know the current differential in gold prices between India and U.S.
- Use the credit card with maximum cash back
- You have collected enough frequent flier miles for an international trip.
- You are saving more than 30% of your salary.
Connect to MS SQL Server using JDBC
Posted by Nalin in Miscellaneous on December 13th, 2008
The following example shows a java class that can be used to verify if you connection to MS SQL Server using JDBC is working properly. The following code establishes a connection to MS SQL server and then executes a query to print system tables. You need to get Microsoft’s JDBC drivers or jTDS JDBC drivers.
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class ConnectMSSQLServer {    public void dbConnect(String db_connect_string,             String db_userid,             String db_password)    {       try {          Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");          Connection conn = DriverManager.getConnection(db_connect_string,                   db_userid, db_password);          System.out.println("connected");          Statement statement = conn.createStatement();          String queryString = "select * from sysobjects where type='u'";          ResultSet rs = statement.executeQuery(queryString);          while (rs.next()) {             System.out.println(rs.getString(1));          }       } catch (Exception e) {          e.printStackTrace();       }    }    public static void main(String[] args)    {       ConnectMSSQLServer connServer = new ConnectMSSQLServer();       connServer.dbConnect("jdbc:sqlserver://<hostname>", "<user>",                "<password>");    } }
Change <hostname>, <user> and <password> to the correct values. Also, if you use the jTDS drivers, the driver class should be set to net.sourceforge.jtds.jdbc.Driver.
My Friend’s Laptop
One of my friends is using a really messed up Dell Inspiron 8600 right now that should have actually been decommissioned ages ago… Almost the whole of left half of the screen is useless and it needs a support to hold up the screen while in use. Pictures below show what I am talking about:









Azureus – Java BitTorrent Client
Get Firefox
Add to Google
Add to My Yahoo!
Recent Comments