Yugandhar
Yugandhar

Reputation: 365

How to find how many testcase are there in TestNG class from another java class

I have two class

public class xyzTest {
@Test
public void TestP1TUNG557(){
    TestHelper.excuteTestcase(TUNG557);
    Assert.assertTrue(TestHelper.TestResult);
}
@Test
public void TestP1TUNG559(){
    TestHelper.excuteTestcase(TUNG559);
    Assert.assertTrue(TestHelper.TestResult);
}
@Test
public void TestP0TUNG558(){
    TestHelper.excuteTestcase(TUNG558);
    Assert.assertTrue(TestHelper.TestResult);
}
}

public class TestHelper {
 public excuteTestcase(String abc)
{
  process(abc)
}
int TotalTescase(String pattern, Class testNGclass)
 {
   How to write here..? plz help
 }
}

suppose if have called TotalTescase(String TestPO, Class xyzTest), it should return 1 and if have called TotalTescase(String TestP1, Class xyzTest) it should return 2. If This is possible to get total test case like this ,plz help me or provide me some link I have searched but i couldnt find. help me

Upvotes: 2

Views: 827

Answers (2)

vijayv
vijayv

Reputation: 1

In dry run try to implement IInvokedMethodListener and override beforeInvocation method

public class Test implements IInvokedMethodListener
{
static int testcount=0;
public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {
        
        testcount=testcount+method.getTestMethod().getInvocationCount();
}

@Override
public void onStart(ISuite suite) {
    // TODO Auto-generated method stub
    
}


@Override
public void onFinish(ISuite suite) {
    System.out.println(testcount);
    
}
}

Upvotes: 0

Pankaj Kumar Katiyar
Pankaj Kumar Katiyar

Reputation: 1494

You can use reflection technique to find out the matching methods in the supplied class like:

     public int TotalTescase(String pattern, Class<?> testNGclass) throws ClassNotFoundException
    {

        int count = 0;

        testNGclass.getClass();
        Class<?> className = Class.forName(testNGclass.getName()); 

        Method[] methods = className.getMethods();

        for(int i=0; i<methods.length; i++)
        {
            String methodName = methods[i].getName();
            System.out.println("Method Name: "+methodName);

            if(methodName.contains(pattern))
            {
                count++;
            }
        }

        return count;

    }

Upvotes: 3

Related Questions