0
@Test(groups = { "all", "smoke" })
class task {
    public void sendTask() {
    }

    public void captureTasks() {
    }

    public void captureTaskEvents() {
    }
}

If I set -Dtest_mode=send, capture and we shall use System.getProperty("test_mode") value to determine in a custom-built.

Now here send test_mode only run sendTasks() methods.

Krishnan Mahadevan
  • 14,121
  • 6
  • 34
  • 66
  • Its not clear what you are asking. Can you please explain a bit as to what you are trying to achieve ? Are you trying to find out a way to run a specific method from the command prompt ? – Krishnan Mahadevan Jan 10 '18 at 03:03
  • Yes, I am trying to use IMethodSelector where if method is specified, all the methods present in different different test cases classes should be executed. mvn -Dtest_mode=sendTasks() should execute all the sendTasks. – user1913685 Jan 10 '18 at 08:55
  • What do you mean by `if method is specified all the methods present in different different test cases classes should be executed` ?Have you considered doing something like [this](https://stackoverflow.com/a/7568737) if you are using Maven as a build tool ? – Krishnan Mahadevan Jan 10 '18 at 08:57
  • Actually problem here is i don't want only a single method to be executed from a single java file, i wanted to execute the method present in all java files. I have tried the way but that is not useful in the current project. in our project we have multiple classes as mentioned above code, so i want method in every class to be executed. M sorry i am new to maven and testng, please help. – user1913685 Jan 10 '18 at 09:32

1 Answers1

1

You can do this easily via a Beanshell method selector in TestNG. Here's a sample.

My test classes look like below

package com.rationaleemotions.stackoverflow.qn48163083;

import org.testng.annotations.Test;

public class SampleA {
    @Test
    public void testForAndroid() {
        System.err.println(getClass().getName() + ".testForAndroid() ran");
    }

    @Test
    public void testForWindows() {
        System.err.println(getClass().getName() + ".testForWindows() ran");
    }
}
package com.rationaleemotions.stackoverflow.qn48163083;

import org.testng.annotations.Test;

public class SampleB {
    @Test
    public void testForAndroid() {
        System.err.println(getClass().getName() + ".testForAndroid() ran");
    }

    @Test
    public void testForWindows() {
        System.err.println(getClass().getName() + ".testForWindows() ran");
    }

}

Here's my suite xml file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="1265_Suite" parallel="false" verbose="4">
    <test name="92" parallel="false">
        <method-selectors>
            <method-selector>
                <script language="beanshell">
                    <![CDATA[
                    methodPattern = System.getProperty("methodPattern");
                    (methodPattern == null || methodPattern.trim().isEmpty()) || testngMethod.getMethodName().contains(methodPattern);
                ]]>
                </script>
            </method-selector>

        </method-selectors>
        <packages>
            <package name="com.rationaleemotions.stackoverflow.qn48163083"/>
        </packages>
    </test>
</suite>

Here I am getting the method name to be executed via the JVM argument -DmethodPattern. If nothing was provided we default to running everything.

For more information on how to work with beanshells in TestNG take a look at my blog post here.

Here's the output of a sample execution with -DmethodPattern=testForAndroid

...
... TestNG 6.13.1 by Cédric Beust (cedric@beust.com)
...
com.rationaleemotions.stackoverflow.qn48163083.SampleA.testForAndroid() ran
com.rationaleemotions.stackoverflow.qn48163083.SampleB.testForAndroid() ran
===== Invoked methods
    SampleA.testForAndroid()[pri:0, instance:com.rationaleemotions.stackoverflow.qn48163083.SampleA@df27fae] 233996206
    SampleB.testForAndroid()[pri:0, instance:com.rationaleemotions.stackoverflow.qn48163083.SampleB@2f0a87b3] 789219251
=====
PASSED: testForAndroid
PASSED: testForAndroid

===============================================
    92
    Tests run: 2, Failures: 0, Skips: 0
===============================================

===============================================
1265_Suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================
Krishnan Mahadevan
  • 14,121
  • 6
  • 34
  • 66
  • Hi Krishnan, Thanks for your reply, I have defined different testng.xml's (ex: testng_send.xml & testng_push.xml). – user1913685 Jan 12 '18 at 09:53
  • Hi Krishnan, Thanks for your reply, Do i need to have anything related to Maven-surefire and anything related to beanshell in my pom. – user1913685 Jan 12 '18 at 10:07