Parameterizing TestNG test externally

2,119 views
Skip to first unread message

steve cochran

unread,
Oct 1, 2007, 11:55:18 AM10/1/07
to testng...@googlegroups.com
We are considering using TestNG to drive some automated tests at the company where I work. I would like to be able to parameterize these test externally, such that they can be run by different people and across different test hardware configurations without changing the testng.xml files if possible.

Basically we have a set of different configuration files that are read by the tests and are shared across all of the tests suites within our automation framework. Something like "large_config.properties," "small_config.properties," "HA_config.properties," etc. In each of our testng.xml files we have a parameter up front that gets passed to the @BeforeSuite method to configure things at startup, like this:

<parameter name="parameterFile" value="large_config.properties"/>

I am running TestNG across a large number of suites each with their own testng.xml file that has this parameter definition in it. I plan to use Ant to launch the task. What I would like to be able to do is to set the value for the config file name from the ant command line, something like:

ant -DparameterFile="HA_config.properties" run_testng

I am not finding a convienent way to accomplish this. The only solution that I have is to use some sort of an ant replace task to edit all of the testng.xml files as a part of the run_testng target. I can make this work, but was hoping for something more elegant. Does anyone have any ideas or suggestions? Is there any way in TestNG to override the definition of a parameter defined in the testng.xml file from the TestNG command line?

thanks,
steve
---------------------------------------------------------------------
Posted via Jive Forums
http://forums.opensymphony.com/thread.jspa?threadID=133382&messageID=205104#205104

Alexandru Popescu ☀

unread,
Oct 1, 2007, 1:17:28 PM10/1/07
to testng...@googlegroups.com

Steve the TestNG ant task allows you to declare environment variables
(-D). Or is it something else you are looking for?

./alex
--
.w( the_mindstorm )p.
TestNG co-founder
EclipseTestNG Creator

steve cochran

unread,
Oct 1, 2007, 2:56:12 PM10/1/07
to testng...@googlegroups.com
Alex,

How can I pick up the value of the environment variable in my test suite then? For example, there are two different configuration files to run tests against: "small_config.properties" and "large_config.properties". When I run in my development environment, I want to use "small_config," but when the tests run in production I want to use "large_config." In my testng.xml files I declare a parameter:

<parameter name="parameterFile" value="large_config.properties"/>

And in my test.java file I declare:

@Parameters( {"parameterFile"} )
@BeforeSuite
public void initVariables(String parameterFile) {

Now when I run in my development environment, I want to pass "small_config.properties" through as the value for "parameterFile."

It could be easy and I'm just missing the obvious. But I can't see a clear way to pass a command line value through ant and all the way into my test suite.

thanks,
steve


---------------------------------------------------------------------
Posted via Jive Forums

http://forums.opensymphony.com/thread.jspa?threadID=133382&messageID=205242#205242

Alexandru Popescu ☀

unread,
Oct 1, 2007, 3:51:12 PM10/1/07
to testng...@googlegroups.com
As you said you can use an env variable: ant -DsomeVar=value

and then in your setup you just pick that value using
System.getProperty("someVar")....


./alex
--
.w( the_mindstorm )p.
TestNG co-founder
EclipseTestNG Creator

On 10/1/07, steve cochran <testng...@opensymphony.com> wrote:
>

Mark Derricutt

unread,
Oct 1, 2007, 4:56:56 PM10/1/07
to testng...@googlegroups.com
I was thinking the other day it would be nice if TestNG's @Parameters annotation would/could pick up system properties as well as suite.xml defined properties (suite.xml overriding probably).  This would keep parameter definition/usage in the same place in the code without having to necessarily think about where they came from (although that information could be handy for inclusion in the reports).

I could see having the system properties just appear could be seen as a con against doing this, so maybe suite.xml could have a variation of the <property> declaration that loads/pulls in the system properties (or even a .properties file) - leaving the location of properties up to the developer?

Cédric Beust ♔

unread,
Oct 1, 2007, 5:31:57 PM10/1/07
to testng...@googlegroups.com
On Oct 1, 2007 1:56 PM, Mark Derricutt <ma...@talios.com> wrote:
I was thinking the other day it would be nice if TestNG's @Parameters annotation would/could pick up system properties as well as suite.xml defined properties (suite.xml overriding probably).  This would keep parameter definition/usage in the same place in the code without having to necessarily think about where they came from (although that information could be handy for inclusion in the reports).

I could see having the system properties just appear could be seen as a con against doing this, so maybe suite.xml could have a variation of the <property> declaration that loads/pulls in the system properties (or even a .properties file) - leaving the location of properties up to the developer?

Actually, I don't see much harm in implicitly creating a TestNG parameter for each system property defined...  Is it possible to enumerate all the system properties?

--
Cédric

Alexandru Popescu ☀

unread,
Oct 1, 2007, 6:04:26 PM10/1/07
to testng...@googlegroups.com

System.getProperties()... however this is not needed. We just have to
try to fetch it from testng.xml and if not found then try to get it
from sys properties using System.getProperty(name).

./alex
--
.w( the_mindstorm )p.
TestNG co-founder
EclipseTestNG Creator

> --
> Cédric
>

Cédric Beust ♔

unread,
Oct 1, 2007, 6:26:15 PM10/1/07
to testng...@googlegroups.com
On Oct 1, 2007 3:04 PM, Alexandru Popescu ☀ <the.mindstor...@gmail.com> wrote:

System.getProperties()... however this is not needed. We just have to
try to fetch it from testng.xml and if not found then try to get it
from sys properties using System.getProperty(name).

Sounds reasonable.  We should make sure to modify the logic that throws an error if a parameter is used in an annotation but not defined in testng.xml, but besides that, it sounds easy to do, harmless and useful...

--
Cédric

Alexandru Popescu ☀

unread,
Oct 2, 2007, 8:35:11 AM10/2/07
to testng...@googlegroups.com
Already implemented in trunk r.517.

./alex
--
.w( the_mindstorm )p.
TestNG co-founder
EclipseTestNG Creator

On 10/2/07, Cédric Beust ♔ <cbe...@google.com> wrote:
>
>

Reply all
Reply to author
Forward
0 new messages