8. The testRunner Variable

The testRunner Variable:

  • The testRunner variables are used to execute tests in a soapUI project.
For example:

The com.eviware.soapui.model.testsuite.TestCaseRunner interface, that extends com.eviware.soapui.model.testsuite.TestRunner, defines a set of methods to manipulate soapUI TestCases. 

Here we will look into the usage of testRunner inside a soapUI project.

The testRunner interfaces provide methods such as start, cancel, and fail to control the test execution. Follow these steps to see how testRunner can be used to control test execution flow:

1. Add the following statement in GroovyTestScript1 and run the getRoomDetails TestCase from the getRoomDetails TestCase editor:

testRunner.cancel("CANCELLED THE TEST")

2. We will see that the further executions of TestCase immediately stop when they reach the preceding statement and the CANCELLED THE TEST message is logged at the TestCase log. This is useful if you want to cancel the test run, based on evaluation of certain conditions.

Let's look at how we can invoke a different TestCase in a different TestSuite using testRunner.

Suppose in our sample HotelReservation project, the addReservation TestCase should fail if the corresponding room does not exist in the system (note that, in the ReservationService implementation, we have not added a validation to check the availability of rooms).

Thus, before invoking the addReservation TestCase, we may need to check the existence of the room which is going to be reserved. In this case, of course we can directly use the Run TestCase TestStep. However, let's try to use a GroovyScript TestStep as a child of addReservation TestCase to invoke getRoomDetails TestCase so that we can look in to the possibilities of using the testRunner object:

1. Select ReservationServiceTestSuite and add Groovy Script TestStep as an immediate child of addReservation TestCase. Name it findRoomScript.

2. Add the following script:

import com.eviware.soapui.model.testsuite.TestRunner.Status
//Get hold of the getRoomDetails TestCase which is at a different 
// TestSuite than the current Suite
def getRoomDetailsTestCase = testRunner.testCase.testSuite.project.testSuites["RoomManagementServiceTestSuite"].testCases["getRoomDetails TestCase"]
//Run the getRoomDetails TestCase synchronously
def testcaserunner = getRoomDetailsTestCase.run(null, false)
//Fail if getRoomDetails TestCase fail
assert testcaserunner.status == Status.FINISHED

Here, we first got a reference to getRoomDetails TestCase. Then, we invoked the run(stringToObjectMap properties, boolean async) method of the WsdlTestCase class (com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase) that implemented the TestCase interface (http://www.soapui.org/apidocs/com/eviware/soapui/impl/wsdl/testcase/WsdlTestCase.html). Finally, we used assert statement to check the status of the TestCase execution.

3. Give an existing room number as value in <typ:roomNumber> in getRoomDetails SOAP request. Also, specify the same room number in the addReservation SOAP request. Run ReservationTestSuite. Make sure to disable GroovyTestScript2 TestStep, which has been added previously to cancel the execution of getRoomDetails TestCase:

The testRunner variable

The TestCase log will be updated with the results, where you can find the findRoomScript is marked in green, denoting the success of execution of the getRoomDetails TestCase.

4. Now, submit the getRoomDetails TestStep of RoomManagementService TestSuite with a non-existing room number. We should add an assertion to denote that the getRoomDetails TestStep of the getRoomDetails TestCase fails if we submit the getRoomDetails SOAP request with a non-existing room. Thus, add the Not SOAP Fault assertion to getRoomDetails SOAP Request TestStep.

5. Run the addReservation TestCase of ReservationTestSuite again. You will get a test failure, as shown in the following screenshot:

The testRunner variable


No comments:

Post a Comment