GdUnit4 Documentation
repository·master·Indexed 22 days ago
https://github.com/godot-gdunit-labs/gdunit4An embedded unit testing framework for Godot 4.x supporting GDScript and C#. It provides an integrated experience within the Godot editor, including a test inspector, automated test generation, and support for CI/CD pipelines. Key features include fuzzing, parameterized tests, mocking, spying, and a scene runner for simulating user interactions. C# support is provided via GdUnit4Net with VSTest integration for Visual Studio, VS Code, and JetBrains Rider.
What's inside GdUnit4
- GdUnit4 is an embedded unit testing framework for Godot 4.x. It allows developers to write and execute tests for GDScript, C# scripts, and Godot scenes directly within the Godot editor. It is designed to support Test-Driven Development (TDD) by providing tools to verify code functionality and performance.
Overview of GdUnit4 Advanced Testing techniques
masterGdUnit4 provides several advanced testing techniques to help write comprehensive and effective tests for Godot games. These techniques allow you to move beyond basic assertions into complex scenarios like signal monitoring, automated fuzzing, and dependency isolation.
Prerequisites
- A basic understanding of the Godot engine and GDScript.
- GdUnit4 must be installed and configured in your Godot project.
Core Testing Features in GdUnit4
masterGdUnit4 provides several integrated features for the Godot editor:
- GDScript and C# Support: Write and execute tests in both languages.
- Embedded Test Inspector: Navigate test suites directly within the Godot editor.
- Test Discovery: Automatically finds tests at runtime and populates the inspector.
- Convenient Interface: Run test suites via context menus in the FileSystem panel, ScriptEditor, or GdUnit Inspector.
- Automated Test Generation: Right-click any function in the ScriptEditor and select "Create TestCase" to generate a test automatically.
GdUnit4Net - C# Support
masterFor C# users, GdUnit4 provides specialized support through GdUnit4Net:
- C# API: Full support for writing tests in C#.
- VSTest Integration: Run and debug tests in Visual Studio, Visual Studio Code, or JetBrains Rider using the
gdunit4.test.adapter.
Use the GdUnit Test Inspector to manage and review tests
masterThe GdUnit Test Inspector is the central interface for running tests and reviewing results within Godot. It consists of four main areas:
- Button Bar: Controls for triggering test runs (Runtime, Debug, or Overall).
- Status Bar: Provides real-time execution progress, elapsed time, and counts for errors, failures, flaky tests, skipped tests, and orphan nodes.
- Test Run Overview Tree: A real-time hierarchical or flat list of all executed tests showing their status and execution time.
- Failure Report: Detailed breakdown of assertion mismatches (expected vs. current) and the full clickable call stack for debugging.
What is Test Driven Development (TDD) with GdUnit4
masterTest Driven Development (TDD) is a methodology where tests are written before the actual implementation code. Using GdUnit4 for TDD follows a specific cycle:
- Write a failing test: Define the expected behavior of a component using GdUnit4 test suites.
- Write minimum code: Implement only the amount of code necessary to make the test pass.
- Refactor: Improve the code design while ensuring the tests continue to pass.
By using GdUnit4 to drive development, you ensure that your Godot code meets requirements and remains reliable through automated verification.
What is the Scene Runner and how to use it
masterThe Scene Runner is a tool for integration testing by simulating interactions on a scene, such as keyboard/mouse input and scene processing over a specific number of frames.
Key constraints:
- Managed by the GdUnit API and automatically freed after use.
- One Scene Runner can only manage one scene. To test multiple scenes, create a separate runner for each.
Initialization:
- In GDScript: Use
scene_runner("<scene_path>"). - In C#: Use
ISceneRunner.Load("<scene_path>").
# GDScript var runner := scene_runner("res://my_scene.tscn")// C# ISceneRunner runner = ISceneRunner.Load("res://my_scene.tscn");What is integration testing in GdUnit4?
masterIntegration testing in GdUnit4 focuses on verifying the interaction between multiple components, modules, or scenes within your game, rather than testing individual components in isolation (unit testing). It is used to identify issues such as:
- Interaction Bugs: Conflicts between different input actions (e.g., keyboard vs. mouse).
- Scene Transitions: Ensuring smooth transitions between different scenes or UI states.
- Gameplay Logic: Verifying mechanics when multiple elements interact (e.g., player actions vs. enemy behavior).
By using integration tests, you can achieve early bug detection and improved stability when updating individual parts of your game.
What is Unit Testing in GdUnit4
masterUnit testing is the practice of testing individual components or 'units' of your code in isolation to ensure they work as expected. In the context of Godot development with GdUnit4, a unit typically refers to a single function, method, or class.
Key Characteristics:
- Isolation: Tests target specific code independently of the rest of the system, making it easier to pinpoint the source of an issue.
- Automation: Tests can be run frequently and consistently to catch regressions.
- Speed: Tests are small and focused on a single piece of functionality (e.g., movement logic or score calculation), allowing for rapid execution.
What is a GdUnit TestSuite?
masterA
TestSuiteis a collection of tests aligned to a specific class or module. It is used to manage test data and environment consistency by providing hooks for setup and teardown.Key capabilities include:
- Grouping: Organizing multiple test cases related to a single unit of code.
- Lifecycle Management: Defining pre-initialized test data and cleanup routines.
- Granular Hooks: Executing logic at different levels of the test lifecycle (before/after each test case, and before/after the entire suite).
What is a Spy and how does it differ from a Mock?
masterA Spy is used to verify behavior during a test by tracking all function calls and their parameters on an instance.
Unlike a Mock, which replaces the implementation, a Spy calls the real implementation. It behaves exactly like the normal instance while recording its interactions for later verification.
Note: This implementation is only available for GDScript. For C#, use existing frameworks like Moq.
How GdUnit compares objects and references
masterGdUnit distinguishes between parameter equality (value comparison) and reference equality (instance comparison).
- Parameter Equality: Two different instances are considered equal if they are of the same type and have the same parameter values. Use
is_equalandis_not_equalviaassert_object. - Reference Equality: Checks if two variables point to the exact same memory instance. Use
is_sameandis_not_sameviaassert_object, or specialized functions likecontains_sameornot_contains_samefor collections.
# Parameter equality (different instances, same values) assert_object(obj1).is_equal(obj2) # Reference equality (same instance) assert_object(obj1).is_same(obj2)- Parameter Equality: Two different instances are considered equal if they are of the same type and have the same parameter values. Use