Forum Discussion

Emil91's avatar
Emil91
Contributor
8 years ago

Is it possible to randomize test steps in test cases using soapUI NG pro?

Hey guys,

 

I need to randomize test steps in my tests, as we all know there are plenty of sequences and combinations

and randomizing such steps could help to catch more unwanted problems/bugs, I wonder if it's possible to

write such script in groovy or in js? Thanks in advance for any kind of help, cheers.

 

Best regards

Emil

  •  

    This is something easily done in groovy. Here's a very simple mocked up script:

     

     

    def proj = testRunner.testCase.testSuite.project;
    
    def TS = proj.getTestSuiteList();
    
    if (proj.getTestSuiteCount() > 1)
    {
    	Collections.shuffle(TS);
    }
    
    for (int i = 0; i < proj.getTestSuiteCount(); i++)
    {
    	def testSuite = TS[i];
    
    	def TC = testSuite.getTestCaseList();
    
    	if (testSuite.getTestCaseCount() > 1)
    	{
    		Collections.shuffle(TC);
    	}
    	for (int j = 0; j < testSuite.getTestCaseCount(); j++)
    	{
    		def testCase = TC[j];
    
    		def TSteps = testCase.getTestStepList();
    
    		if (testCase.getTestStepCount() > 1)
    		{
    			Collections.shuffle(TSteps);
    		}
    		
    		for (int k = 0; k < testCase.getTestStepCount(); k++)
    		{
    			def properties = new com.eviware.soapui.support.types.StringToObjectMap()
    			def async = false
    			//--run object string against TC
    			log.info(TSteps[k].toString());
    			def testStep = TSteps[k];
    			def label = testStep.getLabel();
    			log.info(label);
    			// tesStep.run (properties, async)
    		}
    	}
    }
    
    

     

  • Try replacing

     

    testStep.run (properties, async)

     

    with 

     

    testRunner.runTestStepByName(label)
  • You really don't need groovy skills at this point. You have the script. You know at any given point what the label of the test running is from this line:

     

     

    		def label = testStep.getLabel();

     

    So, if you know the test whose label is "Test X" and that means you need to run transfer Y, you could set a new test step to the property transfer and run that.

     

    if (label == "Test X")
    {
    	def propTransferTestStep = context.testCase.testStep["PropertyTransferStepName"];
    	propTransferTesttStep.run(testRunner, context);	
    }

     

     

     

  • groovyguy's avatar
    groovyguy
    Community Hero

     

    This is something easily done in groovy. Here's a very simple mocked up script:

     

     

    def proj = testRunner.testCase.testSuite.project;
    
    def TS = proj.getTestSuiteList();
    
    if (proj.getTestSuiteCount() > 1)
    {
    	Collections.shuffle(TS);
    }
    
    for (int i = 0; i < proj.getTestSuiteCount(); i++)
    {
    	def testSuite = TS[i];
    
    	def TC = testSuite.getTestCaseList();
    
    	if (testSuite.getTestCaseCount() > 1)
    	{
    		Collections.shuffle(TC);
    	}
    	for (int j = 0; j < testSuite.getTestCaseCount(); j++)
    	{
    		def testCase = TC[j];
    
    		def TSteps = testCase.getTestStepList();
    
    		if (testCase.getTestStepCount() > 1)
    		{
    			Collections.shuffle(TSteps);
    		}
    		
    		for (int k = 0; k < testCase.getTestStepCount(); k++)
    		{
    			def properties = new com.eviware.soapui.support.types.StringToObjectMap()
    			def async = false
    			//--run object string against TC
    			log.info(TSteps[k].toString());
    			def testStep = TSteps[k];
    			def label = testStep.getLabel();
    			log.info(label);
    			// tesStep.run (properties, async)
    		}
    	}
    }
    
    

     

    • Emil91's avatar
      Emil91
      Contributor

      hey I see this script of yours is able to randomize test steps and every time i run script, I see different sequence of test steps in groovy script.

       

      Well I'm quite newbie in soapUI and honestly my groovy skills suck a little bit, i don't understand this line :

       

      // tesStep.run (properties, async)

      this part of code is commented, does it mean that i need to use it as well? if yes how exactly? 

      do i need to switch tesStep.run to (myTestStep.run)? and what about part with (properties, async)?

       

      And is it possible to use such script + use automatic test case looping?

       

      Thank you in advance Sir/Madam for your time and help.

       

      Best regards

      Emil

       

      • groovyguy's avatar
        groovyguy
        Community Hero

        If you put this in a groovy test step anywhere in a project it'll randomize the collection of test suites. For each test suite, it'll randomize the collection of test cases. And for each test case it'll randomize the collection of test steps.

         

        If you uncomment the line you mentioned, it should run the test step. I didn't have something to run it against right now, but you should only need to uncomment that line.