Forum Discussion

mmoser18's avatar
mmoser18
Frequent Contributor
9 years ago
Solved

How to call some common Groovy code from multiple assertions?

I am really getting that hang of soapUI's scripting capabilities. They are totally groovy, indeed! :-)

 

I have meanwhile unified my test cases such, that they all execute the same code-snippet which loads a regex from a file and then asserts whether the received response matches that regex. The only thing that differs between all my test-cases is the name of the file from which to read the applicable regex. For each testcase there is one file containing the regexp that the response of that testcase has to match.

 

The next step in simplifying things would be that all assertions call the exactly same piece of code. I would then not have to copy-paste the same code into each Script Assertion's code field any more (or just some minimalistic of code icentical for all assertions, like

call common_assertion_code(<name-of-testcase>)

 

 

For this I still need the following pieces:

  1. Where should I best place such a piece of groovy code that is to be used by multiple test-suites?
  2. How can I call that common piece of code from the assertion's code-snippet?
  3. How do I access the TestCase's name from within the assertion's code-snippet?
  4. How can I pass than name from the calling assertion code to the called common code?

Regards,

M.

13 Replies

  • rupert_anderson's avatar
    rupert_anderson
    Valued Contributor

    Hello again!

     

    Groovy is one of the best parts of SoapUI I would say! :-)

     

    1. For this part I would consider writing yourself a custom Groovy library to contain any code that you need to be reused. In the pro version of SoapUI there is script library functionality for this, but its also quite easy in the Open Source version. Basically SoapUI can use extenernal java/Groovy libraries added to the /ext extensions folder - brief steps are:

    1a) Create a Groovy class to contain your code e.g. with a public method, in a folder structure matching its package declaration.

    1b) Compile the class

    1c) Create a jar file containing the packaged class.

     

    2. To make the jar file library available to SoapUI, place it in the<SoapUI Home> /java/app/bin/ext folder and restart SoapUI, you should see a message in the SoapUI log indicating the the jar file has been picked up. To use the function from the class in the Jar file, use a standard import statement to import the class e.g. in a Groovy TestStep or Script Assertion. Then instantiate the class and call the method on it you need.

     

    3. Using testRunner.testCase.name from a Groovy TestStep or using the approach from http://community.smartbear.com/t5/SoapUI-Open-Source/How-to-access-standard-i-e-non-custom-properties/m-p/104385#M18200 (assuming it was right for you in that post!)

     

    4. When calling the method of your library class you could pass the value of testRunner.testCase.name directly.

     

    (I can happily elaborate further on any of these if necessary, really needed an actual example to be more clear) 

     

    Does this make sense?

     

    Cheers,

    Rupert

    • nmrao's avatar
      nmrao
      Champion Level 3

      Completely with Rup.

       

      Indeed that is the best approach, but the fact is that it requires coding knowledge of either java or groovy where you will have the assertion logic. And I use in the same manner.

       

      One thing to let you know is that, if at all particular assertion is failed, any way the relevant test case is going to fail, so test case name may not be explicitly required to pass to the function / assert / method.

    • mmoser18's avatar
      mmoser18
      Frequent Contributor

      Hi - sorry for the very lazy reply. I finally came around to dive into that.

       

      My aim is to make my script assertion as short as possible as most of the code is identical and thus shared for most test-cases.

       

      My script assertion contains one line:

      new lhapi_tests.PatternMatch().match_pattern('lounges', false, true);

       

       

      The Groovy class I implemented reads:

      package lhapi_tests
      
      class PatternMatch 
      {
         public void match_pattern(def rulename, def debug, def log) { 
            ... // does some testing based on the rulename.
         }
      }

      The .jar containing that class is place in the ext-directory and is picked up during soapUI's startup:

       

      2016-03-05 23:43:52,452 INFO  [SoapUI] Adding [C:\Program Files\SmartBear\SoapUI-5.2.1\bin\ext\pattern_match.jar] to extensions classpath

      When I send a request the assertion fails with the error message

       

      -> No such property: context for class: lhapi_tests.PatternMatch

       

      What "context" is meant here and how do I define or pass that to my test-class?