Testing Examples
Here are some arbitrary examples of GUT tests in a Godot project that illustrate the benefits of using automated testing:
Unit Testing
1. Testing Player Movement
This test ensures that the player moves correctly when a move function is called, showcasing error detection and regression prevention.
func test_player_moves_right():
var player = Player.new()
var initial_position = player.position
player.move_right()
assert_eq(player.position.x, initial_position.x + 1, "Player should move 1 unit to the right")
2. Testing Scene Loading
This test verifies that a scene is loaded properly, demonstrating the benefit of integration testing.
func test_main_menu_loads():
var main_menu_scene = load("res://scenes/MainMenu.tscn").instance()
add_child(main_menu_scene)
assert_not_null(main_menu_scene, "Main Menu scene should be loaded and not null")
3. Testing Health System
This test checks the player’s health decrement function, illustrating code quality and documentation benefits.
func test_health_decrements_correctly():
var player = Player.new()
var initial_health = player.health
player.take_damage(10)
assert_eq(player.health, initial_health - 10, "Health should decrement by 10")
4. Testing Game Over Condition
This test asserts that the game over condition triggers correctly, aiding in development efficiency.
func test_game_over_triggered_on_zero_health():
var player = Player.new()
player.health = 1
player.take_damage(1)
assert_true(player.is_game_over(), "Game over should be triggered when health reaches zero")
5. Testing Inventory Management
This test ensures that inventory management works as expected, which is crucial for team collaboration and maintaining code quality.
func test_add_item_to_inventory():
var inventory = Inventory.new()
var item = Item.new("Potion")
inventory.add_item(item)
assert_true(inventory.has_item("Potion"), "Inventory should contain the added item")
These examples demonstrate how GUT tests in Godot can help detect errors, improve code quality, support refactoring, prevent regressions, serve as documentation, enhance development efficiency, facilitate team collaboration, enable integration testing, integrate with CI/CD, and provide a fast feedback loop.
E2e (End to end) Testing
For Godot, E2E tests typically run within the Godot environment, executing scenarios that a player or user might experience when using the game or application. This includes navigating through menus, interacting with game objects, triggering events, and verifying the outcomes of these actions. The goal is to ensure that all parts of the application work together as expected in a real-world scenario.
In E2E testing with GUT, the test cases would automate actions like clicking buttons, moving characters, entering text, and then validate the responses of the application, such as changing scenes, updating the game state, or saving data. These tests can catch issues that might not be evident in unit or integration tests, such as problems with user flows, graphical rendering, input handling, and overall behavior of the game or application.
Some examples of e2e tests:
- Running through the entire Quest 1 and validating all steps between - Example:
src/Quests/Resources/Torion/001_quench_the_thirst.test.off.gd
- Starting the game and ensuring you can get to the main menu to play a new game.
- Going through a cutscene and can continue to the next part of the game.