How to fail a test if the condition is false?

Hi all,

I’m facing one issue (maybe the solution is simple, but I don’t know it :slight_smile: ).
I have an “IF” statement to compare two integers, and want to fail the test if the condition is false. My code is as follow:

if ( integer x < integer y) {
do some actions (verify, click …)
} else {
I want the test to fail here … but how?

Thanks in advance!

import com.kms.katalon.core.util.KeywordUtil
KeywordUtil.markFailed("X is greater than Y")

Thank you Marek for your quick replay …
I just tested it and it works perfectly :star_struck: yahoooo!
Thank a lot for your help Marek …

without keyword, you can simply do:
assert false

1 Like

Hmm I don’t know how to do it :roll_eyes: … what could it be in the case above?

Well, if you are using the Katalon GUI, you can select the Assert Statement image


If you are in the scripting, you can use (using your example above)

if ( integer x < integer y) {
do some actions (verify, click …)
} else {
assert false

(At least, that’s how it looks where I’ve used it)

I just tested it Amanda and it works perfectly fine! I have two solutions now haha :star_struck:

Thank you so much guys for helping me …

the benefit of keyword is that you can pass also a message with failure reason to the test report.
the assert one is just a quick universal workaround, not dependant on the framework
(but you can do also a print/log before the assert)

aaand … the short way, without if/else:
assert x < y
(or use any java/groovy compare methods returning a boolean togheter with assert)

Thank you so much Ibus, you’re a genius :slight_smile:

You can even add a log in the assert !
assert false : "The fail log you want"


@anouar.chouikh now you have 3 solutions :smiley:

1 Like

If an assert fails, it shows the TC run status with ‘ERROR’. How to fail a test case if an assert fails ?

Caused by: java.lang.AssertionError: The expected value for orderStatus was not found in the response… Expression: (value.toString().equalsIgnoreCase(text) == true)
… 14 more

Test Cases/API-Tests/InvestigationFollowUpOrderTest
ERRORPreformatted text

@harshita.singh If I understand correctly, the concern you have is that “value” is null, so you can test if value is null (or not null) and do some other statement that will fail. As an example ( as a note you do not need to test if the value.toString() is true as the value.toString() statement is a boolean condition in itself):

if (value) {
assert value.toString().equalsIgnoreCase(text)
} else {
assert false