Test case vs Keyword (non-programmer)

Firstly I would just like to comment that I am not a programmer beyond knowing some basic stuff for cobbling bits together in KSE.

I do have a question out of interest though. Using a basic function as an example such as generating a random string, what is the difference between having this as a keyword, or as code block within a test case?

Edit for clarification: I meant Keyword Vs Test case which is called

A keyword is preffered if you have to repeat the same code block (sequence) multiple times, so you write it only once and then reuse it.

this is also known as ‘lazy programming’ which actually is not a bad thing.

If the code you have to run is unique for a case, use it straight in testcase… altough if the codeblock is large, yet again you can encapsulate it in a keyword to provide more readability (again the laziness criteria)

well… this os on short… for the long version you will have to first get familiar with object oriented programing and you will figure it out what other advantages you may have

basically, a keyword is just a method of a certain class but with a handy annotation.

@kazurayam may have more insights (yeah, i saw you typing on this topic)

1 Like

@paul.french

Let me tell you by example.

I made 2 Groovy classes:

Keywords/my/CustomKeyword.groovy

package my

import com.kms.katalon.core.annotation.Keyword

public class CustomKeywords {
	private static Random random = new Random()

	@Keyword
	public static int nextRandomInt(int bound) {
		return random.nextInt(bound)
	}
}

Please note that this class uses @Keyword annotation to a method.

Another code example:

Keywords/my/CutomUtils.groovy

package my

public class CustomUtils {

	private static Random random = new Random()

	public static int nextRandomInt(int bound) {
		return random.nextInt(bound)
	}
}

Please note this class has no @Keyword annotation.

The my.CustomKeyword class implements a Keyword as it uses a @Keyword annotation. On the other hand, the my.CustomUtils class does NOT implements a Keyword as it uses no @Keyword annotation.

Then, I made a Test Case script:

Test Cases/TC1

import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

int v1 = CustomKeywords.'my.CustomKeywords.nextRandomInt'(100)
WebUI.comment("v1=" + v1)

import my.CustomUtils
int v2 = CustomUtils.nextRandomInt(100)
WebUI.comment("v2=" + v2)

When I ran the TC1, I got the following output in the console:

2022-10-22 20:47:46.208 INFO  k.k.c.m.CustomKeywordDelegatingMetaClass - my.CustomKeywords.nextRandomInt is PASSED
2022-10-22 20:47:46.307 INFO  c.k.k.c.keyword.builtin.CommentKeyword   - v1=60
2022-10-22 20:47:46.329 INFO  c.k.k.c.keyword.builtin.CommentKeyword   - v2=99

Both classes worked fine. As you can see, there is no runtime difference between them.


There is only one difference between these 2 classes.

You can open the test case TC1 in Manual mode to edit it. In the Manual mode, you are suppose to operate the GUI widgets to edit the test case. When you are to select a Custom Keyword, you would see the following:

In the dropdown list of available “Custom Keywords”, you only find my.CustomeKeywords. You won’t find my.CustomeUtils there.

The reason of this differences is simple: if you write a @Keyword annotation to a custom class, it will be picked up as a Keyword by the Manual mode GUI and will be displayed in the dropdown list. If you don’t write @Keyword annotation, then the class is not recognized by the Manual mode GUI, therefore would not appear in the list.

The difference between a custom Keyword and a plain custom class is — with or without @Keyword annotation. That’s all. The @Keyword annotation is refered by the Manual mode of Test Case Editor while authoring only. The @Keyword annotation is not significant at runtime.

Obviously, all of built-in Keywords in Katalon Studio is implicitly provided with @Keyword annotations; therefore they are Keywords.

If you use Script mode only, and if you never use Manual mode, then you would never need @Keyword annotations. So I personally rarely use @Keyword as I never use Manual mode.

Classes located under the Keywords folder does not cause verbose “Test Case Step execution logs” therefore they will run faster. On the other hand a code block in test cases will cause verbose “Test Case Step execution logs” therefore it runs slow.

If you have a code block which is used repeatedly (say 100 times, 1000 times, 10000 times), the speed difference will become obvious and unbearable. See my previous post which argues how the verbose “Step execution logs” slows down you test execution:

2 Likes

++ on this. yet again a reason to adopt the ‘laziness’… will save your brain for further obscure debugging when the jvm goes crazy

Hi, I realise now I wasn’t clear in my original question - I actually meant Keyword Vs Code block in a test case which is then called by other test cases - i.e. not repeating the code each time I want to use it, but have it once and then call that block any time I want to use it

Thanks for this - yeah definitely thinking of getting myself enrolled on some Java coding evening class.

It is possible for a test case to call whole of another test case by WebUI.callTestCase() keyword.

No. It is impossible for you to pick up a piece of code out of a test case and let other test case to reuse that piece.

If you want to do “pick up a piece of code out of a test case”, then you need to create a custom class under the Keywords folder and you want to move the piece into the class.

Or, you want to make the piece to be a single independent Test Case, and other test cases will call it by WebUI.callTestCase()

1 Like

Yep so the question really then is say I had a test case which consists only of the code block for generating a random string and then I call this from other test cases - what’s the difference between this and a keyword

E.g.

Test case 1
{
generate random string
}
Test case 2:

RandomString = Call test case 1

WebUI.commment(RandomString)

Please use “code formatting” syntax to make your post better readable.

If you are a Manual mode user and you want to reuse the piece of code in Manual mode (you wan to see your custom keyword in the dropdown menu), then you need to make it a class with @Keyword annotation and locate it in the Keywords folder.

If you use Script mode only, these are not very different. Your example is too small, too trivial to discuss the difference.

“Step execution logging” woud be a factor of differentiating them.

Plus, if your “piece of code” is long and complexed (say over 100 lines, 3 or 4 methods), and therefore you need to perform unit-test the “piece of code” using JUnit, then you would possibly prefer to make it a class.

3 Likes

Cheers - that’s basically answered the question then. Though someone mentioned above that if you use keywords it doesn’t write out the comments for every line like a test case does so there is an efficiency there in using Keywords it seems.

1 Like

you got it.
with your approach, using keywords to reuse some codeblock can make huge difference with regards to performance vs calling other testcase.
less pressure on the logger and many other aspects hidden under the java/groovy compilers.
if you don’t need verbosity for that ‘step’ a keyword is by far more performant.

1 Like

thanks @bionel and @kazurayam