0

I want to Skip (so I will see at the report that the tests / classes were skipped) with ITestListener, what I did is the listener class like so:

 @Override public void onStart(ITestContext context) { String deviceName = context.getCurrentXmlTest().getParameter("deviceName"); System.out.println("device name : "+deviceName); String className = context.getClass().getSimpleName(); System.out.println("class name : "+className); if(!deviceName.contains("Galaxy S8")) { System.out.println("Skipping class name : "+className); throw new SkipException("Skipping class: " + className); } } 

and I have test class :

@Test(priority = 1) public void installAndDenyPhoneAccess() { onePlus6ProInstallProcess.denyPhoneAccess(); } 

at the testng.xml I added the listener:

 <?xml version="1.0" encoding="UTF-8"?> <suite name="Suite" parallel = "tests" configfailurepolicy="continue"> <listeners> <listener class-name="com.qa.listeners.TestListener" /> </listeners> <test name="Samsung Note 10 - 5G"> <listeners> <listener class-name="com.qa.listeners.TestListener" /> </listeners> <parameter name="emulator" value="false" /> <parameter name="platformName" value="Android" /> <parameter name="udid" value="RPM" /> <parameter name="deviceName" value="Note 10 - 5G" /> <parameter name="systemPort" value="10000" /> <parameter name="chromeDriverPort" value="11000" /> <classes> <class name="note5.Installation"/> </classes> </test> <test name="Redmi 6A"> <listeners> <listener class-name="com.qa.listeners.TestListener" /> </listeners> <parameter name="emulator" value="false" /> <parameter name="platformName" value="Android" /> <parameter name="udid" value="192b" /> <parameter name="deviceName" value="Redmi 6A" /> <parameter name="systemPort" value="10000" /> <parameter name="chromeDriverPort" value="11000" /> <classes> <class name="redmi6A.Installation"/> </classes> </test> <test name="OnePlus 6 Pro"> <parameter name="emulator" value="false" /> <parameter name="platformName" value="Android" /> <parameter name="udid" value="73" /> <parameter name="deviceName" value="onePlus 6Pro" /> <parameter name="systemPort" value="10000" /> <parameter name="chromeDriverPort" value="11000" /> <classes> <class name="onePlus6Pro.Installation"/> </classes> </test> <test name="Galaxy S8"> <parameter name="emulator" value="false" /> <parameter name="platformName" value="Android" /> <parameter name="udid" value="ce3cb10c" /> <parameter name="deviceName" value="Galaxy S8" /> <parameter name="systemPort" value="10000" /> <parameter name="chromeDriverPort" value="11000" /> <classes> <class name="galaxyS8.Installation"/> </classes> </test> <test name="Samsung A21s"> <listeners> <listener class-name="com.qa.listeners.TestListener" /> </listeners> <parameter name="emulator" value="false" /> <parameter name="platformName" value="Android" /> <parameter name="udid" value="RdM" /> <parameter name="deviceName" value="Samsung A21s" /> <parameter name="systemPort" value="10000" /> <parameter name="chromeDriverPort" value="11000" /> <classes> <class name="a21S.Installation"/> </classes> </test> 

now the tests are not skipped and anyhow not all of them when runing together, only when run two. So how to skip it? and also how to skip the all class if it can?

8
  • 1
    All the code looks correct. Can you also print System.out.println(testMethod.getDeclaringClass().getName()) ? Commented Jan 27, 2022 at 9:53
  • 1
    I can confirm that if your class contains a12S in the name and annotation.setEnabled(false) invoked the method from this class will not be executed. I've tested this behavior with testNG 7.5 on a simple example. Commented Jan 27, 2022 at 10:19
  • 1
    If something doesn't work, the issue might be: 1) your test-class name doesn't contain a12S. 2) you are using some testNG version, which has a bug (it's unlikely) 3) you've applied multiple IAnnotationTransformer listeners. One disables the test, but the other enables. 4) listener applied in the code on class, not in xml (but you sayed that it's in xml) 5) junit @Test annotation instead of testng. I've mentioned all the potential reasouns I might remember. Commented Jan 27, 2022 at 10:25
  • 1
    This listener doesn't set Skip status, it just disables these test methods. So they aren't present in test reports at all. And not invoked too. 4) is OK, this is correct from your answer. Commented Jan 27, 2022 at 10:51
  • 1
    Also, @Before/@After methods can be executed(if exist) for the class, even if all its methods were marked as disabled by the listener. Commented Jan 27, 2022 at 10:54

1 Answer 1

1

IAnnotationTransformer is used to modify the @Test annotation. And there is no option to skip a method using the @Test annotation. In order to forcefully skip it, you need to throw a SkipException.

From the code, it seems that you need to skip the test based on the class name alone and method names are not relevant. So I would suggest the use of ITestListener by overriding the onTestStart method as below:

public class MyTestListener implements ITestListener { @Override public void onTestStart(ITestResult result) { String className = result.getTestClass().getName(); if(className.contains("abc") || className.contains("xyz")) { throw new SkipException("Skipping class: " + className); } } } 

If you want to do this at the method level, you could use IInvokedMethodListener:

public class MyListener implements IInvokedMethodListener { @Override public void beforeInvocation(IInvokedMethod method, ITestResult result) { Map<String, String> paramMap = method.getTestMethod() .findMethodParameters(result.getTestContext().getCurrentXmlTest()); String className = method.getTestMethod().getTestClass().getName(); if(className.contains(paramMap.get("deviceName"))) { throw new SkipException("Skipping class: " + className); } } } 
Sign up to request clarification or add additional context in comments.

20 Comments

Hoo sounds right, that code can get an parameter from the maven to testNG and to work according to the @Parameter ?
@nuzooo Yes you can. To get the parameters passed to the method, you could use result.getParameters(). To get all the parameters specified in the suite file, you could use result.getTestContext().getCurrentXmlTest().getAllParameters(). To pass from maven, you might have to use the sure fire plugin
Yes I already use the sureFire plugin and to there I want to point exactly, to take the params from there, to pass them to the listener(code) and than to excute the code according to that
@nuzooo I think you could do that using the code i have added in my previous comment
OK Thank you, I will try that and update here when I will have results
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.