Skip to content

Instantly share code, notes, and snippets.

@WillSams
Last active June 12, 2024 12:36
Show Gist options
  • Save WillSams/6535581dc8074f991673b438b3e526c8 to your computer and use it in GitHub Desktop.
Save WillSams/6535581dc8074f991673b438b3e526c8 to your computer and use it in GitHub Desktop.
Godot Testing Example
rm -rf * .godot
wget -O project.godot \
https://gist.githubusercontent.com/WillSams/a6e3b853595ea6123b5a506acc0ba0a3/raw/f2fab5899029594d66bcba18a2b62cc62927fe81/generic_godot.settings
# download the textures
mkdir {scenes,textures}
wget -O textures/icon.png \
https://github.com/godotengine/godot/blob/master/icon.png?raw=true
wget -O textures/light.png \
https://clipground.com/images/white-light-png-9.png
mkdir -p {scenes,specs,textures}/entities
wget https://github.com/godotengine/godot-docs-project-starters/releases/download/latest-4.x/2d_sprite_animation_assets.zip
unzip -j 2d_sprite_animation_assets.zip '**/run_animation/*' -d textures/entities/player
rm 2d_sprite_animation_assets.zip
wget -O specs/asserts.gd \
https://gist.githubusercontent.com/WillSams/9e0ea2f83f184175412bc6fdfe8238e5/raw/b6d52d33e06c7f7293aee17a60d0fe347dc2c676/asserts.gd
echo "Create a scene...."
echo "echo "extends CharacterBody2D
@export var animated_sprite: AnimatedSprite2D
var animations: SpriteFrames = SpriteFrames.new()
func load_image_texture(img: String) -> ImageTexture:
var texture_path = 'res://textures/%s.png' % img
var image = Image.new()
var err = image.load(texture_path)
if err != OK:
var Errors = Engine.get_singleton('Errors')
push_error('Failed to load texture %s' % [texture_path, Errors.get_message(err)])
return null
return ImageTexture.create_from_image(image)
func _enter_tree():
const duration = 1.0
animations.add_animation('run')
var images = [
'entities/player/run-1',
'entities/player/run-2',
'entities/player/run-3',
'entities/player/run-4',
'entities/player/run-5',
'entities/player/run-6',
'entities/player/run-7',
'entities/player/run-8'
]
for img in images:
animations.add_frame('run', load_image_texture(img), duration)
if not is_instance_valid(animated_sprite):
push_error('AnimatedSprite2D is not a valid instance')
return
animated_sprite.sprite_frames = animations
animated_sprite.autoplay = 'run'
animated_sprite.speed_scale = 2
func _ready():
if not is_instance_valid(animated_sprite):
push_error('AnimatedSprite2D is not a valid instance')
return
animated_sprite.position = Vector2(433, 274)
func _process(_delta):
if not is_instance_valid(animated_sprite):
push_error('AnimatedSprite2D is not a valid instance')
return
if Input.is_action_pressed('right'): animated_sprite.play('run')
else: animated_sprite.stop()" >| scenes/main.gd
godot4 scenes/main.tscn
echo "Testing example below..."
echo "var MainScene = load('res://scenes/main.gd').new()
var _asserts = load('res://specs/asserts.gd').new()
func load_image_texture_should_not_return_null_for_valid_image():
if MainScene:
var image: ImageTexture = MainScene.load_image_texture('entities/player/run-1')
if image:
_asserts.assert_not_null(image)
MainScene.free()
func cleanup():
if MainScene != null:
MainScene.queue_free()
MainScene = null
func run():
if MainScene:
load_image_texture_should_not_return_null_for_valid_image()
cleanup()
else: print('MainScene is not a valid instance')" >| specs/main.spec.gd
echo "extends SceneTree
var _main_spec = load('res://specs/main.spec.gd').new()
func _init():
_main_spec.run()
quit()" >| specs/runner.gd
godot4 --headless -s specs/runner.gd # make fail, then make pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment