Skip to content

Unit Testing

The schema of a template file is as follows:

tests

A list of one or more unit tests to execute.

Type: array

tests[].name

The name of the test, this should be unique and give a rough indication of what behavior is being tested.

Type: string

tests[].environment

An optional map of environment variables to set for the duration of the test.

Type: object

tests[].target_processors

A [JSON Pointer][json-pointer] that identifies the specific processors which should be executed by the test. The target can either be a single processor or an array of processors. Alternatively a resource label can be used to identify a processor.

It is also possible to target processors in a separate file by prefixing the target with a path relative to the test file followed by a # symbol.

Type: string

Default: "/pipeline/processors"

# Examples
target_processors: foo_processor
target_processors: /pipeline/processors/0
target_processors: target.yaml#/pipeline/processors
target_processors: target.yaml#/pipeline/processors

tests[].target_mapping

A file path relative to the test definition path of a Bloblang file to execute as an alternative to testing processors with the target_processors field. This allows you to define unit tests for Bloblang mappings directly.

Type: string

Default: ""

tests[].mocks

An optional map of processors to mock. Keys should contain either a label or a JSON pointer of a processor that should be mocked. Values should contain a processor definition, which will replace the mocked processor. Most of the time you’ll want to use a [mapping processor][processors.mapping] here, and use it to create a result that emulates the target processor.

Type: object

# Examples
mocks:
get_foobar_api:
mapping: root = content().string() + " this is some mock content"
mocks:
/pipeline/processors/1:
mapping: root = content().string() + " this is some mock content"

tests[].input_batch

Define a batch of messages to feed into your test, specify either an input_batch or a series of input_batches.

Type: array

tests[].input_batch[].content

The raw content of the input message.

Type: string

tests[].input_batch[].json_content

Sets the raw content of the message to a JSON document matching the structure of the value.

Type: unknown

# Examples
json_content:
bar:
- element1
- 10
foo: foo value

tests[].input_batch[].file_content

Sets the raw content of the message by reading a file. The path of the file should be relative to the path of the test file.

Type: string

# Examples
file_content: ./foo/bar.txt

tests[].input_batch[].metadata

A map of metadata key/values to add to the input message.

Type: object

tests[].input_batches

Define a series of batches of messages to feed into your test, specify either an input_batch or a series of input_batches.

Type: two-dimensional array

tests[].input_batches[][].content

The raw content of the input message.

Type: string

tests[].input_batches[][].json_content

Sets the raw content of the message to a JSON document matching the structure of the value.

Type: unknown

# Examples
json_content:
bar:
- element1
- 10
foo: foo value

tests[].input_batches[][].file_content

Sets the raw content of the message by reading a file. The path of the file should be relative to the path of the test file.

Type: string

# Examples
file_content: ./foo/bar.txt

tests[].input_batches[][].metadata

A map of metadata key/values to add to the input message.

Type: object

tests[].output_batches

List of output batches.

Type: two-dimensional array

tests[].output_batches[][].bloblang

Executes a Bloblang mapping on the output message, if the result is anything other than a boolean equalling true the test fails.

Type: string

# Examples
bloblang: this.age > 10 && @foo.length() > 0

tests[].output_batches[][].content_equals

Checks the full raw contents of a message against a value.

Type: string

tests[].output_batches[][].content_matches

Checks whether the full raw contents of a message matches a regular expression (re2).

Type: string

# Examples
content_matches: ^foo [a-z]+ bar$

tests[].output_batches[][].metadata_equals

Checks a map of metadata keys to values against the metadata stored in the message. If there is a value mismatch between a key of the condition versus the message metadata this condition will fail.

Type: object

# Examples
metadata_equals:
example_key: example metadata value

tests[].output_batches[][].file_equals

Checks that the contents of a message matches the contents of a file. The path of the file should be relative to the path of the test file.

Type: string

# Examples
file_equals: ./foo/bar.txt

tests[].output_batches[][].file_json_equals

Checks that both the message and the file contents are valid JSON documents, and that they are structurally equivalent. Will ignore formatting and ordering differences. The path of the file should be relative to the path of the test file.

Type: string

# Examples
file_json_equals: ./foo/bar.json

tests[].output_batches[][].json_equals

Checks that both the message and the condition are valid JSON documents, and that they are structurally equivalent. Will ignore formatting and ordering differences.

Type: unknown

# Examples
json_equals:
key: value

tests[].output_batches[][].json_contains

Checks that both the message and the condition are valid JSON documents, and that the message is a superset of the condition.

Type: unknown

# Examples
json_contains:
key: value

tests[].output_batches[][].file_json_contains

Checks that both the message and the file contents are valid JSON documents, and that the message is a superset of the condition. Will ignore formatting and ordering differences. The path of the file should be relative to the path of the test file.

Type: string

# Examples
file_json_contains: ./foo/bar.json