Setting configuration properties before running test suite

715 views
Skip to first unread message

Karthik Krishnan

unread,
Jul 9, 2009, 3:39:11 PM7/9/09
to testng...@googlegroups.com
I apologize if my subject line is not clear. We are introducing TestNG in our project and so far, my project manager loves what TestNG has to offer. Our application is a web app that depends on certain configured System properties for look up. These properties are initialized by the the server during bootstrapping of J2EE application.

My ear contains packaged jars of projects to be tested within the ear in addition to a war file. Each of the unit tests' of a project are defined in test suite xml file. I have multiple test suite xmls; one for every project. To execute the unit tests, I need to configure system properties used by the application before any of the suites are executed. I am using @BeforeSuite to annotate methods that will set system properties. But I find that I have to repeat this in every project to be packaged as jar. Can I configure the properties once without any duplication, and have all projects invoking that method?

Thanks,

Kartik

Cédric Beust ♔

unread,
Jul 9, 2009, 3:53:10 PM7/9/09
to testng...@googlegroups.com
Hi Kartik,

Couldn't all your tests extend the same base class containing the @BeforeSuite and initialize it with the correct values?  These values could be kept in a .properties file specific to each suite...

Sorry if I misunderstood your statement of the problem...

--
Cédric

Karthik Krishnan

unread,
Jul 9, 2009, 4:03:57 PM7/9/09
to testng...@googlegroups.com
Hi Cedric,

This is what I am doing now. But I have to do it in every project of which the jar is created.If I were to move it out the project and into another one, then my test project would be dependant upon path where ever I define the method annotated with BeforeSuite that just handles initialization.

Is this a good practise?

Thanks,

Kartik

2009/7/9 Cédric Beust ♔ <cbe...@google.com>

Cédric Beust ♔

unread,
Jul 9, 2009, 4:15:04 PM7/9/09
to testng...@googlegroups.com
I don't see a way to avoid it:  whatever code you can factor out of your initialization sequence, it has to be shared by all the projects, right?

I would just focus on trying to minimize the boiler plate you need for each new project, such as:

class Project19Init extends ProjectInit {
  public Project19Init() {
    super("foo.properties");
  }
}

class ProjectInit {
  private String mPropertyFile;
  public ProjectInit(String pf) {
    mPropertyFile = pf;
  }

  @BeforeSuite
  public void init() {
    // read the properties file, initialize the system properties with it
  }
}

Does this sound reasonable?

--
Cédric


Karthik Krishnan

unread,
Jul 9, 2009, 4:23:38 PM7/9/09
to testng...@googlegroups.com
That is exactly what I have done.

/**
 * Root test class amongst the hierarchy of unit tests.
 *
 * <p>This class is deliberately made abstract to make the developers extend
 * this class. Use this class to set up project configurations used by other test classes.
 *
 * @author krishn...@gmail.com
 */
public abstract class BaseTest {
   
    /** Does a one time initialization before the execution of test suite. */
    @BeforeSuite
    public void setUpBeforeSuite() throws Exception {
        System.setProperty("cvPropsDirPath", ".....);
    }

}


2009/7/9 Cédric Beust ♔ <cbe...@google.com>

Cédric Beust ♔

unread,
Jul 9, 2009, 4:30:01 PM7/9/09
to testng...@googlegroups.com
Ah, well good then :-)

If anybody sees any way to further streamline this approach, I'd be interested to know.

--
Cédric

Reply all
Reply to author
Forward
0 new messages