When a test suite defined in an xml is executed, is it possible to assume that all tests in the suite is executed in the same JVM?

I have a TestNG test suite defined in an xml file. This suite has a suite listener installed.

<listeners>
    <listener class-name="com.my.package.MyListener"/>
</listeners>

In onStart() of the suite listener, I initialize some static class members.

Is it guaranteed that all the test cases later executed in the suite are executed in the same JVM in which onStart() is fired, so that the static variable values I initialized in onStart() are available to all test cases?

Answer

Yes, it is true, the same JVM will be used and therefore the same static fields will be available to all classes.

Nevertheless, I have found that there may be some initialization issues when the test suite is configured to run with multiple parallel threads (e.g. thread-count="5" parallel="tests").

The issues did not reoccur after I added synchronization to the onStart method of my listener class:

public synchronized void onStart(ISuite suite) {
    // Initialization code
}

Leave a Reply

Your email address will not be published. Required fields are marked *