Kishore Mohanavelu
Kishore Mohanavelu

Reputation: 459

Why does @BeforeSuite and @AfterSuite runs in a separate thread?

I have ThreadLocal variables initialized as a part of @BeforeSuite. But since it is running in a separate thread, the initialized variables are not getting passed on to the @Test. Can someone explain to me the reason behind this behavior? Can we make the @BeforeSuite run as a part of the same thread as other annotated methods?

Code

import org.testng.annotations.*;

public class SampleTest {

ThreadLocal<String> s = new ThreadLocal<>();

@BeforeSuite
public void beforeSuite(){
    System.out.println("beforeSuite");
    s.set("Initialisation value");
    System.out.println(Thread.currentThread().getId());
}

@BeforeClass
public void beforeclass(){
    System.out.println("beforeclass");
    System.out.println(Thread.currentThread().getId());
}

@BeforeTest
public void beforetest(){
    System.out.println("beforetest");
    System.out.println(Thread.currentThread().getId());
}

@BeforeMethod
public void beforemethod(){
    System.out.println("beforemethod");
    System.out.println(Thread.currentThread().getId());
}

@AfterMethod
public void aftermethod(){
    System.out.println("aftermethod");
    System.out.println(Thread.currentThread().getId());
}

@AfterTest
public void afterTest(){
    System.out.println("afterTest");
    System.out.println(Thread.currentThread().getId());
}

@AfterClass
public void afterclass(){
    System.out.println("afterclass");
    System.out.println(Thread.currentThread().getId());
}

@AfterSuite
public void afterSuite(){
    System.out.println("afterSuite");
    System.out.println(Thread.currentThread().getId());
}

@Test
public void testMethod(){
    System.out.println(Thread.currentThread().getId());
    System.out.println("My actual Test");
    System.out.println("Value of threadlocal variable s : "+s.get());
}

}

Code Output

beforeSuite
1

beforetest
11

beforeclass
11

beforemethod
11

11
My actual Test
Value of threadlocal variable s : null

aftermethod
11

afterclass
11

afterTest
11

afterSuite
1  

===============================================
Titan Automation
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================

Testng.xml file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Automation" parallel="tests">
    <test name="Test Cases" thread-count="4">
        <classes>
            <class name="org.example.SampleTest"></class>
        </classes>
    </test>
</suite>

Upvotes: 0

Views: 665

Answers (1)

Krishnan Mahadevan
Krishnan Mahadevan

Reputation: 14746

The reason why you have different threads being spun off where your other methods are running is because you have enabled the parallelism strategy by choosing parallel="tests".

If you would like all the configuration methods to be run on the same thread, you should disable parallelism by setting parallel="false" in your <suite> tag.

Upvotes: 1

Related Questions