I need to execute some parts of code in a individual test cases even the test case failed

I need to execute some code even test case failed or passed.

Scenario

  1. Create a PROMOCODE from the admin side.
  2. On the front end we will apply this PROMOCODE.
  3. Test cases failed while applying promo
  4. Need to go admin site and delete the promo that was created earlier.

Step 4 I want to execute anyhow to delete the promo.

Please help.

Thank You,
Rahul Kumar

How many Test Case(s) do you have?
Do you do #1,#2,#3 and #4 in a single Test Case?
Or, do you have 4 Test Cases bundled in a singe Test Suite?

What do you mean “failed” ? What type of error have you ever encountered? Please let us know an example case error please.

Is that failure predicatable or not?

Suppose there are 3 test cases in a 1 test case.

TC-1 for creating promo from the backend.
TC-2 for applying that promo as an end user.
TC-3 to delete that promo that was created earlier.

in this case, if TC-2 is filled somehow with any error then how can I execute no 3 test cases either 2 failed or pass?

Thanks

???

Do you have 3 Test Cases in a Test Suite now?

A Test Suite is a dum bunch of Test Cases.
A Test Suite just executes TC1, TC2, then TC3. That’s all.
A Test Suite has no intelligence.
A Test Suite does not control the flow of composite Test Cases with if—then—else manner.

You should try “callTestCase()” keyword:

You will add a new Test Case “Controller”.
The Controller will call TC1, TC2, TC3.
The controller can know catch the result of TC2 exection and can control the flow.

@merahul220

If you are a capable programmer, it might be easier to write a single Test Case = Groovy script which comprises with inner groovy functions like:

def createPromoFromBackend(some params) {
    ...
}
def applyPromo(promo, userid) {
   ...
}
def deletePromo(promo) {
   ...
}

// now do your job
createPromoFromBacket(some params)
boolean result = applyPromo(some params)
if (result) {
    deletePromo(some params)
} else {
    // do something else
}

CallTestCase() keyworkd does effectively just the same as the inner function calls as above. No different.

2 Likes

@kazurayam thanks for your response.
I already used this way but just exploring is there any possibilities on the test cases level.

No, there isn’t any, I think.

Why do you seek for other ways?
What aren’t you satisfied with?

1 Like

suppose, somehow my 2nd case applyPromo got failed then the complete test case would be failed, and execution would be terminated from that point and the promo could not be deleted which is created earlier means the 3rd case deletePromo will not execute.

Within your called Test Case, applyPromo, you can use a “try / catch” block so if a part of it fails, the catch block gets control of your method (and could provide your method the return value). If your test case passes, then your catch statement does nothing. You can do different things based upon what specific exception was given. Below, I just used the generic Exception, however, you can maybe use, StepFailedException, or ElementInterceptionException, etc. and each catch statement can do something you want.

"applyPromo(some params)"
   try {
      blah
      blah
      boom boom bang
      blah
   } catch (Exception) {
     // result = true;
   }
   result = true;
   return result
}
2 Likes

whether We can able to write one or more cases in same test case file?

You can do it using the following approach:

Let I assume that your Test Suite has

  • TC-1
  • TC-2
  • TC-3

TC-2 will have a line:

TestCaseResults.assertTestCasePASSED("TC-1")

TC-3 will also have line

TestCaseResults.assertTestCasePASSED("TC-1")
  1. You want to run the Test Suite.
  2. Test Suite will invoke TC-1, TC-2 and TC-3 in this sequence regardles each test cases passed or failed. Even if TC-2 failed, the Test Suite will continue and will invoke TC-2.
  3. When the Test Suite invoke TC-2, the TC-2 will check if the TC-1 has passed using the custom keyword assertTestCasePASSED(tcname). If the TC-2 finds that the TC-1 has passed, then the TC-2 will run normally. If the TC-2 finds that the TC-1 has failed, then the TC-2 will fail and skip its processing body.
  4. Test Suite will invoke TC-3 regardless whether TC2 passed or failed. The TC-3 will check if the TC-1 has passed or not. The TC-3 will not check if the TC-2 has passed or not.

That’s what you want to do, isn’t it?

I do not understand what you mean by “one or more cases”. The term “case” is not defined in Katalon Studio.

There is a programming twist:

A Test Case is in effect an instance of Script class in Groovy language. A Groovy script can contain zero or more local Groovy methods . For example, the following Groovy script can be a valid Test Case with 3 methods:

def method1(String name) {
    println "Hello " + name + ", I am the first case"
}
def method2(String name) {
    println "How are you " + name + "?"
}
def method3(String name) {
    println "Goodbye, " + name
}

method1("Poo")
method2("Poo")
method3("Poo")

When you run this Test Case, you will see an output in the console:

Hello Poo, I am the first case
How are you Poo?
Goodbye Poo

You can let your single Test Case to contain as many methods as you like. You can code anything in each methods.