Forum Discussion

Sidharth24's avatar
Sidharth24
New Contributor
6 years ago

script assertion

i need to validate many assertions in same script assertion. But when any one of assert fails, runner stops there itself and control passed to next step. Below is my case

 

assert (1 ==1)
log.info "1"
assert (1 == 2)
log.info "2"
assert (1 ==3)

log.info "3"

 

When i execute the above, 2nd assertion fails and third assertion did not executed at all. Is there is any way to validate all assertions.

  • MartinSpamer's avatar
    MartinSpamer
    Frequent Contributor

    You could surround each assertion with a try catch.  I would wrap an assertion/try catch in simple function to minimise code duplication and aid clarity.  The following example will exercise all the test assertions, report the result, continue through all the verification and still fail the test overall.  This is known as a fail-safe test vs your fail-fast test, both have their place, it is a horses for courses thing.

     

    passed = true
    verify(1 == 1)
    verify(1 != 1)
    verify(2 == 2)
    verify(2 != 2)
    assert passed
    
    def verify(def condition) {
    	try {
    		assert condition
    	} catch (AssertionError assertion) {
    		log.info assertion
    		passed = false
    	}
    }