Running JUnit4 in Katalon Studio

I have made a demo project and published it on GitHub:

Background

I am going to develop a custom Keyword in a Katalon Studio project. See a post in the Katalon forum for the background story. My custom keyword is expected to be fairly large, complicated, and therefore bug-prone. So I want to execute thorough unit-testing on my Groovy classes and methods with JUnit4 in Katalon Studio.

I was aware that the Katalon Studio’s distributed zip file contains plugins/org.junit_4.12.0.jar. I tried a test case of single line:

import org.junit.runner.JUnitCore

This worked! I mean, import statement succeeded, no error emitted. I realized that the JUnit classes are available in the Katalon Studio’s Java VM.

OK, all I need to know is as follows

  1. where to locate the application classes to be tested
  2. where to locate the test classes using JUnit API
  3. where to locate the test-runner scripts
  4. how to write a junit-test-runner that executes the test-runner scripts
  5. how to report the test result. I want to read the messages in the Log Viewer of Katalon Studio

Solution

I have done an experiment. I believe I have got a success.

  1. where to locate application classes? — in the Keywords directory

  2. where to locate test classes? — in the Include/scripts/groovy directory

  3. where to locate test-runners? — in the Test Cases/test directory

  1. how to write a test-runner? — I want to create a Test Case which performs the role of junit test-runner. It is as simple as:
import static com.kazurayam.junit4ks.JUnitCustomKeywords.runWithJUnitRunner
import junittutorial.CalculatorTest

runWithJUnitRunner(CalculatorTest.class)

I will develop a new Groovy class

  • com.kazurayam.junit4ks.JUnitCustomKeywords.runWithJUnitRunner
    which calls the JUnit API to execute the unit tests.
  1. how to report the test result

— The Test Case as “JUnit Test-Runner” will report log in the LogViewer and the Console as usual.

6 Likes

thanks a lot for your great work !!!

I have made a slight modification to the demo project.

Previously, the “messages” printed by System.out.println(message) from test cases under JUnit did not appear in the Katalon Studio’s log. It was very inconvenient.

In the revised sample project, messages printed into System.out by test cases under JUnit is redirected to Katalon Studio’s log.

1 Like

This is awesome. All Katalon Studio projects are essentially Eclipse projects so the trick is to init the project in Katalon Studio and then using Eclipse for running JUnit. I’ll add detailed steps soon.

I am glad to find people interested in idea of running JUnit in Katalon Studio.

What are you going to do?

1 Like

I hope the last section of this tutorial would help

I have updated my demo project tagged as 1.0:

I introduced a custom keyword class com.kazurayam.ksbackyard.junit.JUnitCustomKeywords. This class provides a keyword method runWithJUnitRunner(Class clazz). Your test cases in Katalon Studio can call this custom keyword which runs your JUnit-based tests. Your JUnit-based tests are backed by the Katalon Studio runtime environment, so that it can interact with external Web sites via WebDriver. You can see the test result in the Log Viewer and Console in Katalon Studio GUI.

I am happy with this approach now.

1 Like

I made a Feature Request to make the keyword builtin:

1 Like

I have an idea of making junit4ks a Katalon Studio Plugin in future. Principally for the sake of myself. I use it in almost all of my Katalon Studio projects. So I need an easy way to install the junit4ks in my projects.

So I renamed the package com.kazurayam.ksbackyard.junit to com.kazurayam.junit4ks for better catch.

See

4 Likes

I reget that junit4ks can not utilize the JUnit Report feature built-in Eclipse.

I have published the jar file of “junit4ks” at the Maven Central Repository. See

From there, you can download the jar into the Drivers folder of your Katalon Studio project.

1 Like