Testing And Benchmarking in GDScript

Here’s the translation of the Go testing and benchmarking example to GDScript, formatted in Markdown suitable for Hugo:

Unit testing is an important part of writing principled GDScript programs. GDScript provides built-in tools for writing and running unit tests.

For the sake of demonstration, this code is in a standalone script, but it could be part of a larger project. Testing code typically lives in the same script or a separate test script for the code it tests.

extends SceneTree

# We'll be testing this simple implementation of an
# integer minimum. Typically, the code we're testing
# would be in a source file named something like
# `intutils.gd`, and the test file for it would then
# be named `test_intutils.gd`.

func int_min(a: int, b: int) -> int:
    if a < b:
        return a
    return b

# A test is created by writing a function with a name
# beginning with `test_`.

func test_int_min_basic():
    var ans = int_min(2, -2)
    assert(ans == -2, "int_min(2, -2) = %d; want -2" % ans)

# Writing tests can be repetitive, so it's common to use
# a data-driven style, where test inputs and expected
# outputs are listed in an array and a single loop
# walks over them and performs the test logic.

func test_int_min_table_driven():
    var tests = [
        {"a": 0, "b": 1, "want": 0},
        {"a": 1, "b": 0, "want": 0},
        {"a": 2, "b": -2, "want": -2},
        {"a": 0, "b": -1, "want": -1},
        {"a": -1, "b": 0, "want": -1},
    ]

    for test in tests:
        var ans = int_min(test.a, test.b)
        assert(ans == test.want, "got %d, want %d" % [ans, test.want])

# GDScript doesn't have built-in benchmarking tools like Go,
# but we can create a simple benchmarking function to measure
# performance.

func benchmark_int_min(iterations: int) -> float:
    var start_time = OS.get_ticks_msec()
    for i in range(iterations):
        int_min(1, 2)
    var end_time = OS.get_ticks_msec()
    return (end_time - start_time) / 1000.0

func _init():
    print("Running tests...")
    test_int_min_basic()
    test_int_min_table_driven()
    print("All tests passed!")

    print("Running benchmark...")
    var bench_time = benchmark_int_min(1000000)
    print("Benchmark completed in %.6f seconds" % bench_time)

    quit()

To run the tests and benchmark, save this script as test_int_min.gd and run it using the Godot command-line interface:

$ godot --script test_int_min.gd
Running tests...
All tests passed!
Running benchmark...
Benchmark completed in 0.123456 seconds

This example demonstrates how to write and run basic unit tests in GDScript. It includes a simple function to test, two test functions (one basic and one table-driven), and a basic benchmarking function.

Note that GDScript doesn’t have built-in testing or benchmarking frameworks like Go does. In a real Godot project, you might use a third-party testing framework or create more robust custom testing and benchmarking tools. The assert function is used here for simplicity, but in a larger project, you might want to create more detailed test reporting.

The benchmarking function is a simple implementation to measure execution time. In a real-world scenario, you might want to run the benchmark multiple times and calculate an average, or use more sophisticated profiling tools provided by the Godot engine.

Remember that in Godot, you would typically organize your code into scenes and scripts attached to nodes, rather than standalone scripts like this example. However, this structure works well for demonstrating testing concepts.