0

I'm currently using IAnnotationTransformer to exclude all tests listed in an XML which contain a particular string in their name, for example:

@Override
@SuppressWarnings("rawtypes")
public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
    if(testMethod.getName().contains("StringGoesHere")){
            annotation.setEnabled(false);
        }

This works perfectly well, however due to the naming convention of my tests, and the vast amount of tests which I need to exclude, the ideal solution would be to find a way to exclude the test if it calls upon a particular method. For example, I want to exclude all tests which call upon the method .clickUserSettings() shown below:

@Test
public void editUserDetailsOnStackOverflow() {
    final StackOverflow so = stackOverflow.loginAsMe()
            .clickUserSettings()
            .changeUserEmail()
            .clickSave()
            .assertChangeSuccessful();
}

Does anyone know how I might be able to achieve this? I feel like I've explored all other avenues (classes, packages, testMethods, groups, xml, etc) And the reason it has to be done in this way is because I currently have 40,000 tests (15,000 of which will likely be excluded depending on the browserMode variable set by the tester) This is the general idea I was hoping to achieve:

@Override
@SuppressWarnings("rawtypes")
public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
    if(testMethod.getAllCalledMethods.contains(".clickUserSettings()")){
            annotation.setEnabled(false);
        }

If this can be done it will solve my issue,

Cheers

  • 1
    Why groups are not fit for purpose? – Xwris Stoixeia Mar 03 '17 at 17:26
  • Well - nothing out of the box will give this to you. Consider writing an external script which runs as a pre-test thing or beforesuite - generates a list of these methods - which you can disable in your annotation transformer. Some ways :http://stackoverflow.com/questions/930289/how-can-i-find-all-the-methods-that-call-a-given-method-in-java. You can do a grep in the files or a quick loop of your included tests..to think of a few solutions – niharika_neo Mar 06 '17 at 07:45

1 Answers1

0

This is not possible with TestNG. TestNG has visibility only to the methods that are annotated with its annotations and its not aware of the code path that a TestNG method traverses during its course of execution. [ Here I am assuming that the method clickUserSettings() is a utility method that your @Test methods perhaps call into, as part of its execution ]

So you have a couple of options

  1. Resort to using groups wherein all methods that call into clickUserSettings() as part of a particular group and then exclude that group from execution.
  2. Create your own custom annotation, annotate all methods that invoke clickUserSettings() using this annotation, and then modify your current AnnotationTransformer to look for this annotation at either the method level or at the class level.
Krishnan Mahadevan
  • 14,121
  • 6
  • 34
  • 66