Unable to set the invocation count using maven/testng with Java

214 views
Skip to first unread message

Ann Millikin

unread,
Apr 17, 2017, 10:35:18 AM4/17/17
to testng-users
Good morning,

I am using TestNG (version 6.8) to set up an selenium automation framework using Java and Maven with a multi-threaded driver on the Grid.  I would like to override the invocation count by specifying a -Dparameter at the command line using maven (mvn).  For example, if I run the following:

mvn clean test -Dgroups=regression -DpositiveTestsToRun=5

I would like the 5 to set how many times the @Test for positiveTests runs.

I've tried this using this in an @BeforeMethod but I don't know how to pass the first argument (the bold null) which is the annotation.  (Btw please excuse the formatting)

@BeforeMethod(alwaysRun = true)

public void beforeMethod(ITestContext context)

{

ITestNGMethod currentTestNGMethod = null;

for (ITestNGMethod testNGMethod : context.getAllTestMethods())

{

if (testNGMethod.getInstance() == this)

{

currentTestNGMethod = testNGMethod;

TestNG tng = new TestNG();

tng.setAnnotationTransformer(new MyTransformer(nullcurrentTestNGMethod.getRealClass(), nullnull));

}

}

}


I've also tried using the ITestNGMethod as follows but it doesn't work either:


// Issue with the below when using both ITestAnnotation annotation,

// ITestContext context

// must be supplied as @Parameters or in @Configuration but googling says

// @Configuration is deprecated

@BeforeMethod(alwaysRun = true)

public void beforeMethod2(ITestContext context)

// ITestAnnotation annotation,

{

ITestNGMethod currentTestNGMethod = null;

System.out.println("============= TestNGMethod.class: " + ITestNGMethod.class);

for (ITestNGMethod testNGMethod : context.getAllTestMethods())

{

if (testNGMethod.getInstance() == this)

{

currentTestNGMethod = testNGMethod;


System.out.println("-------- currentTestNGMethod: " + currentTestNGMethod);

/////////////// doesn't work

currentTestNGMethod.setInvocationCount(getPositiveTestsToRun());

break;

}

}

System.out

.println("============= in @BeforeMethod of DriverBase positiveTestsToRun: " + getPositiveTestsToRun());

// TestNgListener.getTestMethod().setInvocationCount(getPositiveTestsToRun());


System.out.println("=== currentTestNGMethod.getInvocationCount(): " + currentTestNGMethod.getInvocationCount());

System.out.println("currentTestNGMethod.toString(): " + currentTestNGMethod.toString());

}


I've tried so many things to no avail.  Any help would be appreciated!


Best regards,

Ann

SUBRAMANYESWARA RAO BHAVIRISETTY

unread,
Apr 17, 2017, 10:56:01 PM4/17/17
to testng-users
Can you override the transform method provided by IAnnotationTransformer by writing your own listener and then use the listener later in the pom?
 
@Override
        public void transform(ITestAnnotation arg0, Class arg1, Constructor arg2,
                        Method arg3) {
                arg0.setInvocationTimeOut(System.getProperty('timeout'));
                arg0.setInvocationCount(System.getProperty('positiveTestsToRun'));
            LOG.info("Invoking method " + arg3.getName() + " for " + configarr[0] + " times with timeout " + configarr[1] );
        }

<properties>
                                                <property>
                                                        <name>listener</name>
                                                        <value>org.uncommons.reportng.HTMLReporter,org.uncommons.reportng.JUnitXMLReporter,src.TestNGTransformer</value>
                                                </property>
                                        </properties>


Subramanyam


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



--
Subramanyam
Reply all
Reply to author
Forward
0 new messages