you're reading...

BeanShell and TestNG

Very recently someone asked a question if I could have the luxury of referring to VM arguments in the TestNG Suite XML file so that they had the luxury of choosing the group to be run at run-time. At first I ended up responding with “NO” as an answer.

After pondering more over this question, I decided to do a deep dive on this and see if this can actually be done. So here’s what I have found.

Yes you can do this using TestNG..!!! Baffled ??? So was I 🙂

The answer to this problem is “BeanShell”. Not sure how many of us even know the fact that TestNG works with BeanShell as well and lets one use BeanShell as one of the method selectors in a suite xml file.

If you end up providing a method selector to TestNG in your suite XML file, TestNG only honours that and conveniently ignores the for groups.

If you feel like reading more, read TestNG documentation.

Here’s a sample class that has 3 test methods and all belong to different groups:

package organized.chaos;

import org.testng.annotations.Test;

public class GroupsPlayGround {
    @Test(groups = "foo")
    public void foo() {
        System.out.println("foo() was invoked.");

    @Test(groups = "bar")
    public void bar() {
        System.out.println("bar() was invoked.");

    @Test(groups = "foobar")
    public void fooBar() {
        System.out.println("foobar() was invoked.");

Now lets takea look at how does our TestNG XML suite file look like:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="false">
	<test name="Test">
				<script language="beanshell">
				<![CDATA[whatGroup = System.getProperty("groupToRun");
			<class name="organized.chaos.GroupsPlayGround" />
	</test> <!-- Test -->
</suite> <!-- Suite -->

That’s all it takes. We now have embedded the ability to read from VM arguments and pass it to TestNG via the XML suite file itself.

So when you run this suite xml file, all you would need to do is specify the group name to be executed via the VM argument [-DgroupToRun=foo for e.g.,] and TestNG will pick up that specify group for execution.

There’s a lot of pretty cool stuff that you can do with BeanShell because it lets you access a lot of the core packages of Java directly.

This post of mine is definitely not intended to unearth all of that, but just to show you how to get started 🙂


10 thoughts on “BeanShell and TestNG

  1. Thank you for this. I had completely missed the capability to using being shell with TestNG would make available.

    Posted by jmochel` | February 13, 2014, 7:51 pm
  2. Also, may I request that you add whatever tags you can to your posts. I have a feeling it would make a difference in how easy you are to find when doing searches on TestNG.

    Posted by jmochel` | February 13, 2014, 7:54 pm
  3. Hi Krihana,

    Im getting below error, when running as suite.

    Exception in thread “main” com.beust.jcommander.ParameterException: Unknown option: -DgroupToRun=foo
    at com.beust.jcommander.JCommander.parseValues(JCommander.java:723)
    at com.beust.jcommander.JCommander.parse(JCommander.java:275)
    at com.beust.jcommander.JCommander.parse(JCommander.java:258)
    at com.beust.jcommander.JCommander.(JCommander.java:203)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:161)

    Posted by Anand Raja K | February 14, 2014, 5:16 pm
  4. Hi, I got here from my stackoverflow question. What if I want to run all tests if no parameter is present? I wrote:
    whatGroup = System.getProperty(“groupToRun”,””);
    but that actually gets me NullPointerException

    Posted by piotroslav | December 28, 2017, 7:08 pm
  5. Ok, as silly as it seems, all I needed was: “return true;” instead of just “true”;

    Posted by piotroslav | December 28, 2017, 7:15 pm
  6. Sorry, it is me again.
    I have problem with my groups setup as bsh is evaluating ‘groups’ variable as empty.
    My example of test method header:
    @Test(groups = {“smoke”})
    public void homePageVerification(){…}

    when I add ‘-DgroupToRun=smoke’ to maven execution this variable it is properly resolved (code in post above) but I get Nullpointer exception on line:

    I debugged into Bsh.java and found out:
    interpreter.eval(“groups”) –> HashMap size =0
    interpreter.eval(“groups.containsKey(\”smoke\”)”) –> false

    Could you advise where might be the problem?

    Posted by piotroslav | January 3, 2018, 9:54 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: