Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Can not set different attributes to "iTestContext" while do parallel test class execution.

55 views
Skip to first unread message

Hasini weerasooriya

unread,
Apr 24, 2024, 7:59:21 AM4/24/24
to testng-users
I'm trying to run multiple test classes in parallel. For example, I'm running 2 test classes in parallel using xml file. In my test classes I used two different @BeforeClass annotations in which I try to set two different attributes for "iTestContext". But While I'm running the test classes the "iTestContext" value is always the same.

My expectation is to execute parallel test with @BeforeClass annotation setting different values ​​for "iTestContext" in different test classes.

Sample Script:

Test class 01:
@Listeners(CustomITestListener.class)
public class SampleTest01 {

   @BeforeClass(alwaysRun = true)
   public void init(ITestContext iTestContext) {
          iTestContext.setAttribute("feature""Login");
   }

   @Test(description = "Sample Test 1")
   public void testSample1() {
   ...
   }
}

Test class 02:
@Listeners(CustomITestListener.class)
public class SampleTest02 {

   @BeforeClass(alwaysRun = true)
   public void init(ITestContext iTestContext) {
          iTestContext.setAttribute("feature""Create Page");
   }

   @Test(description = "Sample Test 2")
   public void testSample2() {
   ...
   }
}

xml Script:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Thread Local Test Suite" parallel="classes" thread-count="20">
   <test name="Thread Local Tests" >
      <classes>
         <class name="ThreadLocalTests.SampleTest01"/>
         <class name="ThreadLocalTests.SampleTest02"/>
      </classes>
   </test>
</suite>

Requesting support on this issue.

Regards,
Hasini Weerasooriya

Krishnan Mahadevan

unread,
Apr 24, 2024, 9:31:48 AM4/24/24
to testng...@googlegroups.com
This works fine on the latest released TestNG version (7.10.1).
Please try again using this version.


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribblings @ https://rationaleemotions.com/

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/testng-users/b93d4ed1-5370-48d3-b1a4-3195bec34822n%40googlegroups.com.

Hasini weerasooriya

unread,
Apr 24, 2024, 10:43:35 AM4/24/24
to testng-users
Hi  Krishnan,

Thanks for your response. I tried with the latest released TestNG version 7.10.1 and it also had the same problem.

Regards,
Hasini Weerasooriya

Krishnan Mahadevan

unread,
Apr 24, 2024, 11:01:22 AM4/24/24
to testng...@googlegroups.com
  • If you are trying to work with a single <test> tag, then you will ONLY have 1 ITestContext object.
  • Attributes within an ITestContext are stored as Key/Value Pairs.
  • If you use the same key, you are going to overwrite the values.

The right way to do this would be to split your test classes into multiple <test> tags wherein each <test> tag contains exactly 1 test class and then you choose parallel as “tests” (This tells TestNG to run all my <test> tags in parallel). This gives you the same parallelism that you are going to get when you use parallel=“classes” but with just 1 <test> tag.


Here’s the sample that am working with.


import org.testng.ITestContext;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class SampleTestClass {

@BeforeClass(alwaysRun = true)
public void init(ITestContext iTestContext) {
        System.err.println("Setting feature for " + iTestContext.getName());
        iTestContext.setAttribute("feature", "Login");
}

@Test(description = "Sample Test 1")
public void testSample1() {
}
}


import org.testng.ITestContext;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class SampleTestClass2 {

@BeforeClass(alwaysRun = true)
public void init(ITestContext iTestContext) {
        System.err.println("Setting feature for " + iTestContext.getName());
        iTestContext.setAttribute("feature", "Create Page");
}

@Test(description = "Sample Test 2")
public void testSample2() {
}
}



import org.testng.IReporter;
import org.testng.ISuite;
import org.testng.ISuiteResult;
import org.testng.xml.XmlSuite;

import java.util.List;

public class Printer implements IReporter {

@Override
public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
suites.stream()
.flatMap(it -> it.getResults().values().stream())
.map(ISuiteResult::getTestContext)
.forEach(it -> {
System.err.println("Printing attributes for " + it.getName());
it.getAttributeNames()
.forEach(attribute -> System.err.println(attribute + ": " + it.getAttribute(attribute)));
});
}
}


<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="2980_suite" verbose="2" parallel="tests">
<listeners>
<listener class-name="com.rationaleemotions.testnggroups.attributes.Printer"/>
</listeners>
<test name="login_feature" verbose="2">
<classes>
<class name="com.rationaleemotions.testnggroups.attributes.SampleTestClass"/>
</classes>
</test>
<test name="create_page_feature" verbose="2">
<classes>
<class name="com.rationaleemotions.testnggroups.attributes.SampleTestClass2"/>
</classes>
</test>
</suite>

Output

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
...
... TestNG 7.10.0 by Cédric Beust (ced...@beust.com)
...

Setting feature for create_page_feature
Setting feature for login_feature

===============================================
2980_suite
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================

Printing attributes for login_feature
feature: Login
Printing attributes for create_page_feature
feature: Create Page

Process finished with exit code 0



Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribblings @ https://rationaleemotions.com/

Hasini weerasooriya

unread,
Apr 24, 2024, 11:24:08 AM4/24/24
to testng-users
I'll try with this.  Thank you for the detailed response, Krishnan.

Regards,
Hasini Weerasooriya

Reply all
Reply to author
Forward
0 new messages