How to find if the invocationCount attribute is set on @Test method

152 views
Skip to first unread message

VanderRock s

unread,
Apr 12, 2022, 4:30:37 PM4/12/22
to testng-users
I want to find a way to check if the invocationCount attribute is set to an @Test method. Using this information I want the testName attribute to be set to testID_{invocation count}. But I am unable to understand how to get this information. The closest I have come is as follows:

testContext.getAllTestMethods()[0].getInvocationCount()

The problem with above is that if invocationCount is not provided as an attribute to @Test, I still get the value as 1 using the above method. This might not work as that if condition will be set for all TestMethods irrespective of if invocationCount is set or not. Thanks

VanderRock s

unread,
Apr 12, 2022, 8:10:27 PM4/12/22
to testng-users
Also not sure why, but I get my testName as checkBlobCount_org.testng.TestRunner@35835e65

My test method is as follows:

    @FrameworkAnnotation(author = { "abc" }, deviceType="None")
    @Test(description = "Verify history of database files are maintained on Azure", invocationCount = 4, dependsOnMethods= {"TP452_038"}, priority = 1)
    public void checkBlobCount(ITestContext testContext)
    {
        ExtentLogger.logInfo("Title: Verify history of database files are maintained on Azure");

        //Get tablet time after container exists
        LocalDateTime beforeBlobCreationTime = TabletIO.getTabletDateTime_LocalDateTime();

        //Verification: Check if a new blob is created after every azure uploadInterval time
        ExtentLogger.logInfo("Verification: Check if a new blob is created after every azure uploadInterval time");
       
        //Setup for Result report
          ResultWriterParams.testCaseId = "TP452_040";  
          this.testID = new NTestCase(ResultWriterParams.testCaseId, TP_ID);
             
        //Get invocationCount value starts counting from 0. To take into consideration the blob created in previous testcase and this testcase
          // adding value 2 to the currentInvocation count will give the right value
          System.out.println("testContext.getAllTestMethods()[1].getCurrentInvocationCount(): " + testContext.getAllTestMethods()[1].getCurrentInvocationCount());
        int currentCount = (testContext.getAllTestMethods()[1].getCurrentInvocationCount()) + 2;

        TP452_040.has(
                new AcceptanceCriteria("New blob is created after upload interval time of " + (ExternalToolConstants.Azure_CloudBackupInterval) + " secs")
                .with(new ExpectedResult<Boolean>(true)),
               
                new AcceptanceCriteria("Blob " + currentCount + " is created successfully")
                .with(new ExpectedResult<Integer>(currentCount))
            );
           
        TP452_040.evaluateAC(azure.doesBlobExistAfterWaitTime(beforeBlobCreationTime, ExternalToolConstants.Azure_CloudBackupInterval));
        TP452_040.evaluateAC(azure.getNumberOfBlobsInContainer());
       
        Assert.assertTrue(TP452_040.result(), TP452_040.listACs());
    }

Krishnan Mahadevan

unread,
Apr 12, 2022, 9:35:52 PM4/12/22
to testng...@googlegroups.com
I believe that the invocationCount attribute has a default value of 1. Cant you not assume that any value greater than 1 indicates that an invocation value was set at a Test level ?

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"

From: testng...@googlegroups.com <testng...@googlegroups.com> on behalf of VanderRock s <vander...@gmail.com>
Sent: Wednesday, April 13, 2022 2:00:37 AM
To: testng-users <testng...@googlegroups.com>
Subject: [testng-users] How to find if the invocationCount attribute is set on @Test method
 
I want to find a way to check if the invocationCount attribute is set to an @Test method. Using this information I want the testName attribute to be set to testID_{invocation count}. But I am unable to understand how to get this information. The closest I have come is as follows:

testContext.getAllTestMethods()[0].getInvocationCount()

The problem with above is that if invocationCount is not provided as an attribute to @Test, I still get the value as 1 using the above method. This might not work as that if condition will be set for all TestMethods irrespective of if invocationCount is set or not. Thanks

--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/testng-users/b5fabf6e-0207-4e09-a14d-33954a55fc93n%40googlegroups.com.

Krishnan Mahadevan

unread,
Apr 12, 2022, 9:37:22 PM4/12/22
to testng...@googlegroups.com
>>>> Also not sure why, but I get my testName as checkBlobCount_org.testng.TestRunner@35835e65 

How exactly are you getting the testName? Can you show that part ? 

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
Sent: Wednesday, April 13, 2022 5:40:27 AM
To: testng-users <testng...@googlegroups.com>
Subject: [testng-users] Re: How to find if the invocationCount attribute is set on @Test method
 
--
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...@googlegroups.com.

VanderRock s

unread,
Apr 12, 2022, 9:45:27 PM4/12/22
to testng-users
I have this in my parent class to set the testName attribute. Note that the TestMethod might be either a normal testcase or dataprovider (with using spreadsheet or one where the dataprovider is directly defined in the testclass) or invocation count testcase. Hence I have so many if else clauses:

@BeforeMethod(alwaysRun=true)
    public void setUpTest(Method method, Object[] testData, ITestContext ctx) {    
        log.info("BeforeMethod from BaseTestSection");
        try {
            Map<String,String> map = new HashMap<String,String>();
            //For tests that use invocation count
                  //TODO
             //For data-provider tests
            if (testData.length > 0) {
                /*Check if testData is coming from spreadsheet(converted to comma separated values)
                or from a dataprovider class(no comma present)*/
               
                if(testData[0].toString().contains(",")) { //For data-providers that use spreadsheets
                    String[] entries = testData[0].toString().replace("{","").replace("}","").split(",");
                    //For each
                    if(entries.length>1) {
                        for(String entry:entries) {
                            map.put(entry.split("=")[0].trim(), entry.split("=")[1].trim());
                        }
                        TestID = map.get("TestID");
                    }        
                   
                    //Certain spreadsheets like DefaultSessionStimulationTestData does not contain TestID's
                    if(TestID == null) {
                        testName.set(method.getName());
                        ctx.setAttribute("testName", method.getName());
                    }else {
                        testName.set(method.getName() + "_" + TestID);
                        ctx.setAttribute("testName", method.getName() + "_" + TestID);
                    }
                }
                else if(!testData[0].toString().contains(",")) { //For data-providers that don't use spreadsheets
                   
                    TestID = testData[0].toString();    
                    testName.set(method.getName() + "_" + TestID);
                    ctx.setAttribute("testName", method.getName() + "_" + TestID);
                }            
                else { //Code used for dataproviders that have single entry and no TestID's, viz create_patient
                                       
                    testName.set(method.getName() + "_" + testData[0].toString());
                    ctx.setAttribute("testName", method.getName() + "_" + testData[0].toString());
                }                
             }
           
            else //For non-dataprovider tests
             {
                 TestID = method.getName();
                 testName.set(method.getName());
                 ctx.setAttribute("testName", method.getName());
             }
   
   
             log.info("Testname in BaseTestSection is: " + ctx.getAttribute("testName"));
             
            //Also check if app is already running. Helpful in conditions where app shutsdown in middle of any testcase though setup was completed successfully
            if (!setupComplete && driver!=null){
                log.info("Relaunching app if setup incomplete. This also helps with picking up the cp.configuration.json file if setup in beforeClass method...");
                try {
                    driver.launchApp();                    
                    beforeEachTestMethod();
                }catch(Exception e) {
                    e.printStackTrace();
                    throw new Exception();
                }
            }
            setupComplete=true;
        }catch(Exception e) {e.printStackTrace();}
    }

On Tuesday, April 12, 2022 at 6:37:22 PM UTC-7 Krishnan Mahadevan wrote:
>>>> Also not sure why, but I get my testName as checkBlobCount_org.testng.TestRunner@35835e65 

How exactly are you getting the testName? Can you show that part ? 

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"

⇜Krishnan Mahadevan⇝

unread,
Apr 13, 2022, 8:05:02 AM4/13/22
to testng-users
Please check if the below would help?

Java
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.testng.IReporter;
import org.testng.ISuite;
import org.testng.ISuiteResult;
import org.testng.ITestContext;
import org.testng.ITestNGMethod;
import org.testng.ITestResult;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
import org.testng.xml.XmlSuite;

@Listeners(SampleTestClass.MyReporter.class)
public class SampleTestClass {

  @BeforeMethod
  public void beforeMethod(ITestResult itr) {
    ITestNGMethod itm = itr.getMethod();
    String testCase;
    if (itm.isDataDriven()) {
      testCase = "TestCase:" + Arrays.toString(itr.getParameters());
    } else {
      //regular method
      if (itm.getInvocationCount() == 1) {
        testCase = "TestCase:" + itm.getQualifiedName();
      } else {
        testCase = "TestCase:" + itm.getQualifiedName() + "_" + itm.getCurrentInvocationCount();
      }
    }
    itr.setAttribute("testName", testCase);
  }

  @Test
  public void regularTestMethod() {
  }

  @Test(dataProvider = "dp")
  public void dataDrivenTest(int i) {
  }

  @DataProvider(name = "dp")
  public Object[][] getData() {
    return new Object[][]{
        {1},
        {2}
    };
  }

  @Test(invocationCount = 2)
  public void testWithInvocation() {
  }

  public static class MyReporter implements IReporter {

    @Override
    public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites,
        String outputDirectory) {
      suites.stream()
          .flatMap(each -> each.getResults().values().stream())
          .map(ISuiteResult::getTestContext)
          .flatMap(eachTestContext -> extractResults(eachTestContext).stream())
          .forEach(eachTestResult -> {
            String msg = String.format("TestMethod: %s(), TestName: %s",
                eachTestResult.getMethod().getQualifiedName(),
                eachTestResult.getAttribute("testName")
            );
            System.err.println(msg);
          });
    }

    private static Set<ITestResult> extractResults(ITestContext ctx) {
      Stream<ITestResult> stream1 = Stream.concat(
          ctx.getPassedTests().getAllResults().stream(),
          ctx.getFailedTests().getAllResults().stream()
      );
      return Stream.concat(stream1,
              ctx.getSkippedTests().getAllResults().stream())
          .collect(Collectors.toSet());
    }
  }
}

Output:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. =============================================== Default Suite Total tests run: 5, Passes: 5, Failures: 0, Skips: 0 =============================================== TestMethod: com.rationaleemotions.ic.SampleTestClass.testWithInvocation(), TestName: TestCase:com.rationaleemotions.ic.SampleTestClass.testWithInvocation_0 TestMethod: com.rationaleemotions.ic.SampleTestClass.testWithInvocation(), TestName: TestCase:com.rationaleemotions.ic.SampleTestClass.testWithInvocation_1 TestMethod: com.rationaleemotions.ic.SampleTestClass.dataDrivenTest(), TestName: TestCase:[2] TestMethod: com.rationaleemotions.ic.SampleTestClass.dataDrivenTest(), TestName: TestCase:[1] TestMethod: com.rationaleemotions.ic.SampleTestClass.regularTestMethod(), TestName: TestCase:com.rationaleemotions.ic.SampleTestClass.regularTestMethod Process finished with exit code 0

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"

Reply all
Reply to author
Forward
0 new messages