Skip to content

How to write unit tests for scripts with @onready vars? #330

Answered by MikeSchulze
bugbountyguy asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @bugbountyguy i guess you have a scene with attached script you want to test.

The annotations are set when a node is added to the scenetree.
So you have two options.
a) load your scene and add it to the test-suite, with add_child() the _ready step is executed, and you should have a full instantiated model.
Please note that ALL dependencies of the scene are loaded here.
If you only need a part of the dependencies, you should rather mock the relevant components. See option b

func test_xxx() -> void:
	var scene = load("res://my_scene.tscn").instantiate()
	add_child(scene)
	
	assert_that(scene._player_model).is_not_null()

b) Load your script you want to test and set the _player_model manu…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@bugbountyguy
Comment options

Answer selected by bugbountyguy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
help wanted Extra attention is needed clarified
2 participants