GutTest

Inherits: Node

This is the base class for your GUT test scripts.

GUT Wiki: https://gut.readthedocs.io

Simple Example

extends GutTest

func before_all():
    gut.p("before_all called"

func before_each():
    gut.p("before_each called")

func after_each():
    gut.p("after_each called")

func after_all():
    gut.p("after_all called")

func test_assert_eq_letters():
    assert_eq("asdf", "asdf", "Should pass")

func test_assert_eq_number_not_equal():
    assert_eq(1, 2, "Should fail.  1 != 2")

Properties

Variant

DOUBLE_STRATEGY

{"INCLUDE_NATIVE": 0, "SCRIPT_ONLY": 1}

Variant

ParameterFactory

<unknown>

Variant

InputFactory

GutInputFactory

Variant

InputSender

<unknown>

GutMain

gut

null

Variant

collected_script

null

Variant

wait_log_delay

0.5

Methods

Variant

add_child_autofree(node, legible_unique_name = false)

Variant

add_child_autoqfree(node, legible_unique_name = false)

void

after_all()

void

after_each()

void

assert_accessors(obj, property, default, set_to)

void

assert_almost_eq(got, expected, error_interval, text = “”)

void

assert_almost_ne(got, not_expected, error_interval, text = “”)

void

assert_between(got, expect_low, expect_high, text = “”)

void

assert_called(inst, method_name = null, parameters = null)

void

assert_called_count(callable: Callable, expected_count: int)

void

assert_connected(p1, p2, p3 = null, p4 = “”)

void

assert_does_not_have(obj, element, text = “”)

void

assert_engine_error(text, msg = “”)

void

assert_engine_error_count(count: int, msg: String = “”)

void

assert_eq(got, expected, text = “”)

void

assert_eq_deep(v1, v2)

void

assert_exports(obj, property_name, type)

void

assert_false(got, text = “”)

void

assert_file_does_not_exist(file_path)

void

assert_file_empty(file_path)

void

assert_file_exists(file_path)

void

assert_file_not_empty(file_path)

void

assert_freed(obj, title = “something”)

void

assert_gt(got, expected, text = “”)

void

assert_gte(got, expected, text = “”)

void

assert_has(obj, element, text = “”)

void

assert_has_method(obj, method, text = “”)

void

assert_has_signal(object, signal_name, text = “”)

void

assert_is(object, a_class, text = “”)

void

assert_lt(got, expected, text = “”)

void

assert_lte(got, expected, text = “”)

void

assert_ne(got, not_expected, text = “”)

void

assert_ne_deep(v1, v2)

void

assert_no_new_orphans(text = “”)

void

assert_not_between(got, expect_low, expect_high, text = “”)

void

assert_not_called(inst, method_name = null, parameters = null)

void

assert_not_connected(p1, p2, p3 = null, p4 = “”)

void

assert_not_freed(obj, title = “something”)

void

assert_not_null(got, text = “”)

void

assert_not_same(v1, v2, text = “”)

void

assert_not_typeof(object, type, text = “”)

void

assert_null(got, text = “”)

void

assert_property(obj, property_name, default_value, new_value)

void

assert_property_with_backing_variable(obj, property_name, default_value, new_value, backed_by_name = null)

void

assert_push_error(text, msg = “”)

void

assert_push_error_count(count: int, msg: String = “”)

void

assert_push_warning(text: String, msg: String = “”)

void

assert_push_warning_count(count: int, msg: String = “”)

void

assert_same(v1, v2, text = “”)

void

assert_signal_emit_count(p1, p2, p3 = 0, p4 = “”)

void

assert_signal_emitted(p1, p2 = “”, p3 = “”)

void

assert_signal_emitted_with_parameters(p1, p2, p3 = -1, p4 = -1)

void

assert_signal_not_emitted(p1, p2 = “”, p3 = “”)

void

assert_string_contains(text, search, match_case = true)

void

assert_string_ends_with(text, search, match_case = true)

void

assert_string_starts_with(text, search, match_case = true)

void

assert_true(got, text = “”)

void

assert_typeof(object, type, text = “”)

Variant

autofree(thing)

Variant

autoqfree(thing)

void

before_all()

void

before_each()

Variant

compare_deep(v1, v2, max_differences = null)

Variant

did_wait_timeout()

Variant

double(thing, double_strat = null, not_used_anymore = null)

Variant

double_singleton(singleton)

void

fail_test(text)

Variant

get_assert_count()

Variant

get_call_count(object, method_name = null, parameters = null)

Variant

get_call_parameters(object, method_name_or_index = -1, idx = -1)

Variant

get_double_strategy()

int

get_elapsed_idle_frames()

int

get_elapsed_msec()

int

get_elapsed_physics_frames()

int

get_elapsed_process_frames()

float

get_elapsed_sec()

int

get_elapsed_usec()

Array

get_errors()

Variant

get_fail_count()

Variant

get_pass_count()

Variant

get_pending_count()

Variant

get_signal_emit_count(p1, p2 = null)

Variant

get_signal_parameters(p1, p2 = null, p3 = -1)

void

ignore_method_when_doubling(thing, method_name)

Variant

is_failing()

Variant

is_passing()

Variant

partial_double(thing, double_strat = null, not_used_anymore = null)

Variant

partial_double_singleton(singleton)

void

pass_test(text)

void

pause_before_teardown()

void

pending(text = “”)

void

print_tracked_errors()

void

register_inner_classes(base_script)

void

replace_node(base_node, path_or_node, with_this)

void

reset_start_times()

void

set_double_strategy(double_strategy)

Variant

should_skip_script()

void

simulate(obj, times, delta, check_is_processing: bool = false)

Variant

skip_if_godot_version_lt(expected)

Variant

skip_if_godot_version_ne(expected)

Variant

stub(thing, p2 = null, p3 = null)

Variant

use_parameters(params)

Variant

wait_for_signal(sig: Signal, max_time, msg = “”)

Variant

wait_idle_frames(x: int, msg = “”)

Variant

wait_physics_frames(x: int, msg = “”)

Variant

wait_process_frames(x: int, msg = “”)

Variant

wait_seconds(time, msg = “”)

Variant

wait_until(callable, max_time, p3 = “”, p4 = “”)

Variant

wait_while(callable, max_time, p3 = “”, p4 = “”)

void

watch_signals(object)

Deprecated

void

assert_call_count(inst, method_name, expected_count, parameters = null)

Deprecated

void

assert_setget(instance, name_property, const_or_setter = null, getter = “__not_set__”)

Deprecated

Variant

double_inner(path, subpath, strategy = null)

Deprecated

Variant

double_scene(path, strategy = null)

Deprecated

Variant

double_script(path, strategy = null)

Deprecated

Variant

wait_frames(frames: int, msg = “”)

Deprecated

Variant

yield_for(time, msg = “”)

Deprecated

Variant

yield_frames(frames, msg = “”)

Deprecated

Variant

yield_to(obj, signal_name, max_wait, msg = “”)

Internal Use

void

clear_signal_watcher()

Internal Use

Variant

get_logger()

Internal Use

Variant

get_summary()

Internal Use

Variant

get_summary_text()

Internal Use

Variant

run_x_times(x)

Internal Use

void

set_logger(logger)


Enumerations

enum PropertyUsageFlags: 🔗

PropertyUsageFlags VARIABLE_PROPERTY = 4096

No description


Constants

EDITOR_PROPERTY = 4102 🔗

No description


Property Descriptions

Variant DOUBLE_STRATEGY = {"INCLUDE_NATIVE": 0, "SCRIPT_ONLY": 1} 🔗

No description


Variant ParameterFactory = <unknown> 🔗

Reference to addons/gut/parameter_factory.gd script.


Variant InputFactory = GutInputFactory 🔗

Reference to GutInputFactory class that was originally used to reference the Input Factory before the class_name was introduced.


Variant InputSender = <unknown> 🔗

Reference to GutInputSender. This was the way you got to the GutInputSender before it was given a class_name


GutMain gut = null 🔗

No description


Variant collected_script = null 🔗

No description


Variant wait_log_delay = 0.5 🔗

  • void @wait_log_delay_setter(value)

No description


Method Descriptions

Variant should_skip_script() 🔗

Virtual Method. This is run after the script has been prepped for execution, but before before_all is executed. If you implement this method and return true or a String (the string is displayed in the log) then GUT will stop executing the script and mark it as risky. You might want to do this because: - You are porting tests from 3.x to 4.x and you don’t want to comment everything out.

  • Skipping tests that should not be run when in headless mode such as input testing that does not work in headless.

func should_skip_script():
    if DisplayServer.get_name() == "headless":
        return "Skip Input tests when running headless"
  • If you have tests that would normally cause the debugger to break on an error, you can skip the script if the debugger is enabled so that the run is not interrupted.

func should_skip_script():
    return EngineDebugger.is_active()

void before_all() 🔗

Virtual method. Run once before anything else in the test script is run.


void before_each() 🔗

Virtual method. Run before each test is executed


void after_each() 🔗

Virtual method. Run after each test is executed.


void after_all() 🔗

Virtual method. Run after all tests have been run.


void pending(text = “”) 🔗

Mark the current test as pending.


Variant is_passing() 🔗

Returns true if the test is passing as of the time of this call. False if not.


Variant is_failing() 🔗

Returns true if the test is failing as of the time of this call. False if not.


void pass_test(text) 🔗

Marks the test as passing. Does not override any failing asserts or calls to fail_test. Same as a passing assert.


void fail_test(text) 🔗

Marks the test as failing. Same as a failing assert.


void clear_signal_watcher() 🔗

Internal use only.


Variant get_double_strategy() 🔗

Returns the current double strategy.


void set_double_strategy(double_strategy) 🔗

Sets the double strategy for all tests in the script. This should usually be done in before_all. The double strtegy can be set per run/script/double. See Double-Strategy


void pause_before_teardown() 🔗

This method will cause Gut to pause before it moves on to the next test. This is useful for debugging, for instance if you want to investigate the screen or anything else after a test has finished executing.

Sometimes you get lazy, and you don’t remove calls to pause_before_teardown after you are done with them. You can tell GUT to ignore calls to this method through the panel or the command line. Setting this in your .gutconfig.json file is recommended for CI/CD Pipelines.


Variant get_logger() 🔗

Internal use only.


void set_logger(logger) 🔗

Internal use only.


void watch_signals(object) 🔗

This must be called in order to make assertions based on signals being emitted. __Right now, this only supports signals that are emitted with 9 or less parameters.__ This can be extended but nine seemed like enough for now. The Godot documentation suggests that the limit is four but in my testing I found you can pass more.

This must be called in each test in which you want to make signal based assertions in. You can call it multiple times with different objects. You should not call it multiple times with the same object in the same test. The objects that are watched are cleared after each test (specifically right before teardown is called). Under the covers, Gut will connect to all the signals an object has and it will track each time they fire. You can then use the following asserts and methods to verify things are acting correct.


Variant get_signal_emit_count(p1, p2 = null) 🔗

This will return the number of times a signal was fired. This gives you the freedom to make more complicated assertions if the spirit moves you. This will return -1 if the signal was not fired or the object was not being watched, or if the object does not have the signal.

Accepts either the object and the signal name or the signal.


Variant get_signal_parameters(p1, p2 = null, p3 = -1) 🔗

If you need to inspect the parameters in order to make more complicate assertions, then this will give you access to the parameters of any watched signal. This works the same way that assert_signal_emitted_with_parameters does. It takes an object, signal name, and an optional index. If the index is not specified then the parameters from the most recent emission will be returned. If the object is not being watched, the signal was not fired, or the object does not have the signal then null will be returned.

Signatures:

  • get_signal_parameters(p1:Signal, p2:parameter-index (optional))

  • get_signal_parameters(p1:object, p2:signal name, p3:parameter-index (optional))

Examples:

class SignalObject:
    signal some_signal
    signal other_signal


func test_get_signal_parameters():
    var obj = SignalObject.new()
    watch_signals(obj)
    obj.some_signal.emit(1, 2, 3)
    obj.some_signal.emit('a', 'b', 'c')

    # -- Passing --
    # passes because get_signal_parameters returns the most recent emission
    # by default
    assert_eq(get_signal_parameters(obj, 'some_signal'), ['a', 'b', 'c'])
    assert_eq(get_signal_parameters(obj.some_signal), ['a', 'b', 'c'])

    assert_eq(get_signal_parameters(obj, 'some_signal', 0), [1, 2, 3])
    assert_eq(get_signal_parameters(obj.some_signal, 0), [1, 2, 3])

    # if the signal was not fired null is returned
    assert_null(get_signal_parameters(obj, 'other_signal'))
    # if the signal does not exist or isn't being watched null is returned
    assert_null(get_signal_parameters(obj, 'signal_dne'))

    # -- Failing --
    assert_eq(get_signal_parameters(obj, 'some_signal'), [1, 2, 3])
    assert_eq(get_signal_parameters(obj.some_signal, 0), ['a', 'b', 'c'])

Variant get_call_parameters(object, method_name_or_index = -1, idx = -1) 🔗

Get the parameters for a method call to a doubled object. By default it will return the most recent call. You can optionally specify an index for which call you want to get the parameters for. Can be called using a Callable for the first parameter instead of specifying an object and method name. When you do this, the seoncd parameter is used as the index. Returns: * an array of parameter values if a call the method was found * null when a call to the method was not found or the index specified was invalid.


Variant get_call_count(object, method_name = null, parameters = null) 🔗

Returns the call count for a method with optional paramter matching. Can be called with a Callable instead of an object, method_name, and parameters. Bound arguments will be used to match call arguments.


void simulate(obj, times, delta, check_is_processing: bool = false) 🔗

Simulate a number of frames by calling ‘_process’ and ‘_physics_process’ (if the methods exist) on an object and all of its descendents. The specified frame time, ‘delta’, will be passed to each simulated call. NOTE: Objects can disable their processing methods using ‘set_process(false)’ and ‘set_physics_process(false)’. This is reflected in the ‘Object’ methods ‘is_processing()’ and ‘is_physics_processing()’, respectively. To make ‘simulate’ respect this status, for example if you are testing an object which toggles processing, pass ‘check_is_processing’ as ‘true’.


void replace_node(base_node, path_or_node, with_this) 🔗

No description


Variant use_parameters(params) 🔗

Use this as the default value for the first parameter to a test to create a parameterized test. See also the ParameterFactory and Parameterized Tests.

Example

func test_with_parameters(p = use_parameters([1, 2, 3])):

Variant run_x_times(x) 🔗

Internal use only. When used as the default for a test method parameter, it will cause the test to be run x times. I Hacked this together to test a method that was occassionally failing due to timing issues. I don’t think it’s a great idea, but you be the judge. If you find a good use for it, let me know and I’ll make it a legit member of the api.


Variant skip_if_godot_version_lt(expected) 🔗

Checks the passed in version string (x.x.x) against the engine version to see if the engine version is less than the expected version. If it is then the test is mareked as passed (for a lack of anything better to do). The result of the check is returned.

Example

if(skip_if_godot_version_lt('3.5.0')):
    return

Variant skip_if_godot_version_ne(expected) 🔗

Checks if the passed in version matches the engine version. The passed in version can contain just the major, major.minor or major.minor.path. If the version is not the same then the test is marked as passed. The result of the check is returned.

Example

if(skip_if_godot_version_ne('3.4')):
   return

void register_inner_classes(base_script) 🔗

Registers all the inner classes in a script with the doubler. This is required before you can double any inner class.


Variant compare_deep(v1, v2, max_differences = null) 🔗

Peforms a deep compare on both values, a CompareResult instnace is returned. The optional max_differences paramter sets the max_differences to be displayed.


void reset_start_times() 🔗

Resets the time/frame tracking statistics for the current test method


float get_elapsed_sec() 🔗

Returns the number of seconds elapsed since test method began as a float.


int get_elapsed_msec() 🔗

Returns the number of milliseconds elapsed since test method began as a float.


int get_elapsed_usec() 🔗

Returns the number of microseconds elapsed since test method began as a float.


int get_elapsed_idle_frames() 🔗

Alias for wait_process_frames


int get_elapsed_process_frames() 🔗

Returns the number of process/idle frames elapsed since the test method began.


int get_elapsed_physics_frames() 🔗

Returns the number of physics frames elapsed since the test method began.


void assert_eq(got, expected, text = “”) 🔗

Asserts that the expected value equals the value got. assert got == expected and prints optional text. See Comparing-Things for information about comparing dictionaries and arrays.

See also: assert_ne, assert_same, assert_not_same

var one = 1
var node1 = Node.new()
var node2 = node1

# Passing
assert_eq(one, 1, 'one should equal one')
assert_eq('racecar', 'racecar')
assert_eq(node2, node1)
assert_eq([1, 2, 3], [1, 2, 3])
var d1_pass = {'a':1}
var d2_pass = d1_pass
assert_eq(d1_pass, d2_pass)

# Failing
assert_eq(1, 2) # FAIL
assert_eq('hello', 'world')
assert_eq(self, node1)
assert_eq([1, 'two', 3], [1, 2, 3, 4])
assert_eq({'a':1}, {'a':1})

void assert_ne(got, not_expected, text = “”) 🔗

asserts got != expected and prints optional text. See Comparing-Things for information about comparing dictionaries and arrays.

See also: assert_eq, assert_same, assert_not_same

var two = 2
var node1 = Node.new()

# Passing
assert_ne(two, 1, 'Two should not equal one.')
assert_ne('hello', 'world')
assert_ne(self, node1)

# Failing
assert_ne(two, 2)
assert_ne('one', 'one')
assert_ne('2', 2)

void assert_almost_eq(got, expected, error_interval, text = “”) 🔗

Asserts that got is within the range of expected +/- error_interval. The upper and lower bounds are included in the check. Verified to work with integers, floats, and Vector2. Should work with anything that can be added/subtracted.

# Passing
assert_almost_eq(0, 1, 1, '0 within range of 1 +/- 1')
assert_almost_eq(2, 1, 1, '2 within range of 1 +/- 1')
assert_almost_eq(1.2, 1.0, .5, '1.2 within range of 1 +/- .5')
assert_almost_eq(.5, 1.0, .5, '.5 within range of 1 +/- .5')
assert_almost_eq(Vector2(.5, 1.5), Vector2(1.0, 1.0), Vector2(.5, .5))
assert_almost_eq(Vector2(.5, 1.5), Vector2(1.0, 1.0), Vector2(.25, .25))

# Failing
assert_almost_eq(1, 3, 1, '1 outside range of 3 +/- 1')
assert_almost_eq(2.6, 3.0, .2, '2.6 outside range of 3 +/- .2')

void assert_almost_ne(got, not_expected, error_interval, text = “”) 🔗

This is the inverse of assert_almost_eq. This will pass if got is outside the range of not_expected +/- error_interval.


void assert_gt(got, expected, text = “”) 🔗

assserts got > expected

var bigger = 5
var smaller = 0

# Passing
assert_gt(bigger, smaller, 'Bigger should be greater than smaller')
assert_gt('b', 'a')
assert_gt('a', 'A')
assert_gt(1.1, 1)

# Failing
assert_gt('a', 'a')
assert_gt(1.0, 1)
assert_gt(smaller, bigger)

void assert_gte(got, expected, text = “”) 🔗

Asserts got is greater than or equal to expected.

var bigger = 5
var smaller = 0

# Passing
assert_gte(bigger, smaller, 'Bigger should be greater than or equal to smaller')
assert_gte('b', 'a')
assert_gte('a', 'A')
assert_gte(1.1, 1)
assert_gte('a', 'a')

# Failing
assert_gte(0.9, 1.0)
assert_gte(smaller, bigger)

void assert_lt(got, expected, text = “”) 🔗

Asserts got is less than expected

var bigger = 5
var smaller = 0

# Passing
assert_lt(smaller, bigger, 'Smaller should be less than bigger')
assert_lt('a', 'b')
assert_lt(99, 100)

# Failing
assert_lt('z', 'x')
assert_lt(-5, -5)

void assert_lte(got, expected, text = “”) 🔗

Asserts got is less than or equal to expected


void assert_true(got, text = “”) 🔗

asserts that got is true. Does not assert truthiness, only boolean values will pass.


void assert_false(got, text = “”) 🔗

Asserts that got is false. Does not assert truthiness, only boolean values will pass.


void assert_between(got, expect_low, expect_high, text = “”) 🔗

Asserts value is between (inclusive) the two expected values.

got >= expect_low and <= expect_high

# Passing
assert_between(5, 0, 10, 'Five should be between 0 and 10')
assert_between(10, 0, 10)
assert_between(0, 0, 10)
assert_between(2.25, 2, 4.0)

# Failing
assert_between('a', 'b', 'c')
assert_between(1, 5, 10)

void assert_not_between(got, expect_low, expect_high, text = “”) 🔗

Asserts value is not between (exclusive) the two expected values.

asserts that got <= expect_low or got >= expect_high.

# Passing
assert_not_between(1, 5, 10)
assert_not_between('a', 'b', 'd')
assert_not_between('d', 'b', 'd')
assert_not_between(10, 0, 10)
assert_not_between(-2, -2, 10)

# Failing
assert_not_between(5, 0, 10, 'Five shouldnt be between 0 and 10')
assert_not_between(0.25, -2.0, 4.0)

void assert_has(obj, element, text = “”) 🔗

Uses the ‘has’ method of the object passed in to determine if it contains the passed in element.

var an_array = [1, 2, 3, 'four', 'five']
var a_hash = { 'one':1, 'two':2, '3':'three'}

# Passing
assert_has(an_array, 'four') # PASS
assert_has(an_array, 2) # PASS
# the hash's has method checks indexes not values
assert_has(a_hash, 'one') # PASS
assert_has(a_hash, '3') # PASS

# Failing
assert_has(an_array, 5) # FAIL
assert_has(an_array, self) # FAIL
assert_has(a_hash, 3) # FAIL
assert_has(a_hash, 'three') # FAIL

void assert_does_not_have(obj, element, text = “”) 🔗

The inverse of assert_has.


void assert_file_exists(file_path) 🔗

asserts a file exists at the specified path

func before_each():
    gut.file_touch('user://some_test_file')

func after_each():
    gut.file_delete('user://some_test_file')

func test_assert_file_exists():
    # Passing
    assert_file_exists('res://addons/gut/gut.gd')
    assert_file_exists('user://some_test_file')

    # Failing
    assert_file_exists('user://file_does_not.exist')
    assert_file_exists('res://some_dir/another_dir/file_does_not.exist')

void assert_file_does_not_exist(file_path) 🔗

asserts a file does not exist at the specified path

func before_each():
    gut.file_touch('user://some_test_file')

func after_each():
    gut.file_delete('user://some_test_file')

func test_assert_file_does_not_exist():
    # Passing
    assert_file_does_not_exist('user://file_does_not.exist')
    assert_file_does_not_exist('res://some_dir/another_dir/file_does_not.exist')

    # Failing
    assert_file_does_not_exist('res://addons/gut/gut.gd')

void assert_file_empty(file_path) 🔗

asserts the specified file is empty

func before_each():
    gut.file_touch('user://some_test_file')

func after_each():
    gut.file_delete('user://some_test_file')

func test_assert_file_empty():
    # Passing
    assert_file_empty('user://some_test_file')

    # Failing
    assert_file_empty('res://addons/gut/gut.gd')

void assert_file_not_empty(file_path) 🔗

Asserts the specified file is not empty

func before_each():
    gut.file_touch('user://some_test_file')

func after_each():
    gut.file_delete('user://some_test_file')

func test_assert_file_not_empty():
    # Passing
    assert_file_not_empty('res://addons/gut/gut.gd') # PASS

    # Failing
    assert_file_not_empty('user://some_test_file') # FAIL

void assert_has_method(obj, method, text = “”) 🔗

Asserts that the passed in object has a method named method.


void assert_accessors(obj, property, default, set_to) 🔗

This is meant to make testing public get/set methods for a member variable. This was originally created for early Godot 3.x setter and getter methods. See assert_property for verifying Godot 4.x accessors. This makes multiple assertions to verify:

  • The object has a method called get_<PROPERTY_NAME>

  • The object has a method called set_<PROPERTY_NAME>

  • The method get_<PROPERTY_NAME> returns the expected default value when first called.

  • Once you set the property, the get_<PROPERTY_NAME> returns the new value.


void assert_exports(obj, property_name, type) 🔗

Asserts that obj exports a property with the name property_name and a type of type. The type must be one of the various Godot built-in TYPE_ constants.

class ExportClass:
    export var some_number = 5
    export(PackedScene) var some_scene
    var some_variable = 1

func test_assert_exports():
    var obj = ExportClass.new()

    # Passing
    assert_exports(obj, "some_number", TYPE_INT)
    assert_exports(obj, "some_scene", TYPE_OBJECT)

    # Failing
    assert_exports(obj, 'some_number', TYPE_VECTOR2)
    assert_exports(obj, 'some_scene', TYPE_AABB)
    assert_exports(obj, 'some_variable', TYPE_INT)

void assert_connected(p1, p2, p3 = null, p4 = “”) 🔗

Asserts that signaler_obj is connected to connect_to_obj on signal signal_name. The method that is connected is optional. If method_name is supplied then this will pass only if the signal is connected to the method. If it is not provided then any connection to the signal will cause a pass.

Signatures:

  • assert_connected(p1:Signal, p2:connected-object)

  • assert_connected(p1:Signal, p2:connected-method)

  • assert_connected(p1:object, p2:connected-object, p3:signal-name, p4: connected-method-name <optional>)

Examples:

class Signaler:
    signal the_signal

class Connector:
    func connect_this():
        pass
    func  other_method():
        pass

func test_assert_connected():
    var signaler = Signaler.new()
    var connector  = Connector.new()
    signaler.the_signal.connect(connector.connect_this)

    # Passing
    assert_connected(signaler.the_signal, connector.connect_this)
    assert_connected(signaler.the_signal, connector)
    assert_connected(signaler, connector, 'the_signal')
    assert_connected(signaler, connector, 'the_signal', 'connect_this')

    # Failing
    assert_connected(signaler.the_signal, connector.other_method)

    var foo = Connector.new()
    assert_connected(signaler,  connector, 'the_signal', 'other_method')
    assert_connected(signaler, connector, 'other_signal')
    assert_connected(signaler, foo, 'the_signal')

void assert_not_connected(p1, p2, p3 = null, p4 = “”) 🔗

The inverse of assert_connected. See assert_connected for parameter syntax.

This will fail with specific messages if the target object is connected to the specified signal on the source object.


void assert_signal_emitted(p1, p2 = “”, p3 = “”) 🔗

Assert that the specified object emitted the named signal. You must call watch_signals and pass it the object that you are making assertions about. This will fail if the object is not being watched or if the object does not have the specified signal. Since this will fail if the signal does not exist, you can often skip using assert_has_signal.

Signatures:

  • assert_signal_emitted(p1:Signal, p2: text <optional>)

  • assert_signal_emitted(p1:object, p2:signal-name, p3: text <optional>)

Examples:

class SignalObject:
    signal some_signal
    signal other_signal


func test_assert_signal_emitted():
    var obj = SignalObject.new()

    watch_signals(obj)
    obj.emit_signal('some_signal')

    ## Passing
    assert_signal_emitted(obj, 'some_signal')
    assert_signal_emitted(obj.some_signal)

    ## Failing
    # Fails with specific message that the object does not have the signal
    assert_signal_emitted(obj, 'signal_does_not_exist')
    # Fails because the object passed is not being watched
    assert_signal_emitted(SignalObject.new(), 'some_signal')
    # Fails because the signal was not emitted
    assert_signal_emitted(obj, 'other_signal')
    assert_signal_emitted(obj.other_signal)

void assert_signal_not_emitted(p1, p2 = “”, p3 = “”) 🔗

This works opposite of assert_signal_emitted. This will fail if the object is not being watched or if the object does not have the signal.

Signatures:

  • assert_signal_not_emitted(p1:Signal, p2: text <optional>)

  • assert_signal_not_emitted(p1:object, p2:signal-name, p3: text <optional>)

Examples:

class SignalObject:
    signal some_signal
    signal other_signal

func test_assert_signal_not_emitted():
    var obj = SignalObject.new()

    watch_signals(obj)
    obj.emit_signal('some_signal')

    # Passing
    assert_signal_not_emitted(obj, 'other_signal')
    assert_signal_not_emitted(obj.other_signal)

    # Failing
    # Fails with specific message that the object does not have the signal
    assert_signal_not_emitted(obj, 'signal_does_not_exist')
    # Fails because the object passed is not being watched
    assert_signal_not_emitted(SignalObject.new(), 'some_signal')
    # Fails because the signal was emitted
    assert_signal_not_emitted(obj, 'some_signal')

void assert_signal_emitted_with_parameters(p1, p2, p3 = -1, p4 = -1) 🔗

Asserts that a signal was fired with the specified parameters. The expected parameters should be passed in as an array. An optional index can be passed when a signal has fired more than once. The default is to retrieve the most recent emission of the signal.

This will fail with specific messages if the object is not being watched or the object does not have the specified signal

Signatures:

  • assert_signal_emitted_with_parameters(p1:Signal, p2:expected-parameters, p3: index <optional>)

  • assert_signal_emitted_with_parameters(p1:object, p2:signal-name, p3:expected-parameters, p4: index <optional>)

Examples:

class SignalObject:
    signal some_signal
    signal other_signal

func test_assert_signal_emitted_with_parameters():
    var obj = SignalObject.new()

    watch_signals(obj)
    # emit the signal 3 times to illustrate how the index works in
    # assert_signal_emitted_with_parameters
    obj.emit_signal('some_signal', 1, 2, 3)
    obj.emit_signal('some_signal', 'a', 'b', 'c')
    obj.emit_signal('some_signal', 'one', 'two', 'three')

    # Passing
    # Passes b/c the default parameters to check are the last emission of
    # the signal
    assert_signal_emitted_with_parameters(obj, 'some_signal', ['one', 'two', 'three'])
    assert_signal_emitted_with_parameters(obj.some_signal, ['one', 'two', 'three'])

    # Passes because the parameters match the specified emission based on index.
    assert_signal_emitted_with_parameters(obj, 'some_signal', [1, 2, 3], 0)
    assert_signal_emitted_with_parameters(obj.some_signal, [1, 2, 3], 0)

    # Failing
    # Fails with specific message that the object does not have the signal
    assert_signal_emitted_with_parameters(obj, 'signal_does_not_exist', [])
    # Fails because the object passed is not being watched
    assert_signal_emitted_with_parameters(SignalObject.new(), 'some_signal', [])
    # Fails because parameters do not match latest emission
    assert_signal_emitted_with_parameters(obj, 'some_signal', [1, 2, 3])
    # Fails because the parameters for the specified index do not match
    assert_signal_emitted_with_parameters(obj, 'some_signal', [1, 2, 3], 1)

void assert_signal_emit_count(p1, p2, p3 = 0, p4 = “”) 🔗

Asserts that a signal fired a specific number of times.

Signatures:

  • assert_signal_emit_count(p1:Signal, p2:expected-count, p3: text <optional>)

  • assert_signal_emit_count(p1:object, p2:signal-name, p3:expected-count, p4: text <optional>)

Examples:

class SignalObject:
    signal some_signal
    signal other_signal


func test_assert_signal_emit_count():
    var obj_a = SignalObject.new()
    var obj_b = SignalObject.new()

    watch_signals(obj_a)
    watch_signals(obj_b)

    obj_a.emit_signal('some_signal')
    obj_a.emit_signal('some_signal')

    obj_b.emit_signal('some_signal')
    obj_b.emit_signal('other_signal')

    # Passing
    assert_signal_emit_count(obj_a, 'some_signal', 2, 'passes')
    assert_signal_emit_count(obj_a.some_signal, 2, 'passes')

    assert_signal_emit_count(obj_a, 'other_signal', 0)
    assert_signal_emit_count(obj_a.other_signal, 0)

    assert_signal_emit_count(obj_b, 'other_signal', 1)

    # Failing
    # Fails with specific message that the object does not have the signal
    assert_signal_emit_count(obj_a, 'signal_does_not_exist', 99)
    # Fails because the object passed is not being watched
    assert_signal_emit_count(SignalObject.new(), 'some_signal', 99)
    # The following fail for obvious reasons
    assert_signal_emit_count(obj_a, 'some_signal', 0)
    assert_signal_emit_count(obj_b, 'other_signal', 283)

void assert_has_signal(object, signal_name, text = “”) 🔗

Asserts the passed in object has a signal with the specified name. It should be noted that all the asserts that verify a signal was/wasn’t emitted will first check that the object has the signal being asserted against. If it does not, a specific failure message will be given. This means you can usually skip the step of specifically verifying that the object has a signal and move on to making sure it emits the signal correctly.

class SignalObject:
    signal some_signal
    signal other_signal

func test_assert_has_signal():
    var obj = SignalObject.new()

    ## Passing
    assert_has_signal(obj, 'some_signal')
    assert_has_signal(obj, 'other_signal')

    ## Failing
    assert_has_signal(obj, 'not_a real SIGNAL')
    assert_has_signal(obj, 'yea, this one doesnt exist either')
    # Fails because the signal is not a user signal.  Node2D does have the
    # specified signal but it can't be checked this way.  It could be watched
    # and asserted that it fired though.
    assert_has_signal(Node2D.new(), 'exit_tree')

void assert_is(object, a_class, text = “”) 🔗

Asserts that object extends a_class. object must be an instance of an object. It cannot be any of the built in classes like Array or Int or Float. a_class must be a class, it can be loaded via load, a GDNative class such as Node or Label or anything else.

# Passing
assert_is(Node2D.new(), Node2D)
assert_is(Label.new(), CanvasItem)
assert_is(SubClass.new(), BaseClass)
# Since this is a test script that inherits from test.gd, so
# this passes.  It's not obvious w/o seeing the whole script
# so I'm telling you.  You'll just have to trust me.
assert_is(self, load('res://addons/gut/test.gd'))

var Gut = load('res://addons/gut/gut.gd')
var a_gut = Gut.new()
assert_is(a_gut, Gut)

# Failing
assert_is(Node2D.new(), Node2D.new())
assert_is(BaseClass.new(), SubClass)
assert_is('a', 'b')
assert_is([], Node)

void assert_typeof(object, type, text = “”) 🔗

Asserts that object is the the type specified. type should be one of the Godot TYPE_ constants.

# Passing
var c = Color(1, 1, 1, 1)
gr.test.assert_typeof(c, TYPE_COLOR)
assert_pass(gr.test)

# Failing
gr.test.assert_typeof('some string', TYPE_INT)
assert_fail(gr.test)

void assert_not_typeof(object, type, text = “”) 🔗

The inverse of assert_typeof


void assert_string_contains(text, search, match_case = true) 🔗

Assert that text contains search. Can perform case insensitive search by passing false for match_case.

# Passing
assert_string_contains('abc 123', 'a')
assert_string_contains('abc 123', 'BC', false)
assert_string_contains('abc 123', '3')

# Failing
assert_string_contains('abc 123', 'A')
assert_string_contains('abc 123', 'BC')
assert_string_contains('abc 123', '012')

void assert_string_starts_with(text, search, match_case = true) 🔗

Assert that text starts with search. Can perform case insensitive check by passing false for match_case

# Passing
assert_string_starts_with('abc 123', 'a')
assert_string_starts_with('abc 123', 'ABC', false)
assert_string_starts_with('abc 123', 'abc 123')

## Failing
assert_string_starts_with('abc 123', 'z')
assert_string_starts_with('abc 123', 'ABC')
assert_string_starts_with('abc 123', 'abc 1234')

void assert_string_ends_with(text, search, match_case = true) 🔗

Assert that text ends with search. Can perform case insensitive check by passing false for match_case

## Passing
assert_string_ends_with('abc 123', '123')
assert_string_ends_with('abc 123', 'C 123', false)
assert_string_ends_with('abc 123', 'abc 123')

## Failing
assert_string_ends_with('abc 123', '1234')
assert_string_ends_with('abc 123', 'C 123')
assert_string_ends_with('abc 123', 'nope')

void assert_called(inst, method_name = null, parameters = null) 🔗

Assert that a method was called on an instance of a doubled class. If parameters are supplied then the params passed in when called must match. Can be called with a Callabe instead of specifying the object, method_name, and parameters. The Callable’s object must be a double. Bound arguments will be used to match calls based on values passed to the method.

See also: Doubles, Spies

Examples

var my_double = double(Foobar).new()
...
assert_called(my_double, 'foo')
assert_called(my_double.foo)
assert_called(my_double, 'foo', [1, 2, 3])
assert_called(my_double.foo.bind(1, 2, 3))

void assert_not_called(inst, method_name = null, parameters = null) 🔗

Assert that a method was not called on an instance of a doubled class. If parameters are specified then this will only fail if it finds a call that was sent matching parameters. Can be called with a Callabe instead of specifying the object, method_name, and parameters. The Callable’s object must be a double. Bound arguments will be used to match calls based on values passed to the method.

See also: Doubles, Spies

Examples

assert_not_called(my_double, 'foo')
assert_not_called(my_double.foo)
assert_not_called(my_double, 'foo', [1, 2, 3])
assert_not_called(my_double.foo.bind(1, 2, 3))

void assert_called_count(callable: Callable, expected_count: int) 🔗

Asserts the the method of a double was called an expected number of times. If any arguments are bound to the callable then only calls with matching arguments will be counted.

See also: Doubles, Spies

Examples

# assert foo was called on my_double 5 times
assert_called_count(my_double.foo, 5)
# assert foo, with parameters [1,2,3], was called on my_double 4 times.
assert_called_count(my_double.foo.bind(1, 2, 3), 4)

void assert_null(got, text = “”) 🔗

Asserts the passed in value is null


void assert_not_null(got, text = “”) 🔗

Asserts the passed in value is not null.


void assert_freed(obj, title = “something”) 🔗

Asserts that the passed in object has been freed. This assertion requires that you pass in some text in the form of a title since, if the object is freed, we won’t have anything to convert to a string to put in the output statement.

Note that this currently does not detect if a node has been queued free.

var obj = Node.new()
obj.free()
test.assert_freed(obj, "New Node")

void assert_not_freed(obj, title = “something”) 🔗

The inverse of assert_freed


void assert_no_new_orphans(text = “”) 🔗

This method will assert that no orphaned nodes have been introduced by the test when the assert is executed. See the Memory-Management page for more information.


void assert_property_with_backing_variable(obj, property_name, default_value, new_value, backed_by_name = null) 🔗

Assumes backing varible with be _<property_name>. This will perform all the asserts of assert_property. Then this will set the value through the setter and check the backing variable value. It will then reset throught the setter and set the backing variable and check the getter.


void assert_property(obj, property_name, default_value, new_value) 🔗

This will verify that the method has a setter and getter for the property. It will then use the getter to check the default. Then use the setter with new_value and verify the getter returns the same value.


void assert_eq_deep(v1, v2) 🔗

Performs a deep comparison between two arrays or dictionaries and asserts they are equal. If they are not equal then a formatted list of differences are displayed. See Comparing-Things for more information.


void assert_ne_deep(v1, v2) 🔗

Performs a deep comparison of two arrays or dictionaries and asserts they are not equal. See Comparing-Things for more information.


void assert_same(v1, v2, text = “”) 🔗

Assert v1 and v2 are the same using is_same. See @GlobalScope.is_same.


void assert_not_same(v1, v2, text = “”) 🔗

Assert using v1 and v2 are not the same using is_same. See @GlobalScope.is_same.


Array get_errors() 🔗

Get all the errors generated by the test up to this point. Each error is an instance of GutTrackedError. Setting the GutTrackedError.handled handled property of an element in the array will prevent it from causing a test to fail.

This method allows you to inspect the details of any errors that occurred and decide if it’s the error you are expecting or not.

func divide_them(a, b):
    return a / b

func test_with_script_error():
    divide_them('one', 44)
    push_error('this is a push error')
    var errs = get_errors()
    assert_eq(errs.size(), 2, 'expected error count')

    # Maybe inspect some properties of the errors here.

    # Mark all the errors as handled.
    for e in errs:
        e.handled = true

See GutTrackedError, Error-Tracking.


void assert_engine_error_count(count: int, msg: String = “”) 🔗

Asserts that a number of engine errors were generated by the test.

Note: Each error can only be asserted against once.

func divide_them(a, b):
    return a / b

func test_asserting_engine_error_count():
    divide_them('one', 44)
    assert_engine_error_count(1, "expecing a script error")

func test_no_errors():
    assert_engine_error_count(0, 'should be no errors here')

func test_this_fails_cannot_assert_an_error_twice():
    divide_them('one', 44)
    assert_engine_error_count(1, "expecing a script error")
    # this assert will fail because we already counted it.
    assert_engine_error_count(1, "expecing a script error")

See Error-Tracking.


void assert_engine_error(text, msg = “”) 🔗

Asserts that a single engine error containing text (case insensitive) was generated by the test. If the expected error is found then this assert will pass and the test will not fail from that engine error.

Note: Each error can only be asserted against once.

func divide_them(a, b):
    return a / b

func test_asserting_engine_error_text():
    divide_them('word', 91)
    assert_engine_error('invalid operands')

func test_asserting_multipe_engine_error_texts():
    divide_them('foo', Node)
    divide_them(1729, 0)
    assert_engine_error('Division by zero')
    assert_engine_error('invalid operands')

See Error-Tracking.


void assert_push_error_count(count: int, msg: String = “”) 🔗

Asserts that a number of push_errors were generated by the test.

Note: Each error can only be asserted against once.

func test_with_push_error():
    push_error("This is an error")
    assert_push_error(1, 'This test should have caused a push_error)

See Error-Tracking.


void assert_push_error(text, msg = “”) 🔗

Asserts that a single push error containing text (case insensitive) was generated by the test. If the expected error is found then this assert will pass and the test will not fail from that push_error.

Note: Each error can only be asserted against once.

func test_push_error_text():
    push_error("SpecialText")
    assert_push_error("CIALtex")

func test_push_error_multiple_texts():
    push_error("Error One")
    push_error("Expception two")
    assert_push_error("one")
    assert_push_error("two")

See Error-Tracking.


void assert_push_warning_count(count: int, msg: String = “”) 🔗

Asserts that a number of push_warning were generated by the test.

Note: Each warning can only be asserted against once.

See Error-Tracking.


void assert_push_warning(text: String, msg: String = “”) 🔗

Asserts that a single push warning containing text (case insensitive) was generated byt the test.

Note: Each warning can only be asserted against once.

See Error-Tracking.


void print_tracked_errors() 🔗

Prints all detected engine errors, push_error, and push_warning that were generated by the test.


Variant wait_seconds(time, msg = “”) 🔗

Use with await to wait an amount of time in seconds. The optional message will be printed when the await starts.

See Awaiting


Variant wait_for_signal(sig: Signal, max_time, msg = “”) 🔗

Use with await to wait for a signal to be emitted or a maximum amount of time. Returns true if the signal was emitted, false if not.

See Awaiting


Variant wait_frames(frames: int, msg = “”) 🔗

Deprecated: This method may be changed or removed in future versions.

Use wait_physics_frames or wait_process_frames See Awaiting.


Variant wait_physics_frames(x: int, msg = “”) 🔗

This returns a signal that is emitted after x physics frames have elpased. You can await this method directly to pause execution for x physics frames. The frames are counted prior to _physics_process being called on any node (when SceneTree.physics_frame is emitted). This means the signal is emitted after x frames and just before the x + 1 frame starts.

await wait_physics_frames(10)

See Awaiting


Variant wait_idle_frames(x: int, msg = “”) 🔗

Alias for wait_process_frames


Variant wait_process_frames(x: int, msg = “”) 🔗

This returns a signal that is emitted after x process/idle frames have elpased. You can await this method directly to pause execution for x process/idle frames. The frames are counted prior to _process being called on any node (when SceneTree.process_frame is emitted). This means the signal is emitted after x frames and just before the x + 1 frame starts.

await wait_process_frames(10)
# wait_idle_frames is an alias of wait_process_frames
await wait_idle_frames(10)

See Awaiting


Variant wait_until(callable, max_time, p3 = “”, p4 = “”) 🔗

Use with await to wait for callable to return the boolean value true or a maximum amount of time. All values that are not the boolean value true are ignored. callable is called every _physics_process tick unless an optional time between calls is specified.

p3 can be the optional message or an amount of time to wait between calls.

p4 is the optional message if you have specified an amount of time to wait between calls.

Returns true if callable returned true before the timeout, false if not.

var foo = 1
func test_example():
    var foo_func = func():
        foo += 1
        return foo == 10
    foo = 1
    wait_until(foo_func, 5, 'optional message')
    # or give it a time between
    foo = 1
    wait_until(foo_func, 5, 1,
        'this will timeout because we call it every second and are waiting a max of 10 seconds')

See also wait_while

See Awaiting


Variant wait_while(callable, max_time, p3 = “”, p4 = “”) 🔗

This is the inverse of wait_until. This will continue to wait while callable returns the boolean value true. If ANY other value is is returned then the wait will end. Returns true if callable returned a value other than true before the timeout, false if not.

var foo = 1
func test_example():
    var foo_func = func():
        foo += 1
        if(foo < 10):
            return true
        else:
            return 'this is not a boolean'
    foo = 1
    wait_while(foo_func, 5, 'optional message')
    # or give it a time between
    foo = 1
    wait_while(foo_func, 5, 1,
        'this will timeout because we call it every second and are waiting a max of 10 seconds')

See Awaiting


Variant did_wait_timeout() 🔗

Returns whether the last wait_* method timed out. This is always true if the last method was wait_xxx_frames or wait_seconds. It will be false when using wait_for_signal and wait_until if the timeout occurs before what is being waited on. The wait_* methods return this value so you should be able to avoid calling this directly, but you can.


Variant get_summary() 🔗

Internal use only.


Variant get_fail_count() 🔗

Returns the number of failing asserts in this script at the time this method was called. Call in after_all to get total count for script.


Variant get_pass_count() 🔗

Returns the number of passing asserts in this script at the time this method was called. Call in after_all to get total count for script.


Variant get_pending_count() 🔗

Returns the number of pending tests in this script at the time this method was called. Call in after_all to get total count for script.


Variant get_assert_count() 🔗

Returns the total number of asserts this script has made as of the time of this was called. Call in after_all to get total count for script.


Variant get_summary_text() 🔗

Internal use only.


Variant double(thing, double_strat = null, not_used_anymore = null) 🔗

Create a Double of thing. thing should be a Class, script, or scene. See Doubles


Variant partial_double(thing, double_strat = null, not_used_anymore = null) 🔗

Create a Partial Double of thing. thing should be a Class, script, or scene. See Partial-Doubles


Variant double_singleton(singleton) 🔗

Creates a psuedo-double of an Engine Singleton. These doubles wrap around the singleton, and do not inherit from them. These doubles do not replace the Engine Singleton instance. You must use a local reference to the Engine Singleton that the double can be injected into.

class_name UsesTime
var t := Time
extends GutTest
func test_something():
    var dbl_time = partial_double_singleton(Time).new()
    var inst = UsesTime.new()
    inst.t = dbl_time

More information can be found at Doubling-Singletons


Variant partial_double_singleton(singleton) 🔗

This creates a partial double of a singleton, where all methods are intially stubbed to punch through to the Engine Singleton they wrap around. See double_singleton and Doubling-Singletons for more information.


void ignore_method_when_doubling(thing, method_name) 🔗

This was implemented to allow the doubling of classes with static methods. There might be other valid use cases for this method, but you should always try stubbing before using this method. Using stub(my_double, 'method').to_call_super() or creating a partial_double works for any other known scenario. You cannot stub or spy on methods passed to ignore_method_when_doubling.


Variant stub(thing, p2 = null, p3 = null) 🔗

Stub something. See Stubbing for detailed information about stubbing.


Variant autofree(thing) 🔗

Marks whatever is passed in to be freed after the test finishes. It also returns what is passed in so you can save a line of code. var thing = autofree(Thing.new())


Variant autoqfree(thing) 🔗

Works the same as autofree except queue_free will be called on the object instead. This also imparts a brief pause after the test finishes so that the queued object has time to free.


Variant add_child_autofree(node, legible_unique_name = false) 🔗

The same as autofree but it also adds the object as a child of the test.


Variant add_child_autoqfree(node, legible_unique_name = false) 🔗

The same as autoqfree but it also adds the object as a child of the test.


Variant yield_for(time, msg = “”) 🔗

Deprecated: use wait_seconds


Variant yield_to(obj, signal_name, max_wait, msg = “”) 🔗

Deprecated: use wait_for_signal


Variant yield_frames(frames, msg = “”) 🔗

Deprecated: use wait_frames


Variant double_scene(path, strategy = null) 🔗

Deprecated: no longer supported. Use double


Variant double_script(path, strategy = null) 🔗

Deprecated: no longer supported. Use double


Variant double_inner(path, subpath, strategy = null) 🔗

Deprecated: no longer supported. Use register_inner_classes + double


void assert_call_count(inst, method_name, expected_count, parameters = null) 🔗

Deprecated: Use assert_called_count instead.


void assert_setget(instance, name_property, const_or_setter = null, getter = “__not_set__”) 🔗

Deprecated: no longer supported.