Get current suite name during @Factory method call

114 views
Skip to first unread message

Curtis W

unread,
Nov 6, 2009, 3:05:27 PM11/6/09
to testng-users
I have a group of tests that are created using an @Factory method, and
are part of a larger suite that I've defined in an xml file. I'd like
these tests to run against one of several environments which are at
different URLs. For normal @Test methods this is not a problem because
in the @BeforeSuite period of testng's lifecycle I can simply call a
method in a class which has the appropriate set-up hard coded.
@Factory runs before @BeforeSuite however, so I can't rely on the same
mechanism.

Is there any way to access the testng framework dynamically to
determine which xml file was used to configure the current test run?

Cédric Beust ♔

unread,
Nov 6, 2009, 3:24:33 PM11/6/09
to testng...@googlegroups.com
Hi Curtis,

You can tell TestNG to pass your factory an ITestContext that contains all the information you need.

Maybe something a bit more flexible for you would be to use a parameter:

<suite name="Single" verbose="1" parallel="false" thread-count="2">

  <parameters>
    <parameter name="foo" value="bar" />
  </parameters>

  <test name="Single">
    <classes>
      <class name="test.factory.FactoryChild" />
    </classes>     
  </test>

</suite>

and your factory:

  @Factory
  public Object[] create(ITestContext ctx) {
    // will print "bar"
    System.out.println("Parameter:" + ctx.getCurrentXmlTest().getParameter("foo"));
    return new Object[] {
    // ...

Does this help?

--
Cedric
--
Cédric


Curtis W

unread,
Nov 6, 2009, 3:47:39 PM11/6/09
to testng-users
That works perfectly. You're a legend!
> ***Cédric
> *

Cédric Beust ♔

unread,
Nov 6, 2009, 3:51:38 PM11/6/09
to testng...@googlegroups.com
On Fri, Nov 6, 2009 at 12:47 PM, Curtis W <gal...@gmail.com> wrote:

That works perfectly. You're a legend!

Wow, thanks :-)

--
Cédric


Reply all
Reply to author
Forward
0 new messages