testng annotations -- how can I pass them diff values each time

90 views
Skip to first unread message

Eyal

unread,
Oct 27, 2021, 6:13:39 AM10/27/21
to testng-users
For example I want to pass to invocationcount = 5
same test with 3
4
10
Threadpool size = 6
7
9
10
100

How can I make the threadpool and invocation count values to be external ?
for example from azure pipeline to have it parametrise ?and fully dynamic ?

Thanks!

⇜Krishnan Mahadevan⇝

unread,
Oct 27, 2021, 6:22:13 AM10/27/21
to testng-users
Please do the following:
  1. Make sure you are using the latest version of TestNG (7.4.0 as of today)
  2. Build an implementation of IAnnotationTransformer interface which works as a listener and then within its transform method (the one that takes in a ITestAnnotation parameter) and flip the desired values. The input form them could be via JVM arguments with a default value if not provided.
  3. Wire in this listener either via the <listener> tag or using the service loader mechanism as described in the documentation https://testng.org/doc/documentation-main.html#listeners-service-loader
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/09c6388f-086b-4ed9-8c79-1c3589cd4866n%40googlegroups.com.

Eyal Avramchik

unread,
Oct 27, 2021, 8:41:45 AM10/27/21
to testng...@googlegroups.com
Hi,

tried that:
public class AnnotationTransformer implements IAnnotationTransformer {

@Override
public void transform(ITestAnnotation annotation, Class testClass,
Constructor testConstructor, Method testMethod) {
if (testMethod.getName().contains("test01")) {
System.out.println("The listener is activated for:-" + testMethod.getName());
annotation.setInvocationCount(3);
System.out.println("Invocation count is set to :-" + annotation.getInvocationCount());
}
But it doesn't do anything, I didn't forgot to define this class as Listener in the test class

‫בתאריך יום ד׳, 27 באוק׳ 2021 ב-13:22 מאת ‪⇜Krishnan Mahadevan⇝‬‏ <‪krishnan.ma...@gmail.com‬‏>:‬


--










 בברכה,
     אייל אברמצ'יק
  טל:  03-5733714
נייד: 054-5565-183

⇜Krishnan Mahadevan⇝

unread,
Oct 27, 2021, 10:52:14 AM10/27/21
to testng-users
Implementations of IAnnotationTransformer should not be wired in using the "@Listeners" annotation. Please fix that if thats how you are trying to wire in your listener.

As I mentioned before, it should be wired in via :

1. <listeners> tag in the testng suite xml file.
2. Using the Service Provider Interface mechanism which is detailed in the documentation link that I shared earlier.

I just now tried this and it works.

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/

Eyal

unread,
Oct 28, 2021, 2:27:55 AM10/28/21
to testng-users
getting this after trying to use service loader:

[TestNG] [WARN] AnnotationTransformer already set

ב-יום רביעי, 27 באוקטובר 2021 בשעה 17:52:14 UTC+3, Krishnan Mahadevan כתב/ה:

⇜Krishnan Mahadevan⇝

unread,
Oct 28, 2021, 2:40:47 AM10/28/21
to testng-users
Can you please create a simple sample project that can be used to reproduce this problem?
You can perhaps upload it into github and share a link (or) if that's not possible, you can maybe attach a zip version of it.

That will be more useful and we can get to the bottom of this much faster.

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/

Eyal

unread,
Oct 28, 2021, 3:14:53 AM10/28/21
to testng-users
Hi,

I can do that, but the issue is not the code, probably someting is missing:

1. I am using my working listener class
2. I define it only in the xml listener tag
3. then when I run the test, invocation count remain 0 as the default

What am I missing ? can you please please share the min of code that works ?
structure ?

thanks....

ב-יום חמישי, 28 באוקטובר 2021 בשעה 09:40:47 UTC+3, Krishnan Mahadevan כתב/ה:

⇜Krishnan Mahadevan⇝

unread,
Oct 28, 2021, 11:46:21 PM10/28/21
to testng-users
Here's the sample. I am using TestNG 7.4.0 (latest released version as of today)

import java.util.concurrent.atomic.AtomicInteger;
import org.testng.annotations.Test;

public class SampleTestClass {

  public static final AtomicInteger counter = new AtomicInteger(0);
  @Test
  public void testMethod() {
    System.err.println(counter.incrementAndGet());
  }
}

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import org.testng.IAnnotationTransformer;
import org.testng.annotations.ITestAnnotation;

public class SimpleTransformer implements IAnnotationTransformer {

  public static final String ITERATIONS_COUNT = "iterations.count";

  @Override
  public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor,
      Method testMethod) {
    int count = Integer.parseInt(System.getProperty(ITERATIONS_COUNT, "1"));
    annotation.setInvocationCount(count);
  }
}
Suite xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="sample_suite" verbose="2">
    <listeners>
        <listener class-name="com.rationaleemotions.dynamic.SimpleTransformer"/>
    </listeners>
    <test name="sample_test" verbose="2">
        <classes>
            <class name="com.rationaleemotions.dynamic.SampleTestClass"/>
        </classes>
    </test>
</suite>

And here's a test case that ensures that this works fine.

import static org.assertj.core.api.Assertions.assertThat;

import java.util.Collections;
import org.testng.TestNG;
import org.testng.annotations.Test;

public class TestNGApiRunner {

  @Test
  public void runTest() {
    System.setProperty(SimpleTransformer.ITERATIONS_COUNT, "5");
    TestNG testng = new TestNG();
    testng.setTestClasses(new Class[]{SampleTestClass.class});
    testng.setListenerClasses(Collections.singletonList(SimpleTransformer.class));
    testng.run();
    assertThat(SampleTestClass.counter.get()).isEqualTo(5);
  }
}

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/

Eyal Avramchik

unread,
Oct 31, 2021, 4:10:33 AM10/31/21
to testng...@googlegroups.com
tried it,
The test is running normally, I would expect the test to run x time as the invocation counter configured, But no.

‫בתאריך יום ו׳, 29 באוק׳ 2021 ב-6:46 מאת ‪⇜Krishnan Mahadevan⇝‬‏ <‪krishnan.ma...@gmail.com‬‏>:‬

⇜Krishnan Mahadevan⇝

unread,
Oct 31, 2021, 11:20:22 AM10/31/21
to testng-users
Not sure what you mean by that. You can actually pass in a different invocation count as a JVM argument.

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/

Reply all
Reply to author
Forward
0 new messages