Quantcast

equivalent of IMethodInterceptor for configuration methods?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

equivalent of IMethodInterceptor for configuration methods?

Todd Bradley-2
I created a listener class that implements IMethodInterceptor, so I can modify the list of test methods that are going to be run before any are run. It's working, but can only affect test methods. Is there an equivalent for configuration methods? I want to be able to modify that list before any tests are run.

Ultimately, what I'm trying to do is to discard test methods and configuration methods based on their test groups, at runtime. My test framework inspects the system-under-test, and makes decisions about what groups shouldn't be run. But even though my list of tests is correct (the tests that shouldn't be run are being discarded by my IMethodInterceptor), the @BeforeClass method is still being executed.


Thanks,
Todd.

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: equivalent of IMethodInterceptor for configuration methods?

Julien Herr
Hi,

You can try to add a http://testng.org/javadocs/org/testng/IAnnotationTransformer2.html on your listener and disable configuration methods according to your rules.

Le vendredi 17 février 2017 16:25:49 UTC+1, Todd Bradley a écrit :
I created a listener class that implements IMethodInterceptor, so I can modify the list of test methods that are going to be run before any are run. It's working, but can only affect test methods. Is there an equivalent for configuration methods? I want to be able to modify that list before any tests are run.

Ultimately, what I'm trying to do is to discard test methods and configuration methods based on their test groups, at runtime. My test framework inspects the system-under-test, and makes decisions about what groups shouldn't be run. But even though my list of tests is correct (the tests that shouldn't be run are being discarded by my IMethodInterceptor), the @BeforeClass method is still being executed.


Thanks,
Todd.

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: equivalent of IMethodInterceptor for configuration methods?

Todd Bradley-2
Thanks for the suggestion. It occurred to me that maybe I'm taking totally the wrong approach here. What I'm really trying to do is set the test groups I want to include and exclude at runtime from within my test framework, instead of passing them in via <include/> and <exclude/> tags in my XML. So I think the workflow I want is something like this:

1) Start TestNG with a test suite XML file, that specifies some groups to include and exclude
2) Based on logic in my base class' constructor, add more groups to include and exclude programmatically
3) Then TestNG runs my tests and configuration methods accordingly

My current approach to #2 has been to discard test methods based on group using an IMethodInterceptor. But I realized there's probably a smarter approach that I just don't know. If I could change the include/exclude lists before TestNG decides which test and configuration methods to run, then I can let TestNG do things its normal way, instead of me removing methods in my own code.

Any suggestions?


Thanks,
Todd.




On Sun, Feb 19, 2017 at 2:14 AM, Julien Herr <[hidden email]> wrote:
Hi,

You can try to add a http://testng.org/javadocs/org/testng/IAnnotationTransformer2.html on your listener and disable configuration methods according to your rules.


Le vendredi 17 février 2017 16:25:49 UTC+1, Todd Bradley a écrit :
I created a listener class that implements IMethodInterceptor, so I can modify the list of test methods that are going to be run before any are run. It's working, but can only affect test methods. Is there an equivalent for configuration methods? I want to be able to modify that list before any tests are run.

Ultimately, what I'm trying to do is to discard test methods and configuration methods based on their test groups, at runtime. My test framework inspects the system-under-test, and makes decisions about what groups shouldn't be run. But even though my list of tests is correct (the tests that shouldn't be run are being discarded by my IMethodInterceptor), the @BeforeClass method is still being executed.


Thanks,
Todd.

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: equivalent of IMethodInterceptor for configuration methods?

⇜Krishnan Mahadevan⇝
Todd,
I think IAlterSuiteListener would perhaps fit the bill for you. Here's a sample


package com.rationaleemotions.googleforums.groups;

import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test;

public class TestA {
    @BeforeGroups(groups = {"gradle", "ant"})
    public void beforeGroup() {
        System.err.println("gradle/ant before group got invoked");
    }

    @Test(groups = "gradle")
    public void testMethod() {
        System.err.println("gradle test method got invoked");
    }

    @Test(groups = "ant")
    public void antTestMethod() {
        System.err.println("ant test method got invoked");
    }

}

package com.rationaleemotions.googleforums.groups;

import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test;

public class TestB {
    @BeforeGroups(groups = "maven")
    public void beforeGroup() {
        System.err.println("maven before group got invoked");
    }

    @Test(groups = "maven")
    public void testMethod() {
        System.err.println("maven test method got invoked");
    }
}

package com.rationaleemotions.googleforums.groups;

import org.testng.IAlterSuiteListener;
import org.testng.xml.XmlSuite;

import java.util.Arrays;
import java.util.List;

public class AlterSuiteListener implements IAlterSuiteListener {

    @Override
    public void alter(List<XmlSuite> suites) {
        String groupName = System.getProperty("run.groups");
        if (groupName == null || groupName.trim().isEmpty()) {
            return;
        }
        List<String> groups = Arrays.asList(groupName.split(","));
        for (XmlSuite suite : suites) {
            suite.setIncludedGroups(groups);
        }

    }
}
Here's how the suite xml looks like

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="1265_Suite" parallel="false" verbose="2">
    <listeners>
        <listener class-name="com.rationaleemotions.googleforums.groups.AlterSuiteListener"/>
    </listeners>
    <test name="92" parallel="false" preserve-order="true">
        <packages>
            <package name="com.rationaleemotions.googleforums.groups"/>
        </packages>
    </test>
</suite>
And here's the output
mvn clean test -DsuiteXmlFile=groups.xml -Drun.groups=ant
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running TestSuite
[XmlSuite] [WARN] 'parallel' value 'false' is deprecated, default value will be used instead: 'none'.
[XmlSuite] [WARN] 'parallel' value 'false' is deprecated, default value will be used instead: 'none'.
...
... TestNG 6.10 by Cédric Beust ([hidden email])
...

gradle/ant before group got invoked
ant test method got invoked
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.301 sec - in TestSuite

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

Would this solve your problem ?

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 Scribbings @ http://rationaleemotions.wordpress.com/

On Wed, Mar 1, 2017 at 4:37 AM, Todd Bradley <[hidden email]> wrote:
Thanks for the suggestion. It occurred to me that maybe I'm taking totally the wrong approach here. What I'm really trying to do is set the test groups I want to include and exclude at runtime from within my test framework, instead of passing them in via <include/> and <exclude/> tags in my XML. So I think the workflow I want is something like this:

1) Start TestNG with a test suite XML file, that specifies some groups to include and exclude
2) Based on logic in my base class' constructor, add more groups to include and exclude programmatically
3) Then TestNG runs my tests and configuration methods accordingly

My current approach to #2 has been to discard test methods based on group using an IMethodInterceptor. But I realized there's probably a smarter approach that I just don't know. If I could change the include/exclude lists before TestNG decides which test and configuration methods to run, then I can let TestNG do things its normal way, instead of me removing methods in my own code.

Any suggestions?


Thanks,
Todd.




On Sun, Feb 19, 2017 at 2:14 AM, Julien Herr <[hidden email]> wrote:
Hi,

You can try to add a http://testng.org/javadocs/org/testng/IAnnotationTransformer2.html on your listener and disable configuration methods according to your rules.


Le vendredi 17 février 2017 16:25:49 UTC+1, Todd Bradley a écrit :
I created a listener class that implements IMethodInterceptor, so I can modify the list of test methods that are going to be run before any are run. It's working, but can only affect test methods. Is there an equivalent for configuration methods? I want to be able to modify that list before any tests are run.

Ultimately, what I'm trying to do is to discard test methods and configuration methods based on their test groups, at runtime. My test framework inspects the system-under-test, and makes decisions about what groups shouldn't be run. But even though my list of tests is correct (the tests that shouldn't be run are being discarded by my IMethodInterceptor), the @BeforeClass method is still being executed.


Thanks,
Todd.

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: equivalent of IMethodInterceptor for configuration methods?

Todd Bradley-2
I think this will solve my problem perfectly! Not only that, but this is one of the clearest TestNG examples I've ever seen. Thank you so much! 


Cheers,
Todd. 


On Tue, Feb 28, 2017 at 8:01 PM ⇜Krishnan Mahadevan⇝ <[hidden email]> wrote:
Todd,
I think IAlterSuiteListener would perhaps fit the bill for you. Here's a sample


package com.rationaleemotions.googleforums.groups;

import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test;

public class TestA {
    @BeforeGroups(groups = {"gradle", "ant"})
    public void beforeGroup() {
        System.err.println("gradle/ant before group got invoked");
    }

    @Test(groups = "gradle")
    public void testMethod() {
        System.err.println("gradle test method got invoked");
    }

    @Test(groups = "ant")
    public void antTestMethod() {
        System.err.println("ant test method got invoked");
    }

}

package com.rationaleemotions.googleforums.groups;

import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test;

public class TestB {
    @BeforeGroups(groups = "maven")
    public void beforeGroup() {
        System.err.println("maven before group got invoked");
    }

    @Test(groups = "maven")
    public void testMethod() {
        System.err.println("maven test method got invoked");
    }
}

package com.rationaleemotions.googleforums.groups;

import org.testng.IAlterSuiteListener;
import org.testng.xml.XmlSuite;

import java.util.Arrays;
import java.util.List;

public class AlterSuiteListener implements IAlterSuiteListener {

    @Override
    public void alter(List<XmlSuite> suites) {
        String groupName = System.getProperty("run.groups");
        if (groupName == null || groupName.trim().isEmpty()) {
            return;
        }
        List<String> groups = Arrays.asList(groupName.split(","));
        for (XmlSuite suite : suites) {
            suite.setIncludedGroups(groups);
        }

    }
}
Here's how the suite xml looks like

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="1265_Suite" parallel="false" verbose="2">
    <listeners>
        <listener class-name="com.rationaleemotions.googleforums.groups.AlterSuiteListener"/>
    </listeners>
    <test name="92" parallel="false" preserve-order="true">
        <packages>
            <package name="com.rationaleemotions.googleforums.groups"/>
        </packages>
    </test>
</suite>
And here's the output
mvn clean test -DsuiteXmlFile=groups.xml -Drun.groups=ant
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running TestSuite
[XmlSuite] [WARN] 'parallel' value 'false' is deprecated, default value will be used instead: 'none'.
[XmlSuite] [WARN] 'parallel' value 'false' is deprecated, default value will be used instead: 'none'.
...
... TestNG 6.10 by Cédric Beust ([hidden email])
...

gradle/ant before group got invoked
ant test method got invoked
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.301 sec - in TestSuite

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

Would this solve your problem ?

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 Scribbings @ http://rationaleemotions.wordpress.com/

On Wed, Mar 1, 2017 at 4:37 AM, Todd Bradley <[hidden email]> wrote:
Thanks for the suggestion. It occurred to me that maybe I'm taking totally the wrong approach here. What I'm really trying to do is set the test groups I want to include and exclude at runtime from within my test framework, instead of passing them in via <include/> and <exclude/> tags in my XML. So I think the workflow I want is something like this:

1) Start TestNG with a test suite XML file, that specifies some groups to include and exclude
2) Based on logic in my base class' constructor, add more groups to include and exclude programmatically
3) Then TestNG runs my tests and configuration methods accordingly

My current approach to #2 has been to discard test methods based on group using an IMethodInterceptor. But I realized there's probably a smarter approach that I just don't know. If I could change the include/exclude lists before TestNG decides which test and configuration methods to run, then I can let TestNG do things its normal way, instead of me removing methods in my own code.

Any suggestions?


Thanks,
Todd.




On Sun, Feb 19, 2017 at 2:14 AM, Julien Herr <[hidden email]> wrote:
Hi,

You can try to add a http://testng.org/javadocs/org/testng/IAnnotationTransformer2.html on your listener and disable configuration methods according to your rules.


Le vendredi 17 février 2017 16:25:49 UTC+1, Todd Bradley a écrit :
I created a listener class that implements IMethodInterceptor, so I can modify the list of test methods that are going to be run before any are run. It's working, but can only affect test methods. Is there an equivalent for configuration methods? I want to be able to modify that list before any tests are run.

Ultimately, what I'm trying to do is to discard test methods and configuration methods based on their test groups, at runtime. My test framework inspects the system-under-test, and makes decisions about what groups shouldn't be run. But even though my list of tests is correct (the tests that shouldn't be run are being discarded by my IMethodInterceptor), the @BeforeClass method is still being executed.


Thanks,
Todd.

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
--
Sent from Gmail Mobile

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Loading...