Add Unit Tests​Add ​Unit ​Tests

To avoid regression, we recommend adding unit tests for all your tasks.

There are two main ways to unit-test your tasks. In both cases, your tests must be annotated with @KestraTest to start the needed Kestra components properly.

Unit test a RunnableTask

This is the most common way to test a RunnableTask. You create your RunnableTask, test its run() method, and assert on its output or exception.

Example

This is done the same as any Java unit tests. Feel free to use any dependencies, test methods, and start docker containers as you need. Kestra tests are Micronaut tests so you can inject any bean in them.

Unit test with a full flow

If you want to add some unit tests with a full flow (which can be necessary in some rare cases, for example, for a FlowableTask), you will use the @ExecuteFlow annotation.

Example
  • @KestraTest(startRunner = true) will start Kestra with an in-memory backend.
  • @ExecuteFlow("flows/example.yaml") will start the flow from the src/test/resources/flows/example.yaml file and execute it.
  • The created execution is then available for test method parameter injection so that you can make assertions on it.

To make it work, you need to have an application.yml file with this minimum configuration:

yaml
kestra:
  repository:
    type: memory
  queue:
    type: memory
  storage:
    type: local
    local:
      basePath: /tmp/unittest

And these dependencies on your build.gradle:

groovy
testAnnotationProcessor group: "io.kestra", name: "processor", version: kestraVersion
testImplementation group: "io.kestra", name: "core", version: kestraVersion
testImplementation group: "io.kestra", name: "tests", version: kestraVersion
testImplementation group: "io.kestra", name: "repository-memory", version: kestraVersion
testImplementation group: "io.kestra", name: "runner-memory", version: kestraVersion
testImplementation group: "io.kestra", name: "storage-local", version: kestraVersion

This will enable the in memory runner and will run your flow without any other dependencies (e.g., kafka). If you created it from our plugin template, those are usually already included in your project.

Was this page helpful?