Skip to content

Assertions reference

Adrien Quillet edited this page Mar 10, 2023 · 9 revisions

STH provides a lot of build-int assertions. Here is the exhaustive list of assertions, with explainations and examples.

Synchronous assertions

For synchronous assertion explanation, please refer to this page.

Assertion shortcuts

assert_that

This utility contains basic assertions that you will need every time you write a test. By composing those assertions, you can express any kind of verification.

is_true

Is an assertion to verify if the given value is of type bool and is strictly true

assert_that(true).is_true() # OK

assert_that(false).is_true() # KO
assert_that(null).is_true() # KO
assert_that(Vector2()).is_true() # KO

is_false

Is an assertion to verify if the given value is of type bool and is strictly false

assert_that(false).is_false() # OK

assert_that(true).is_false() # KO
assert_that(null).is_false() # KO
assert_that("").is_false() # KO

is_null

Is an assertion to verify if the given value is strictly null

assert_that(null).is_null() # OK

assert_that(Vector3()).is_null() # KO
assert_that("").is_null() # KO
assert_that(false).is_null() # KO
assert_that(0).is_null() # KO

is_not_null

Is an assertion to verify if the given value is not null

assert_that(0).is_not_null() # OK
assert_that("").is_not_null() # OK
assert_that([]).is_not_null() # OK

assert_that(null).is_not_null() # KO

is_equal_to

Is an assertion to verify if the given value is equal to another value. Equality is checked using == operator.

assert_that(0).is_equal_to(0) # OK
assert_that(null).is_equal_to(null) # OK
assert_that(Vector2(5, 8)).is_equal_to(Vector2(5, 8)) # OK

assert_that(null).is_equal_to(Vector2(5, 8)) # KO
assert_that("").is_equal_to("a") # KO
assert_that(5).is_equal_to([]) # KO

is_not_equal_to

Is an assertion to verify if the given value is not equal to another value. Equality is checked using == operator.

assert_that(0).is_not_equal_to(1) # OK
assert_that(null).is_not_equal_to("") # OK
assert_that(Vector2(5, 8)).is_not_equal_to(Vector2(1, 6)) # OK

assert_that(null).is_not_equal_to(null) # KO
assert_that("").is_not_equal_to("") # KO
assert_that([]).is_not_equal_to([]) # KO

is_same_has

Is an assertion to verify if the given value is:

  • for value types, the same value as another one,
  • for reference types, the same reference as another one.
var arrayA:Array = []
var arrayB:Array = []

assert_that(0).is_same_has(0) # OK
assert_that(Vector2(0, 0)).is_same_has(Vector2(0, 0)) # OK
assert_that(arrayA).is_same_has(arrayA) # OK

assert_that(5).is_same_has(6) # KO
assert_that(arrayA).is_same_has(arrayB) # KO

is_not_same_has

Is an assertion to verify if the given value is:

  • for value types, not the same value as another one,
  • for reference types, not the same reference as another one.
var arrayA:Array = []
var arrayB:Array = []

assert_that(0).is_not_same_has(1) # OK
assert_that(Vector2(0, 0)).is_not_same_has(Vector2(5, 8)) # OK
assert_that(arrayA).is_not_same_has(arrayB) # OK

assert_that(5).is_not_same_has(5) # KO
assert_that(arrayA).is_not_same_has(arrayA) # KO

assert_that_array

This utility contains assertions that are specialized for verifying Array values.

is_null

Is an assertion to verify if the given value is strictly null

assert_that_array(null).is_null() # OK

assert_that_array(Array()).is_null() # KO
assert_that_array([1]).is_null() # KO

is_not_null

Is an assertion to verify if the given value is not null

assert_that_array([]).is_not_null() # OK
assert_that_array([null]).is_not_null() # OK

assert_that_array(null).is_not_null() # KO

is_equal_to

Is an assertion to verify if the given value is equal to another value. Equality is checked using == array operator.

assert_that_array([]).is_equal_to(Array()) # OK
assert_that_array([1]).is_equal_to([1]) # OK

assert_that_array(null).is_equal_to([]) # KO
assert_that_array([1]).is_equal_to([2]) # KO

is_not_equal_to

Is an assertion to verify if the given value is not equal to another value. Equality is checked using == array operator.

assert_that_array(null).is_not_equal_to([]) # OK
assert_that_array([1,2,3]).is_not_equal_to([3,2,1]) # OK

assert_that_array([]).is_not_equal_to([]) # KO
assert_that_array(["a"]).is_not_equal_to(["a"]) # KO

is_empty

Is an assertion to verify if the given array is empty. Note that null values will result in failed assertion.

assert_that_array(Array()).is_empty() # OK
assert_that_array([]).is_empty() # OK

assert_that_array(null).is_empty() # KO
assert_that_array([1]).is_empty() # KO

is_not_empty

Is an assertion to verify if the given array is not empty. Note that null values will result in failed assertion.

assert_that_array([1,2,3]).is_not_empty() # OK

assert_that_array(null).is_not_empty() # KO
assert_that_array([]).is_not_empty() # KO

has_size

Is an assertion to verify if the given array has the expected size. Note that null values will result in failed assertion, independent of the expected value.

assert_that_array([]).has_size(0) # OK
assert_that_array([1,2,3]).has_size(3) # OK

assert_that_array(null).has_size(0) # KO
assert_that_array([]).has_size(1) # KO

contains

Is an assertion to verify if the given array contains the expected value. It complies to has method contract from arrays. Note that null values will result in failed assertion.

assert_that_array([0]).contains(0) # OK
assert_that_array([1,2,3]).contains(2) # OK

assert_that_array(null).contains(null) # KO
assert_that_array([]).contains(1) # KO

contains_in_exact_order

Is an assertion to verify if the given array contains the expected values, in exact order, anywhere in the array. Note that null values will result in failed assertion.

assert_that_array([0,1,2,3]).contains_in_exact_order([2,3]) # OK
assert_that_array([1,2,3]).contains_in_exact_order([1]) # OK

assert_that_array(null).contains_in_exact_order([]) # KO
assert_that_array([1,2,3,4,5]).contains_in_exact_order([2,4,5]) # KO

contains_in_any_order

Is an assertion to verify if the given array contains the expected values, in any order. Note that null values will result in failed assertion.

assert_that_array([0,1,2,3]).contains_in_any_order([2,3]) # OK
assert_that_array([0,1,2,3]).contains_in_any_order([2,3]) # OK
assert_that_array([1,2,3]).contains_in_any_order([1]) # OK

assert_that_array(null).contains_in_any_order([]) # KO
assert_that_array([1,2,3,4,5]).contains_in_any_order([1,6]) # KO

all_match

Is an assertion to verify that each element in the given array can be positively test by the given predicate. Note that null values will result in failed assertion.

Given predicate is a Callable that take an array element as parameter, and returns true or false.

assert_that_array([0,1,2,3]).all_match(func(e): return e < 10) # OK
assert_that_array([2,3,4]).all_match(is_even) # OK, if a function is_even exists

assert_that_array(null).all_match(func(e) : return true) # KO
assert_that_array([1,2,3,4,5]).all_match(is_even) # KO

any_match

Is an assertion to verify that at least one element in the given array can be positively test by the given predicate. Note that null values will result in failed assertion.

Given predicate is a Callable that take an array element as parameter, and returns true or false.

assert_that_array([0,1,2,3]).any_match(func(e): return e < 10) # OK
assert_that_array([1,2,3,5]).any_match(is_even) # OK, if a function is_even exists

assert_that_array(null).any_match(func(e) : return true) # KO
assert_that_array([1,2,3,4,5]).any_match(func(e) : return e > 10) # KO

none_match

Is an assertion to verify that all elements in the given array can be negatively test by the given predicate. Note that null values will result in failed assertion.

Given predicate is a Callable that take an array element as parameter, and returns true or false.

assert_that_array([0,1,2,3]).none_match(func(e): return e > 10) # OK
assert_that_array([1,3,5]).none_match(is_even) # OK, if a function is_even exists

assert_that_array(null).none_match(func(e) : return false) # KO
assert_that_array([1,10,11]).none_match(func(e) : return e < 10) # KO

assert_that_color

This utility contains assertions that are specialized for verifying Color values.

is_null

Is an assertion to verify if the given value is strictly null

assert_that_color(null).is_null() # OK

assert_that_color(Color.RED).is_null() # KO

is_not_null

Is an assertion to verify if the given value is not null

assert_that_color(Color.BLACK).is_not_null() # OK

assert_that_color(null).is_not_null() # KO

is_equal_to

Is an assertion to verify if the given value is equal to another value. Equality is checked using == operator.

assert_that_color(Color.RED).is_equal_to(Color.RED) # OK

assert_that_array(Color.RED).is_equal_to(Color.GREEN) # KO

is_not_equal_to

Is an assertion to verify if the given value is not equal to another value. Equality is checked using == operator.

assert_that_color(Color.RED).is_not_equal_to(Color.BLUE) # OK

assert_that_color(Color.RED).is_not_equal_to(Color.RED) # KO

has_red

Is an assertion to verify if the given color has approximately the given red value, between 0 and 1. Equality is checked using is_equal_approx function.

assert_that_color(Color.RED).has_red(0.99999) # OK

assert_that_color(Color.RED).has_red(0.88) # KO

has_red_int

Is an assertion to verify if the given color has exactly the given red value, between 0 and 255.

assert_that_color(Color.RED).has_red_int(255) # OK

assert_that_color(Color.RED).has_red_int(254) # KO

has_green

Is an assertion to verify if the given color has approximately the given green value, between 0 and 1. Equality is checked using is_equal_approx function.

assert_that_color(Color.GREEN).has_green(0.99999) # OK

assert_that_color(Color.GREEN).has_green(0.88) # KO

has_green_int

Is an assertion to verify if the given color has exactly the given green value, between 0 and 255.

assert_that_color(Color.GREEN).has_green_int(255) # OK

assert_that_color(Color.GREEN).has_green_int(254) # KO

has_blue

Is an assertion to verify if the given color has approximately the given blue value, between 0 and 1. Equality is checked using is_equal_approx function.

assert_that_color(Color.BLUE).has_blue(0.99999) # OK

assert_that_color(Color.BLUE).has_blue(0.88) # KO

has_blue_int

Is an assertion to verify if the given color has exactly the given blue value, between 0 and 255.

assert_that_color(Color.BLUE).has_blue_int(255) # OK

assert_that_color(Color.BLUE).has_blue_int(254) # KO

has_alpha

Is an assertion to verify if the given color has approximately the given blue value, between 0 and 1. Equality is checked using is_equal_approx function.

assert_that_color(Color.BLUE).has_alpha(0.99999) # OK

assert_that_color(Color.BLUE).has_alpha(0.88) # KO

has_alpha_int

Is an assertion to verify if the given color has exactly the given blue value, between 0 and 255.

assert_that_color(Color.BLUE).has_alpha_int(255) # OK

assert_that_color(Color.BLUE).has_alpha_int(254) # KO

has_hue

Is an assertion to verify if the given color has approximately the given hue value, between 0 and 1.

assert_that_color(Color.BLUE).has_hue(0.66666) # OK

assert_that_color(Color.BLUE).has_hue(0.2) # KO

has_saturation

Is an assertion to verify if the given color has approximately the given saturation value, between 0 and 1.

assert_that_color(Color.BLUE).has_saturation(1) # OK

assert_that_color(Color.BLUE).has_saturation(0.4) # KO

has_brightness

Is an assertion to verify if the given color has approximately the given brightness value, between 0 and 1.

assert_that_color(Color.BLUE).has_brightness(1) # OK

assert_that_color(Color.BLUE).has_brightness(0.7) # KO

is_fully_opaque

Is an assertion to verify if the given color is not transparent.

assert_that_color(Color.BLUE).is_fully_opaque() # OK

assert_that_color(Color(1,1,1,0.5)).is_fully_opaque() # KO

is_fully_transparent

Is an assertion to verify if the given color is fully transparent.

assert_that_color(Color(1,1,1,0)).is_fully_transparent() # OK

assert_that_color(Color(1,1,1,0.5)).is_fully_transparent() # KO
assert_that_color(Color.BLUE).is_fully_transparent() # KO

assert_that_dictionary

This utility contains assertions that are specialized for verifying Dictionary values.

assert_that_int

This utility contains assertions that are specialized for verifying int values.

is_zero

Is an assertion to verify the given value is exactly 0

assert_that(0).is_zero() # OK

assert_that(5).is_zero() # KO
assert_that(-5).is_zero() # KO

is_not_zero

Is an assertion to verify the given value is not 0

assert_that(1).is_not_zero() # OK
assert_that(-9).is_not_zero() # OK

assert_that(0).is_not_zero() # KO

is_strictly_negative

Is an assertion to verify the given value is strictly lower than 0

assert_that(-1).is_strictly_negative() # OK
assert_that(-9).is_strictly_negative() # OK

assert_that(0).is_strictly_negative() # KO
assert_that(6).is_strictly_negative() # KO

is_negative_or_zero

Is an assertion to verify the given value is lower than or equals to 0

assert_that(-1).is_negative_or_zero() # OK
assert_that(0).is_negative_or_zero() # OK

assert_that(1).is_negative_or_zero() # KO
assert_that(6).is_negative_or_zero() # KO

is_strictly_positive

Is an assertion to verify the given value is strictly greater than 0

assert_that(1).is_strictly_positive() # OK
assert_that(7).is_strictly_positive() # OK

assert_that(0).is_strictly_positive() # KO
assert_that(-42).is_strictly_positive() # KO

is_positive_or_zero

Is an assertion to verify the given value is greater than or equals to 0

assert_that(1).is_positive_or_zero() # OK
assert_that(0).is_positive_or_zero() # OK

assert_that(-7).is_positive_or_zero() # KO
assert_that(-53).is_positive_or_zero() # KO

is_odd

Is an assertion to verify the given value is odd (not divisible by 2)

assert_that(-7).is_odd() # OK
assert_that(1).is_odd() # OK
assert_that(15).is_odd() # OK

assert_that(2).is_odd() # KO
assert_that(0).is_odd() # KO

is_even

Is an assertion to verify the given value is even (divisible by 2)

assert_that(2).is_even() # OK
assert_that(6).is_even() # OK
assert_that(0).is_even() # OK

assert_that(-7).is_even() # KO
assert_that(1).is_even() # KO

is_equal_to

Is an assertion to verify the given value is equal to another

assert_that(2).is_equal_to(2) # OK
assert_that(6).is_equal_to(6) # OK

assert_that(-7).is_equal_to(0) # KO
assert_that(1).is_equal_to(-1) # KO

is_not_equal_to

Is an assertion to verify the given value is not equal to another

assert_that(2).is_not_equal_to(1) # OK
assert_that(6).is_not_equal_to(0) # OK

assert_that(-7).is_not_equal_to(-7) # KO
assert_that(32).is_not_equal_to(23) # KO

is_strictly_less_than

Is an assertion to verify the given value is strictly less than another value

assert_that(2).is_strictly_less_than(3) # OK
assert_that(-5).is_strictly_less_than(-7) # OK

assert_that(5).is_strictly_less_than(5) # KO
assert_that(32).is_strictly_less_than(21) # KO

is_less_than_or_equal_to

Is an assertion to verify the given value is less than or equals to another value

assert_that(2).is_less_than_or_equal_to(3) # OK
assert_that(2).is_less_than_or_equal_to(2) # OK

assert_that(5).is_less_than_or_equal_to(1) # KO
assert_that(32).is_less_than_or_equal_to(21) # KO

is_strictly_greater_than

Is an assertion to verify the given value is strictly greater than another value

assert_that(2).is_strictly_greater_than(1) # OK
assert_that(-5).is_strictly_greater_than(-7) # OK

assert_that(5).is_strictly_greater_than(5) # KO
assert_that(32).is_strictly_greater_than(45) # KO

is_greater_than_or_equal_to

Is an assertion to verify the given value is greater than or equal to another value

assert_that(2).is_greater_than_or_equal_to(2) # OK
assert_that(-5).is_greater_than_or_equal_to(-7) # OK

assert_that(5).is_greater_than_or_equal_to(6) # KO
assert_that(32).is_greater_than_or_equal_to(45) # KO

is_in_range_inclusive

Is an assertion to verify the given value is included in a given range of values. Starting and ending range values are included.

assert_that(2).is_in_range_inclusive(2, 2) # OK
assert_that(0).is_in_range_inclusive(-5, 5) # OK

assert_that(20).is_in_range_inclusive(1, 10) # KO

assert_that_signal

This utility contains assertion to check for signal emition (with or without arguments). It works with collect_signal and collect_signals_of functions from TestCase class. Using those collection functions, the test case will start to record emitted signals with arguments. Using assert_that_signal, you can then check for emitted signals. If you dont collect signals that you are asserting about, assertions will fail.

Unlike await_for, signal assertions are synchronous, meaning assertions are verified immediatly when method is called.

Collected signals are automatically disconnected and freed.

has_never_been_emitted

Is an assertion to verify that the given signal has never been emitted (with or without arguments)

collect_signals_of(node)
assert_that_signal(node.tree_entered).has_never_been_emitted() # OK
add_child(node)
await get_tree().process_frame
assert_that_signal(node.tree_entered).has_never_been_emitted() # KO

has_been_emitted_only_once

Is an assertion to verify that the given signal has been emitted exactly once (with or without arguments).

collect_signals_of(node)
# assert_that_signal(node.tree_entered).has_been_emitted_only_once() # KO
add_child(node)
await get_tree().process_frame
assert_that_signal(node.tree_entered).has_never_been_emitted() # OK

has_been_emitted

Is an assertion to verify that the given signal has been emitted exactly a number of times (with or without arguments).

collect_signals_of(node)
assert_that_signal(node.tree_entered).has_been_emitted(0) # OK
add_child(node)
await get_tree().process_frame
assert_that_signal(node.tree_entered).has_been_emitted(1) # OK
assert_that_signal(node.tree_entered).has_been_emitted(2) # KO

has_never_been_emitted_with_args

Is an assertion to verify that the given signal has never been emitted with given arguments. If signal has never been emitted or emitted with other arguments, the assertion is successful.

collect_signals_of(npc)
assert_that_signal(npc.on_damage).has_never_been_emitted_with_args([5]) # OK
npc.on_damage.emit(7)
assert_that_signal(npc.on_damage).has_never_been_emitted_with_args([5]) # OK
assert_that_signal(npc.on_damage).has_never_been_emitted_with_args([7]) # KO

has_been_emitted_only_once_with_args

Is an assertion to verify that the given signal has been emitted only once with given arguments. If signal has never been emitted or emitted with other arguments, the assertion is successful. If signal was emitted many times with given arguments, the assertion fails.

collect_signals_of(npc)
# assert_that_signal(npc.on_damage).has_been_emitted_only_once_with_args([7]) # KO
npc.on_damage.emit(7)
assert_that_signal(npc.on_damage).has_been_emitted_only_once_with_args([7]) # OK
npc.on_damage.emit(7)
assert_that_signal(npc.on_damage).has_been_emitted_only_once_with_args([7]) # KO

has_been_emitted_with_args

Is an assertion to verify that the given signal has been emitted exactly a number of times with given arguments. If signal has never been emitted or emitted with other arguments, the assertion is failed. If signal was emitted the required number of times but with unexpected arguments, the assertion fails. If the signal is emitted more or less than the expected number of times but with expected arguments, the assertion fails.

collect_signals_of(npc)
assert_that_signal(npc.on_damage).has_been_emitted_with_args(0, [7]) # OK
npc.on_damage.emit(7)
assert_that_signal(npc.on_damage).has_been_emitted_with_args(1, [7]) # OK
npc.on_damage.emit(5)
npc.on_damage.emit(7)
assert_that_signal(npc.on_damage).has_been_emitted_with_args(2, [7]) # OK
assert_that_signal(npc.on_damage).has_been_emitted_with_args(3, [7]) # KO

has_been_emitted_with_args_in_exact_order

Is an assertion to verify that there exists a sequence of emission of the given signal with given arguments in this exact order. Signal can be emitted before and/or after the sequence, as long as the sequence exists, the assertion will be successful.

collect_signals_of(npc)
# assert_that_signal(npc.on_damage).has_been_emitted_with_args_in_exact_order([5, 6, 3]) # KO
npc.on_damage.emit(7)
# assert_that_signal(npc.on_damage).has_been_emitted_with_args_in_exact_order([5, 6, 3]) # KO
npc.on_damage.emit(5)
npc.on_damage.emit(6)
# assert_that_signal(npc.on_damage).has_been_emitted_with_args_in_exact_order([5, 6, 3]) # KO
npc.on_damage.emit(3)
assert_that_signal(npc.on_damage).has_been_emitted_with_args_in_exact_order([5, 6, 3]) # OK
assert_that_signal(npc.on_damage).has_been_emitted_with_args_in_exact_order([7, 5]) # OK
npc.on_damage.emit(10)
assert_that_signal(npc.on_damage).has_been_emitted_with_args_in_exact_order([5, 6, 3]) # OK

Asynchronous assertions

For asynchronous assertion explanation, please refer to this page.

await_for

This utility offers assertions to test asynchronous behaviors and states. Note that all invocation of this utility must be prefixed by keyword await since all assertions are coroutines. If you forgot to use await instruction, the assertion will fail.

Since this utility make asynchronous assertions, time can pass between assertion call and actual result. For that reason, all assertions can be parametrized before invoking them:

  • at_least : allow to choose the minimum amount of time (in seconds) before expected behavior activation. If the expected behavior occurs before this amount of time, the assertion will fail. Default value is 0,
  • at_most : allow to choose the maximum amount of time (in seconds) to expect a given behavior. In case the behavior occurs after this duration, the assertion will fail.
  • poll_delay : allow to choose the poll delay, in seconds. Some assertions check are waiting for a condition to be true. This condition is checked at regular intervals. The poll delay represents this interval. This parameter is not used by all assertions.

This utility is not design to assert on signals emitted synchronously. To do that, please refer to assert_that_signal.

await await_for("NPC is not moving anymore").at_least(0.5).at_most(5).until(func(): return not _npc.is_moving())

until

Is an instruction to wait until the given Callable returns true. If the condition is fulfilled within the time range defined par àt_leastandat_most`, the assertion is successful, else the assertion is failed.

var _state:int = 0

#...

# Wait at most 3 seconds until _state is 4
await await_for().at_most(3).until(func(): return _state == 4)

until_signal_emitted

Is an instruction to wait until the given signal has been emitted at least once, with any arguments. If the signal is emitted within the time range defined par at_least and at_most, the assertion is successful, else the assertion is failed.

# Wait at least 1 second until node emits signal tree_entered
await await_for().at_least(1).until_signal_emitted(node.tree_entered)

until_signal_emitted_with_args

Is an instruction to wait until the given signal has been emitted at least once, with the given arguments. If the signal is emitted within the time range defined par at_least and at_most, the assertion is successful, else the assertion is failed.

If signal is emitted with another argument value, the condition is not fulfilled.

signal on_damage(value:int, attacker:Node)

# Wait at most 1 second until node emits signal on_damage with value 7 and player
await await_for().at_most(1).until_signal_emitted(npc.on_damage, [7, player])