Testing Custom Keywords class using JUnit4 in Katalon Studio

I have published a project at

This post is just for your interest. No bug report.

Problem to solve

I wanted to develop a set of Custom Keywords in a Katalon Studio project. For the background story, see a post in the Katalon Forum: Taking entire page screenshot using AShot in Katalon Studio. My custom keywords were expected to be large and complexed, therefore bug-prone. I wanted to do unit-testing thoroughly using JUnit 4.

Solution

Unfortunately, Katalon Studio does not offer a toolset to do unit-testing for custom Keywords. Therefore I will invent a framework for myself.

How to inject the Junit4ks files into your own Katlaon project

  1. Here I assume you have a Katalon Studio project already created. Any project will do.

  2. Visit the Releases v1.6.3 page. Locate the link injectJunit4ks-build.gradle. Download the file into your project’s root directory. The file has the following content:

plugins {
    id "de.undercouch.download" version "5.7.0"
}
task injectJunit4ks {
    doLast {
        download.run {
            src "https://github.com/kazurayam/junit4ks/releases/download/1.6.3/junit4ks-distribution-1.6.3.zip"
            dest layout.buildDirectory.dir('dist-downloaded')
            overwrite true
        }
        copy {
            from zipTree(layout.buildDirectory.file("dist-downloaded/junit4ks-distribution-1.6.3.zip"))
            into layout.projectDirectory.dir('.')
        }
    }
}
  1. You need to rename the injectJunit4ks-build.gradle file to build.gradle.

  2. In a Terminal window, you want to do the following operation in the command line:

$ cd <yourProjecDir>
$ gradle injectJunit4ks

Obviously here I assume you have the Gradle build tool is installed and operational in your environment.

  1. The injectJunit4ks task will unzip the archive and copy the contained files into your own Katalon Studio project.

  2. Once the zip’s content files have been extracted and injected into your project, the build.gradle is no longer needed. You should remove the build.gradle file.

You don`t have Gradle on your machine? OK, then you can do the same manually. Download the from the junit4js-distribution-1.6.3.zip, unzip it, copy the folder tree into your Katalon project.

What’s in the zip file

Once you ran $ gradle injectJunit4ks task, in your own Katalon Studio project directory, you would find a new file created in the build directory:

$ pwd
$ <yourProjectDir>
$ tree build
build
β”œβ”€β”€ dist-downloaded
β”‚   └── junit4ks-distribution-1.6.3.zip

The zip file contains the following file tree:

$ tree .
.
β”œβ”€β”€ Include
β”‚   └── scripts
β”‚       └── groovy
β”‚           └── junittutorial
β”‚               β”œβ”€β”€ CalculatorFailingTest.groovy
β”‚               β”œβ”€β”€ CalculatorTest.groovy
β”‚               └── CalculatorWithIgnoreRestTest.groovy
β”œβ”€β”€ Keywords
β”‚   β”œβ”€β”€ com
β”‚   β”‚   └── kazurayam
β”‚   β”‚       └── junit4ks
β”‚   β”‚           β”œβ”€β”€ IgnoreRest.groovy
β”‚   β”‚           β”œβ”€β”€ IgnoreRestHelper.groovy
β”‚   β”‚           β”œβ”€β”€ IgnoreRestRule.groovy
β”‚   β”‚           β”œβ”€β”€ IgnoreRestSupportRunner.groovy
β”‚   β”‚           β”œβ”€β”€ JUnitCustomKeywords.groovy
β”‚   β”‚           β”œβ”€β”€ JUnitRunnerResult.groovy
β”‚   β”‚           β”œβ”€β”€ JUnitRunnerResultImpl.groovy
β”‚   β”‚           β”œβ”€β”€ LegacyXmlReportGeneratingListener.groovy
β”‚   β”‚           └── RunListener4ks.groovy
β”‚   └── junittutorial
β”‚       └── Calculator.groovy
β”œβ”€β”€ Scripts
β”‚   └── test
β”‚       └── junittutorial
β”‚           β”œβ”€β”€ CalculatorFailingTestRunner
β”‚           β”‚   └── Script1751710481301.groovy
β”‚           β”œβ”€β”€ CalculatorTestRunner
β”‚           β”‚   └── Script1547192368406.groovy
β”‚           └── CalculatorTestWithIgnoreRestRunner
β”‚               └── Script1552166240495.groovy
β”œβ”€β”€ Test Cases
β”‚   └── test
β”‚       └── junittutorial
β”‚           β”œβ”€β”€ CalculatorFailingTestRunner.tc
β”‚           β”œβ”€β”€ CalculatorTestRunner.tc
β”‚           └── CalculatorTestWithIgnoreRestRunner.tc
└── Test Suites
    └── test
        └── junittutorial
            β”œβ”€β”€ runAll.groovy
            └── runAll.ts
----

The *.groovy files under the Keywords directory is the implementation of the junit4ks library.

The files under the Test Cases, Scripts, Test Suite and Include directory are the sample code that utilizes the junit4ks.

How to run the demonstration

Close and reopen your Katalon Studio project in which the junit4ks files have been injected.

Open the Test Suites/test/junittutorial/runAll. Run it it by clicking the green button green button. The Test Suite would pass.

When it finished, you can see the test result output in the Log Viewer and Console in Katalon Studio GUI. When any test fails, a Java StackTrace will be printed into the Log Viewer.

You may expect a nicely formatted report file in HTML to be generated, but unfortunately my junit4ks does not do it. In fact, I don’t need it.

Source codes

Have a look at the following sample codes.

The Calculator.groovy is the target class to be unit-tested. The CalculatorTest.groovy carries out unit-tests for the custom class. You can run the test by running the CalcluratorTestRunner in Katalon Studio GUI by clicking the ordinary green button green button.

API doc

The groovydoc of junit4ks is here

2 Likes

Hi there, and thanks for posting in the Katalon community! :hugs:

To help you faster, please review our guide on Custom Keyword here: Introduction to custom keywords in Katalon Studio | Katalon Docs. Double-checking the steps and configurations might resolve the issue.

If the doc doesn’t help, feel free to provide more details, and a community member will assist you soon. Thanks for being a part of our community!

Best,
Elly Tran

Thank you, Mr. Kazu. You have never ceased to surprise us with your outstanding work.

On another note, have you tried out The TruePlatform, would love to hear feedback from you.

Wow loved your work. Kudos !!!

1 Like

great work mr @kazurayam

i have a few questions:

  1. have you considered packaging this as a proper Katalon plugin from the Katalon Store? that would simplify installation for users

  2. can these tests run via katalonc.exe in headless mode for pipeline integration?

1 Like

No. I have never considered packaging this as a Katalon plugin.

I don’t think it is worth. Usual Katalon users (non programmers) would never require junit for their work.

Also, I believe, my instruction β€œhow to inject the Junit4ks files into your own Katalon project” is simple enough for the experienced java programmers.


I just read the doc Create your first Katalon Studio plugin. I can understand it. Now I know I would be able to create a jar that contains com.kazurayam.junit4ks.JUnitCustomKeywords and other classes in the Keywords folder. But I am not satisfied with publishing only jar that contains the compiled .class binary files. Why? β€” I want to distribute the Groovy source codes as a sample use case such as Test Cases/test/junittutorial/CalculatorTestRunner. It seems that the Katalon plugin does not allow me to distribute any source codes. It does not let me inject the source files into the user’s own project. That is exactly what I realized by $ gradle injectJunit4ks task.
Katalon plugin is not enough for me. I would not go for it.

Perhaps yes, though I haven’t examined it.

The junit4ks project provides a set of files in the Keywords, Test Cases, Includes and Test Suites folders, which are just the ordinary katalon project’s constructs. Certainly, these codes would work in Katalon Runtime Engine.

However, I do not think that the junit4ks artifacts are useful in the pipleline of Continuous Integration/Continuous Delivery for UI verification. Why? β€” I intended junit4ks to be useful to carry out unit-testing for custom Keyword classes. Custom Keywords will be used in Test Case scripts for UI verification. Therefore the custom classes must be finished and thoroughly tested before you start CI/CD for automated UI verifications. Personally, I would never utilize the junit4ks artifacts in my CI/CD processes.

It would be a poor joke if you want to use junit for UI verifications. If you are serious, it means, you do not need Katalon Studio. This book would suffice.