Forum Discussion

shyne's avatar
shyne
Occasional Contributor
2 years ago

Groovy - Get the name of the test case that called the "Run Test Case" for this test case

Get the name of the test case that called the "Run Test Case" for this test case in Groovy:

 

  • Test Suite: "ABC" has Test case "ABC123",
  • which has a "Run Test Case" step,
  • that runs "CommonTest1" test case (in Test Suite "Common").

I want the Groovy script in CommonTest1 to log/use the name of the Test case that called it. That is: "ABC123". Ideally without passing in "context.currentStep.name" as a parameter.

 

Help much appreciated.

  • shyne 

    Yes, got it.

     

    Here is the script needed:

     

    def showCallTrace = {
    	def step = context.'#CallingRunTestCaseStep#'
    	def myname = { it.name }
    	log.info "I am called by ${myname(step)} step of ${myname(step.parent)} test case of ${myname(step.parent.parent)} suite"
    }
    showCallTrace()

     

     

  • nmrao's avatar
    nmrao
    Champion Level 3
    It would be helpful if you share what you have already tried.
    By the way, why do you not want to pass the name if no other way? Is there any constraints?
    • shyne's avatar
      shyne
      Occasional Contributor

      Hopefully this image makes it clearer Rao. I'm looking for a command or way that would work like my made-up "getTheTestCaseThatCalledMe()" function. The reason I'd rather not pass the value through is, there are well over a hundred calls to different CommonTest1 type test cases. It is of course do-able but it'd be cleaner, safer quicker and easier if we didn't need to.

       

       

       

       

      The code shown in the image for "WhoCalledMe" Groovy script step:

      log.info testRunner.getTestCase().getName().toString()  
      /* equals "CommonTest1" */
      
      log.info context.expand( '${#TestCase#input_passedInPropertyTestCaseName}' )
      /* Equals the "ABC123" that I want, but requires that property value to be passed in */ 
      
      //log.info testRunner.getTestCase().getTheTestCaseThatCalledMe().getName().toString() 
      /* getTheTestCaseThatCalledMe() of course does not exist */

       

  • nmrao's avatar
    nmrao
    Champion Level 3

    shyne 

    Yes, got it.

     

    Here is the script needed:

     

    def showCallTrace = {
    	def step = context.'#CallingRunTestCaseStep#'
    	def myname = { it.name }
    	log.info "I am called by ${myname(step)} step of ${myname(step.parent)} test case of ${myname(step.parent.parent)} suite"
    }
    showCallTrace()