Use IAnnotationTransformer to disable testcases

31 views
Skip to first unread message

mike

unread,
Jul 5, 2022, 5:36:28 AM7/5/22
to testng-users
Hi,

I have written an AnnotationTransformer to disable certain tests.

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

import org.testng.IAnnotationTransformer;
import org.testng.annotations.ITestAnnotation;

public class AnnotationTransformer implements IAnnotationTransformer {

    public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
        if (checkOsAndtestCaseName(testMethod)) {
            annotation.setEnabled(false);
        }
    }

 
    private boolean checkOsAndtestCaseName(Method testMethod) {
        if (testMethod.getName().toLowerCase().contains("win")
                && System.getProperty("os.name").toLowerCase().equals("linux")) {
            return true;
        } else if (testMethod.getName().toLowerCase().contains("unix")
                && System.getProperty("os.name").toLowerCase().equals("windows")) {
            return true;
        }
        return false;
    }

}

In my testcase I try to use it with.

@Listeners(AnnotationTransformer.class)

Is this correct?

Why I ask is because I found that the doc is outdated.

https://testng.org/doc/documentation-main.html#annotationtransformers

This is not valid anymore since setAnnotationTransformer() is private.
TestNG tng = new TestNG();
tng.setAnnotationTransformer(new MyTransformer());

br,

//mike

⇜Krishnan Mahadevan⇝

unread,
Jul 5, 2022, 7:03:25 AM7/5/22
to testng-users
Mike, 

You would need to refer to the IAnnotationTransformer via either the <listeners> tag in your suite file (or) using the ServiceLoader approach as detailed in the documentation.


The TestNG API is typically used (the code snippet you shared refers to that) when you have a need to build some customisations on top of TestNG execution engine.

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/cc8d3447-6185-426f-bd65-9d3a1dcf83c1n%40googlegroups.com.

mike

unread,
Jul 5, 2022, 7:22:51 AM7/5/22
to testng-users
Hi,

So you cannot use the way I used? For what reason, since it is a listener and listner can be added in mulitple ways.


Or if you prefer to define these listeners in Java:

@Listeners({ com.example.MyListener.class, com.example.MyMethodInterceptor.class })

public class MyTest {

  // ...

}

as stated in the doc.

br,

//mike

⇜Krishnan Mahadevan⇝

unread,
Jul 5, 2022, 7:25:13 AM7/5/22
to testng-users
Mike,

The documentation explains the reason behind why IAnnotationTransformer is an exception to the way in which listeners can be wired in.

Quoting the documentation for your reference

The @Listeners annotation can contain any class that extends org.testng.ITestNGListener except IAnnotationTransformer and IAnnotationTransformer2. The reason is that these listeners need to be known very early in the process so that TestNG can use them to rewrite your annotations, therefore you need to specify these listeners in your testng.xml file.


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/

mike

unread,
Jul 5, 2022, 8:47:12 AM7/5/22
to testng-users
Hi,

Ok it seems to work with one side effect in my maven module project.


[INFO] --- maven-surefire-plugin:3.0.0-M6:test (default-cli) @ data-model ---

[WARNING] The POM for com.google.guava:guava:jar:16.0.1 is invalid, transitive dependencies (if any) will not be available, enable debug logging for more details

[INFO] Using auto detected provider org.apache.maven.surefire.testng.TestNGProvider

[INFO]

[INFO] -------------------------------------------------------

[INFO] T E S T S

[INFO] -------------------------------------------------------

Picked up JAVA_TOOL_OPTIONS: -Dmaven.ext.class.path="/repo/jenkins/jenkinshost/****/exechost/workspace/cl_premerge_eadnmak2/proj/cl1@tmp/withMaven5915da10/pipeline-maven-spy.jar" -Dorg.jenkinsci.plugins.pipeline.maven.reportsFolder="/repo/jenkins/ jenkinshost /****/ exechost /workspace/cl_premerge_eadnmak2/proj/cl1@tmp/withMaven5915da10"

[INFO] Running TestSuite

[ERROR] org.testng.ITestNGListener: Provider com.ericsson.commonlibrary.netconf.transport.AnnotationTransformer not found

[INFO]

[INFO] Results:

I am not sure why this is picked up in all my modules since I only added it to one module.

Any ideas?

//mike

⇜Krishnan Mahadevan⇝

unread,
Jul 5, 2022, 8:51:18 AM7/5/22
to testng-users
If the module that is adding this via a service loader is being used by other modules as a test dependency, then it would get invoked for all other modules.

If that's not your scenario, it would be good if you could please help create a sample multi module maven project that I can use to reproduce the problem. I would be better poised to answer what may be going wrong here.


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