Run your Karate tests in IntelliJ IDEA without paying: a free and efficient solution

 

Karate is an open source testing tool renowned for its lightness, efficiency and user-friendliness. If you're already familiar with Karate, you'll know how powerful it can be for automating your API and UI tests.

However, running these tests can sometimes be tedious, especially when you need to run them regularly in your development environment.

In this article, I'm going to share with you a simple and free solution for automating Karate test execution in IntelliJ IDEA at 0 Euro.

Existing options

When it comes to running Karate tests, you generally have three options:

Java file with dedicated runner

You can create a Java file for each Karate test, containing a specific runner. This can be handy for running individual tests, but it quickly becomes tedious when you have a large number of tests.

link karate-template

karate

This may be convenient for running individual tests, but it quickly becomes tedious when you have a large number of tests.

UsersRunner.java

package examples.users;

import com.intuit.karate.junit5.Karate;

class UsersRunner {
    
    @Karate.Test
    Karate testUsers() {
        return Karate.run("users").relativeTo(getClass());
    }    

}

Simultaneous execution of all tests

Another option is to group all tests in a single Java file and run them simultaneously. This may be convenient for running all tests at once, but it can become cumbersome when you're trying to work on individual tests or debug specific problems.

ExamplesTest.java


package examples;

import com.intuit.karate.Results;
import com.intuit.karate.Runner;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;

class ExamplesTest {

    @Test
    void testParallel() {
        Results results = Runner.path("classpath:examples")
                //.outputCucumberJson(true)
                .parallel(5);
        assertEquals(0, results.getFailCount(), results.getErrorMessages());
    }

}

Use of paid plugins

Finally, you can use paid plugins that offer a user-friendly interface for running Karate tests. While these plugins can be handy, they do add an extra cost.
12 USD / Month for Karate PLUS
64 USD / Month for Karate PRO

More information on the karate page Price link

With the plugin you can run tests with a run button and run them by Scenario or Feature.

The V 0.0 solution

After trying various approaches, I finally found a simple solution and to automate the execution of Karate tests in IntelliJ IDEA. 🎡

Implementation

To implement this solution, you'll need two things: a generic Java class to run Karate tests and a custom runtime configuration in IntelliJ IDEA.

Creating the KarateRunner file

Create a file KarateRunner.java in your tree at the same level as your file "karate-config.js" file.

File contents


import com.intuit.karate.junit5.Karate;

public class KarateRunner {
    @Karate.Test
    Karate runTest() {
        String filePath = System.getProperty("karate.test");
        if (filePath == null || filePath.isEmpty()) {
            throw new IllegalArgumentException("Nom du fichier de test non spécifié. Veuillez spécifier le nom du fichier de test en tant que propriété système avec -Dkarate.test=nom_du_fichier.feature");
        }
        String fileName = filePath.replace('\\', '/');
        return Karate.run(fileName).relativeTo(KarateRunner.class);
    }
}

Custom runtime configuration in IntelliJ IDEA

  • Open IntelliJ IDEA and access the menu bar.
  • Select "Run" -> "Edit Configurations".
  • Create a new runtime configuration or modify an existing one.
  • Choose JUnit as template
Setting up our configuration

Set up your configuration as follows:

  1. Give your exp configuration a name: " KarateRunnerExc
  2. Use the "Dynamic Macros" feature to insert the name of the file currently selected in the IDE. Program arguments
    -ea -Dkarate.test=$FilePathRelativeToSourcepath$
  3. Look for our generic KarateRunner file.
  • Save configuration:

    • Once you have set the values in the configuration, click on "OK" to save the configuration.

Use

To use our new configuration, choose the "KarateRunnerExc" configuration, then go to your test file (toto.feature) and run this configuration

Results

Conclusion

With this solution in place, you can now automate the execution of your Karate tests in IntelliJ IDEA without having to spend money on paid plugins. This saves you

Fancy Tuxedo GIF - Fancy Tuxedo Winnie The Pooh - Discover & Share GIFs

save time and money while taking advantage of Karate's simplicity and user-friendliness in your software development process.

In the future, if I'm not too lazy, I'll work on a real plugin.

To find out more about Karate and start automating your tests, visit the official Karate website.

maudmcok Written by

Be the first to comment

Leave a comment

Your e-mail address will not be published. Required fields are marked with *.