Eclipse plugin - passing arguments to a test

144 views
Skip to first unread message

moran

unread,
Oct 16, 2006, 2:35:06 PM10/16/06
to testng-users
I've recently downloaded the TestNG eclipse plugin, and was wondering
how (if its even possible) to pass arguments into the test.

The command line arguments are used to insert a permutation (or
context)
in which the test, or series of test-cases are supposed to run. For
example, the same
test can be run with or without transactions - but the test logic
remains as is.

So I would have something like:
public class TestNg
{
public TestNg(String[] args)
{
//parse arguments and save
//in the context of the TestNg instance.
}
}

can someone guide me through?

Thank you.

Cédric Beust ♔

unread,
Oct 16, 2006, 2:46:00 PM10/16/06
to testng...@googlegroups.com
Hi Moran,

Unfortunately, there is no "clean" programmatic API to do that at the moment:  parameters are not exposed to the TestNG class.  Now that you mention it, it's something that I'd like to fix, but for now, you have two options:

1) Use a DataProvider that will pass the correct parameter to your test method:

@DataProvider(name = "transaction")
public Object[][] createTransactionSettings() {
  return new Object[][] {
    new Object[] { true },
    new Object[] { false },
  }
}

@Test(dataProvider = "transaction")
public void test(boolean useTransactions) {
  // will be invoked once with true, then with false
}

Of course, you can pass more than one parameter this way (see the doc for DataProviders).

2) If you absolutely want to use your own TestNG object, you can create a synthetic XmlSuite (which represents your testng.xml):

XmlSuite suite = new XmlSuite();
suite.getParameters().put("transaction", "true");
...
(then add an XmlTest object, etc...)

and then you pass this to the TestNG object.

I would recommend option 1)...

Let me know if you have more questions!

--
Cedric

moran

unread,
Oct 16, 2006, 3:21:47 PM10/16/06
to testng-users
Thank you for the prompt response.

my understanding is that the test(boolean useTransactions) would be run
once with true and the other with false automatically. Meaning I won't
have to add delegating methods...

Well, I went out and tried this as follows:

@DataProvider(name = "transaction")
public Object[][] createTransactionSettings() {
return new Object[][] {

new Object[] { TxnSupport.LOCAL_TXN },
new Object[] { TxnSupport.JINI_TXN },
};
}

@Parameters({ "transaction" })
@Test
public TestNg(TxnSupport transaction)
{
transactionType = transaction;
}

But, immediately got the following:

org.testng.TestNGException:
Parameter 'transaction' is required by @Parameters on method public
com.framework.TestNg(com.framework.TestNg$TxnSupport)
but has not been defined in D:\cvs\QA\temp-testng-customsuite.xml
at
org.testng.internal.Parameters.createParameters(Parameters.java:102)
at org.testng.internal.Parameters.createParameters(Parameters.java:37)

I looked at the customsuite file it created, and it doesn't contain any
parameters -
but should it? My intention is that it would run it twice with both
parameters.

I can live with the 'permutations' being inside the test and not passed
as arguments,
but this then would have to work as I assume - running each method as
many times
as it's data provider feeds it.

Thank you.

Cédric Beust ♔

unread,
Oct 16, 2006, 3:25:59 PM10/16/06
to testng...@googlegroups.com
On 10/16/06, moran <mor...@gmail.com> wrote:

Thank you for the prompt response.

my understanding is that the test(boolean useTransactions) would be run
once with true and the other with false automatically. Meaning I won't
have to add delegating methods...

Well, I went out and tried this as follows:

        @DataProvider(name = "transaction")
        public Object[][] createTransactionSettings() {
          return new Object[][] {
            new Object[] { TxnSupport.LOCAL_TXN },
            new Object[] { TxnSupport.JINI_TXN },
          };
        }

        @Parameters({ "transaction" })
        @Test
        public TestNg(TxnSupport transaction)
        {
                transactionType = transaction;
        }

But, immediately got the following:

Yup, you didn't follow the snippet of code I posted in my initial email :-)

Replace:

        @Parameters({ "transaction" })
        @Test

with

        @Test(dataProvider = "transaction")

The first version if when you define these parameters in your testng.xml file.

--
Cédric

Reply all
Reply to author
Forward
0 new messages