You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by "Deng, Lea" <Le...@ccc.govt.nz> on 2016/01/12 01:50:49 UTC

"continue" does not continue the while loop after an assertion fails

I'm using Soap UI and Groovy Script to go through a xml response with many Decision elements.
My Groovy Script refers to a DataSource 'Decision Exclusion List RMA/2010/33789' for a list of exlusion text to search for in the xml response.
I search for each DecisionCode read from the DataSource, then extract the xml lines for this DecisionCode element, then look for the exlusion text within them.
If the DecisionCode can not be found within the xml text, then I assert a failure, and continue the loop.
However, the test does not continue. It stops after the failed assertion.

Is that because "continue" does not work from within if { } ?
Thanks,
Lea

Please see my code below:

--------------------------------------------------

//(singleConsentString contains the xml text to search through)

def exlusionList = testRunner.testCase.testSteps['Decision Exclusion List RMA/2010/33789'] // refer to another DataSource test step to get a list of exlusion text


def counter = 0
while(exlusionList.next(testRunner,context)){
        counter ++
        log.info("counter for decision exlusion list: " + counter)
        decisionCode = exlusionList.getPropertyValue('PathwayDecisionCode')
        decisionDescription = exlusionList.getPropertyValue('PathwayDecisionDescription')

        // search for the DecisionCode in xml text
        int startIndexDecision = singleConsentString.indexOf('<a:DecisionCode>'+decisionCode+'</a:DecisionCode>')
        log.info('logging start index of Decision:  ' + startIndexDecision)

        // if DecisionCode is not found, assert a failure, and continue the loop;
        if (startIndexDecision < 0){
                log.info('Check Decision Code Failed because Pathway data does not contain Decision Code: '+decisionCode)
                assert startIndexDecision >= 0, "Failed because "+decisionCode+" is not found in Pathway data, thus index is less than 0"
                continue // this does not work
        }

        // if DecisionCode is found, then extract the xml lines for this DecisionCode, and check for DecisionDescription as well.
        else {
                String subStringDecision = singleConsentString.substring(startIndexDecision)
                int endIndexDecision = subStringDecision.indexOf('</a:DecisionEntity>')
                singleDecisionString = subStringDecision.substring(0,endIndexDecision)
                log.info('logging singe decision:  '+ singleDecisionString)

                assert singleDecisionString.contains(decisionCode)&& singleConsentString.contains(decisionDescription)
        }
}


**********************************************************************
This electronic email and any files transmitted with it are intended
solely for the use of the individual or entity to whom they are addressed.

The views expressed in this message are those of the individual sender
and may not necessarily reflect the views of the Christchurch City Council.

If you are not the correct recipient of this email please advise the
sender and delete.

Christchurch City Council
http://www.ccc.govt.nz
**********************************************************************

RE: "continue" does not continue the while loop after an assertion fails

Posted by "Deng, Lea" <Le...@ccc.govt.nz>.
Hi John and Edinson,
Thank you very much for the suggestion of using flag, and a list. They work well for the main Test Case
Many thanks,
lea

From: John Wagenleitner [mailto:john.wagenleitner@gmail.com]
Sent: Tuesday, 12 January 2016 3:04 p.m.
To: users@groovy.apache.org
Subject: Re: "continue" does not continue the while loop after an assertion fails


Could also put the text you were using in the asserts into a List instead and after the while loop assert list isEmpty().
On Jan 11, 2016 5:28 PM, "Edinson E. Padrón Urdaneta" <ed...@gmail.com>> wrote:
​​
​​
I don't really know what are your intentions, so I gonna try to guess and help you if I can:
If you are trying to test every element in `exlusionList`, you don't want to stop the execution until the last one has been tested, and you want the test to fail if at least one element failed, you can use a flag to help you with that. Set the flag if the assertion failed at least once, and if the flag was set (it's true, for example) you can throw an exception to make the test fail as a whole.
**********************************************************************
This electronic email and any files transmitted with it are intended
solely for the use of the individual or entity to whom they are addressed.

The views expressed in this message are those of the individual sender
and may not necessarily reflect the views of the Christchurch City Council.

If you are not the correct recipient of this email please advise the
sender and delete.

Christchurch City Council
http://www.ccc.govt.nz
**********************************************************************

Re: "continue" does not continue the while loop after an assertion fails

Posted by John Wagenleitner <jo...@gmail.com>.
Could also put the text you were using in the asserts into a List instead
and after the while loop assert list isEmpty().
On Jan 11, 2016 5:28 PM, "Edinson E. Padrón Urdaneta" <
edinson.padron.urdaneta@gmail.com> wrote:

> ​​
> ​​
> I don't really know what are your intentions, so I gonna try to guess and
> help you if I can:
>
> If you are trying to test every element in `exlusionList`, you don't want
> to stop the execution until the last one has been tested, and you want the
> test to fail if at least one element failed, you can use a flag to help you
> with that. Set the flag if the assertion failed at least once, and if the
> flag was set (it's true, for example) you can throw an exception to make
> the test fail as a whole.
>

Re: "continue" does not continue the while loop after an assertion fails

Posted by "Edinson E. Padrón Urdaneta" <ed...@gmail.com>.
​​
​​
I don't really know what are your intentions, so I gonna try to guess and
help you if I can:

If you are trying to test every element in `exlusionList`, you don't want
to stop the execution until the last one has been tested, and you want the
test to fail if at least one element failed, you can use a flag to help you
with that. Set the flag if the assertion failed at least once, and if the
flag was set (it's true, for example) you can throw an exception to make
the test fail as a whole.

RE: "continue" does not continue the while loop after an assertion fails

Posted by "Deng, Lea" <Le...@ccc.govt.nz>.
Hi John and Edinson,
You two are right. After the failed assertion, my code never reached "continue".
After I added try and catch for the assertion, continue worked. (please see code below)
However, the main Test Case appeared to be Passed after I catch this assertion error for this xml test step. I was going to show the assertion error and error logging in the Test Case.

if (startIndexDecision < 0){

log.info('Check Decision Code Failed because Pathway data does not contain Decision Code: '+decisionCode)

try {assert startIndexDecision >= 0, "Failed because "+decisionCode+" is not found in Pathway data, thus index is less than 0"}

catch (AssertionError e){
log.error("assertion error")
continue }
}





From: Edinson E. Padrón Urdaneta [mailto:edinson.padron.urdaneta@gmail.com]
Sent: Tuesday, 12 January 2016 2:10 p.m.
To: users@groovy.apache.org
Subject: Re: "continue" does not continue the while loop after an assertion fails

I could be very wrong but I think that the problem is a logical one. If an assertion doesn't hold (if it fails), an exception will be thrown and the rest of the code won't be executed.

On Mon, Jan 11, 2016 at 8:20 PM, Deng, Lea <Le...@ccc.govt.nz>> wrote:
I'm using Soap UI and Groovy Script to go through a xml response with many Decision elements.
My Groovy Script refers to a DataSource 'Decision Exclusion List RMA/2010/33789' for a list of exlusion text to search for in the xml response.
I search for each DecisionCode read from the DataSource, then extract the xml lines for this DecisionCode element, then look for the exlusion text within them.
If the DecisionCode can not be found within the xml text, then I assert a failure, and continue the loop.
However, the test does not continue. It stops after the failed assertion.

Is that because "continue" does not work from within if { } ?
Thanks,
Lea

Please see my code below:

--------------------------------------------------

//(singleConsentString contains the xml text to search through)

def exlusionList = testRunner.testCase.testSteps['Decision Exclusion List RMA/2010/33789'] // refer to another DataSource test step to get a list of exlusion text


def counter = 0
while(exlusionList.next(testRunner,context)){
        counter ++
        log.info<http://log.info>("counter for decision exlusion list: " + counter)
        decisionCode = exlusionList.getPropertyValue('PathwayDecisionCode')
        decisionDescription = exlusionList.getPropertyValue('PathwayDecisionDescription')

        // search for the DecisionCode in xml text
        int startIndexDecision = singleConsentString.indexOf('<a:DecisionCode>'+decisionCode+'</a:DecisionCode>')
        log.info<http://log.info>('logging start index of Decision:  ' + startIndexDecision)

        // if DecisionCode is not found, assert a failure, and continue the loop;
        if (startIndexDecision < 0){
                log.info<http://log.info>('Check Decision Code Failed because Pathway data does not contain Decision Code: '+decisionCode)
                assert startIndexDecision >= 0, "Failed because "+decisionCode+" is not found in Pathway data, thus index is less than 0"
                continue // this does not work
        }

        // if DecisionCode is found, then extract the xml lines for this DecisionCode, and check for DecisionDescription as well.
        else {
                String subStringDecision = singleConsentString.substring(startIndexDecision)
                int endIndexDecision = subStringDecision.indexOf('</a:DecisionEntity>')
                singleDecisionString = subStringDecision.substring(0,endIndexDecision)
                log.info<http://log.info>('logging singe decision:  '+ singleDecisionString)

                assert singleDecisionString.contains(decisionCode)&& singleConsentString.contains(decisionDescription)
        }
}


**********************************************************************
This electronic email and any files transmitted with it are intended
solely for the use of the individual or entity to whom they are addressed.

The views expressed in this message are those of the individual sender
and may not necessarily reflect the views of the Christchurch City Council.

If you are not the correct recipient of this email please advise the
sender and delete.

Christchurch City Council
http://www.ccc.govt.nz
**********************************************************************

**********************************************************************
This electronic email and any files transmitted with it are intended
solely for the use of the individual or entity to whom they are addressed.

The views expressed in this message are those of the individual sender
and may not necessarily reflect the views of the Christchurch City Council.

If you are not the correct recipient of this email please advise the
sender and delete.

Christchurch City Council
http://www.ccc.govt.nz
**********************************************************************

Re: "continue" does not continue the while loop after an assertion fails

Posted by "Edinson E. Padrón Urdaneta" <ed...@gmail.com>.
I could be very wrong but I think that the problem is a logical one. If an
assertion doesn't hold (if it fails), an exception will be thrown and the
rest of the code won't be executed.

On Mon, Jan 11, 2016 at 8:20 PM, Deng, Lea <Le...@ccc.govt.nz> wrote:

> I'm using Soap UI and Groovy Script to go through a xml response with many
> Decision elements.
> My Groovy Script refers to a DataSource 'Decision Exclusion List
> RMA/2010/33789' for a list of exlusion text to search for in the xml
> response.
> I search for each DecisionCode read from the DataSource, then extract the
> xml lines for this DecisionCode element, then look for the exlusion text
> within them.
> If the DecisionCode can not be found within the xml text, then I assert a
> failure, and continue the loop.
> However, the test does not continue. It stops after the failed assertion.
>
> Is that because "continue" does not work from within if { } ?
> Thanks,
> Lea
>
> Please see my code below:
>
> --------------------------------------------------
>
> //(singleConsentString contains the xml text to search through)
>
> def exlusionList = testRunner.testCase.testSteps['Decision Exclusion List
> RMA/2010/33789'] // refer to another DataSource test step to get a list of
> exlusion text
>
>
> def counter = 0
> while(exlusionList.next(testRunner,context)){
>         counter ++
>         log.info("counter for decision exlusion list: " + counter)
>         decisionCode = exlusionList.getPropertyValue('PathwayDecisionCode')
>         decisionDescription =
> exlusionList.getPropertyValue('PathwayDecisionDescription')
>
>         // search for the DecisionCode in xml text
>         int startIndexDecision =
> singleConsentString.indexOf('<a:DecisionCode>'+decisionCode+'</a:DecisionCode>')
>         log.info('logging start index of Decision:  ' +
> startIndexDecision)
>
>         // if DecisionCode is not found, assert a failure, and continue
> the loop;
>         if (startIndexDecision < 0){
>                 log.info('Check Decision Code Failed because Pathway data
> does not contain Decision Code: '+decisionCode)
>                 assert startIndexDecision >= 0, "Failed because
> "+decisionCode+" is not found in Pathway data, thus index is less than 0"
>                 continue // this does not work
>         }
>
>         // if DecisionCode is found, then extract the xml lines for this
> DecisionCode, and check for DecisionDescription as well.
>         else {
>                 String subStringDecision =
> singleConsentString.substring(startIndexDecision)
>                 int endIndexDecision =
> subStringDecision.indexOf('</a:DecisionEntity>')
>                 singleDecisionString =
> subStringDecision.substring(0,endIndexDecision)
>                 log.info('logging singe decision:  '+
> singleDecisionString)
>
>                 assert singleDecisionString.contains(decisionCode)&&
> singleConsentString.contains(decisionDescription)
>         }
> }
>
>
> **********************************************************************
> This electronic email and any files transmitted with it are intended
> solely for the use of the individual or entity to whom they are addressed.
>
> The views expressed in this message are those of the individual sender
> and may not necessarily reflect the views of the Christchurch City Council.
>
> If you are not the correct recipient of this email please advise the
> sender and delete.
>
> Christchurch City Council
> http://www.ccc.govt.nz
> **********************************************************************
>

Re: "continue" does not continue the while loop after an assertion fails

Posted by John Wagenleitner <jo...@gmail.com>.
The code never reaches the continue because the assert will throw an
AssertionError since the expression in the if statement proves it will
fail.  If you remove the assert it should work.  Also may want to remove
the assert in your else branch, unless it is meant to abort the program if
it's reached and fails.  In those cases log.error may be a good choice.

On Mon, Jan 11, 2016 at 4:50 PM, Deng, Lea <Le...@ccc.govt.nz> wrote:

> I'm using Soap UI and Groovy Script to go through a xml response with many
> Decision elements.
> My Groovy Script refers to a DataSource 'Decision Exclusion List
> RMA/2010/33789' for a list of exlusion text to search for in the xml
> response.
> I search for each DecisionCode read from the DataSource, then extract the
> xml lines for this DecisionCode element, then look for the exlusion text
> within them.
> If the DecisionCode can not be found within the xml text, then I assert a
> failure, and continue the loop.
> However, the test does not continue. It stops after the failed assertion.
>
> Is that because "continue" does not work from within if { } ?
> Thanks,
> Lea
>
> Please see my code below:
>
> --------------------------------------------------
>
> //(singleConsentString contains the xml text to search through)
>
> def exlusionList = testRunner.testCase.testSteps['Decision Exclusion List
> RMA/2010/33789'] // refer to another DataSource test step to get a list of
> exlusion text
>
>
> def counter = 0
> while(exlusionList.next(testRunner,context)){
>         counter ++
>         log.info("counter for decision exlusion list: " + counter)
>         decisionCode = exlusionList.getPropertyValue('PathwayDecisionCode')
>         decisionDescription =
> exlusionList.getPropertyValue('PathwayDecisionDescription')
>
>         // search for the DecisionCode in xml text
>         int startIndexDecision =
> singleConsentString.indexOf('<a:DecisionCode>'+decisionCode+'</a:DecisionCode>')
>         log.info('logging start index of Decision:  ' +
> startIndexDecision)
>
>         // if DecisionCode is not found, assert a failure, and continue
> the loop;
>         if (startIndexDecision < 0){
>                 log.info('Check Decision Code Failed because Pathway data
> does not contain Decision Code: '+decisionCode)
>                 assert startIndexDecision >= 0, "Failed because
> "+decisionCode+" is not found in Pathway data, thus index is less than 0"
>                 continue // this does not work
>         }
>
>         // if DecisionCode is found, then extract the xml lines for this
> DecisionCode, and check for DecisionDescription as well.
>         else {
>                 String subStringDecision =
> singleConsentString.substring(startIndexDecision)
>                 int endIndexDecision =
> subStringDecision.indexOf('</a:DecisionEntity>')
>                 singleDecisionString =
> subStringDecision.substring(0,endIndexDecision)
>                 log.info('logging singe decision:  '+
> singleDecisionString)
>
>                 assert singleDecisionString.contains(decisionCode)&&
> singleConsentString.contains(decisionDescription)
>         }
> }
>
>
> **********************************************************************
> This electronic email and any files transmitted with it are intended
> solely for the use of the individual or entity to whom they are addressed.
>
> The views expressed in this message are those of the individual sender
> and may not necessarily reflect the views of the Christchurch City Council.
>
> If you are not the correct recipient of this email please advise the
> sender and delete.
>
> Christchurch City Council
> http://www.ccc.govt.nz
> **********************************************************************
>