Skip to content

Unit tests

Mex777 edited this page Jun 4, 2024 · 1 revision

Unit Testing with GUT in Godot

Introduction

Unit testing is essential for ensuring the quality and reliability of your game code. In Godot, you can use the GUT (Godot Unit Test) framework to write and execute unit tests directly within the engine. GUT allows you to create test scripts for your GDScript code, making it easier to catch bugs and verify functionality.

Test Coverage

Our game project boasts an impressive suite of over 40 unit tests. These tests cover various aspects of our codebase, ensuring that critical functionality works as expected. From player interactions to backend systems, our comprehensive test suite helps catch bugs early and maintain code quality.

Setting Up GUT

  1. Install GUT: You can install GUT via the Asset Library in Godot. Search for "GUT" and install the plugin.
  2. Folder Structure: Organize your tests by creating a Tests folder in your project. Inside this folder, create separate GDScript files for different test cases. For example:
    • TestPlayerSingleton.gd
    • TestAuroraCharacter.gd

Writing Tests

  1. Test Scripts: In each test script, define test methods that check specific functionality. Use GUT's plethora of asserts and utility methods to keep your tests concise and effective.
  2. Inner Test Classes: For better context and maintainability, consider using inner test classes. These allow you to group related test methods together within a single script.
  3. Mocks for Easy Testing: When testing classes that depend on other classes (e.g., singletons or services), create mock versions of those dependencies. This ensures that your tests focus on the specific code you're testing.

Running Tests

  1. Godot Editor: You can run your tests directly from the Godot Editor. Look for the GUT menu options.
  2. Command Line: Use the Command Line Interface (CLI) to execute your tests programmatically.
  3. VSCode Integration: If you're using Visual Studio Code, install the GUT extension. It allows you to run tests directly from the editor.

Example Test

Here's a simple example of a test script:

extends GutTest

func test_take_damage_below_0():
	Player.set_hp(50)
	Player.take_damage(100)
	assert_eq(Player.get_hp(), 0, "take_damage should not reduce hp below 0")