← Tutorials

Test and CI

Tests catch the bug you would otherwise find after the fifth playtest. This tutorial takes about fifteen minutes. You need Turian and the .NET SDK installed, and you should have finished the getting-started tutorial.

A component test runs your component in a headless scene: no window, no GPU and no audio. The test advances time one frame at a time, presses keys on schedule and checks the result. You will test a spinning component that turns only while Space is held, run the tests from the command line, read the report a CI server understands and run the same tests in CI.

1. Create a project and run the sample test

turian-cli new HelloTests
cd HelloTests
turian-cli test .

A new project includes Tests/SceneTests.cs, a sample test that passes. The output has a result line for each test, followed by the line of the test's source file that it ran:

Passed: SceneTests.StartsWithAnEmptyScene
  Tests/SceneTests.cs(7):

The Tests/ folder sits next to Assets/. Its files are compiled in their own project, so tests never end up in your game or in an export.

2. Write the component

Create Assets/Spinner.cs. It is the same idea as the spinner in the getting-started tutorial, but it reads the keyboard through the injected IInputSource service, which the test can drive:

using System.Text.Json.Serialization;

namespace Usercode;

/// <summary>Turns its node around the Y axis while Space is held.</summary>
public class Spinner : Component
{
    /// <summary>Degrees per second.</summary>
    public float speed = 90f;

    [InjectService, JsonIgnore]
    public IInputSource Input { get; private set; } = null!;

    public override void OnUpdate(float deltaTime)
    {
        if (!Input.IsKeyDown(Key.Space)) return;
        var node = Node!;
        var rotation = node.Rotation;
        node.Rotation = new Vector3(rotation.X, rotation.Y + speed * deltaTime, rotation.Z);
    }
}

[InjectService] fills the property before the component's callbacks run, and JsonIgnore keeps it out of scene files. Check that the project compiles:

turian-cli compile .

3. Write the test

Create Tests/SpinnerTests.cs:

using Silk.NET.Input;
using Turian.Testing;
using Usercode;

public static class SpinnerTests
{
    [SceneTest]
    public static void SpinsWhileSpaceIsHeld(TestScene scene)
    {
        var spinner = scene.Add<Spinner>();

        // Space goes down before frame 0 and up before frame 2, so frames 0 and 1 turn it: 2 x 90 x 0.1 = 18 degrees.
        scene.Step(4, 0.1, (frame, input) =>
        {
            if (frame == 0) input.PushKeyDown(Key.Space);
            if (frame == 2) input.PushKeyUp(Key.Space);
        });

        var turned = spinner.Node!.Rotation.Y;
        SceneAssert.True(Math.Abs(turned - 18f) < 0.01f,
            $"Expected 18 degrees after two held frames; received {turned}.");
    }

    [SceneTest]
    public static void StaysStillWithoutInput(TestScene scene)
    {
        var spinner = scene.Add<Spinner>();
        scene.Step(10);
        SceneAssert.True(Math.Abs(spinner.Node!.Rotation.Y) < 0.01f, "Expected no rotation without input.");
    }
}

Some things to notice:

  • A test is a public static void method marked [SceneTest] that takes one TestScene parameter. Each test gets a fresh scene.
  • scene.Add<Spinner>() creates a node with the component, and the component gets its normal injection and Start callbacks.
  • Step runs frames. Its beforeFrame callback receives the frame number and the input buffer, so you script key presses per frame. A key stays down until you push its release, just as on a real keyboard.
  • SceneAssert.True fails the test with your message. The test project does not get the project's Globals.cs, so the file names its own namespaces, such as Silk.NET.Input for Key.

4. Run the tests

turian-cli test . --list
turian-cli test . --filter SpinnerTests

--list prints each test as NotRun: without running it. --filter runs the tests whose full name contains the text, ignoring case. The filtered run prints two Passed lines.

Now make a test fail. Change 18f in SpinnerTests.cs to 20f and run again:

turian-cli test . --filter SpinnerTests
echo $?

The failing test is reported with its message and the line of the assertion, and the command exits with status 1. A compiler error or a run that finds no tests also exits with 1, so a CI job fails on any of them. Change the value back to 18f before you continue.

Studio runs the same tests. Open Project → Test Runner, click Discover to list them and Run to execute them. Select a failure to see its message, the source lines around it and the stack trace. Stop Play Mode before you run tests.

5. Read the JUnit report

Every run writes a JUnit XML report. By default it goes to TestResults/results.xml in the project; --results chooses another file:

turian-cli test . --results TestResults/results.xml

The report has one testsuite with a testcase for each test. A failed test contains a failure element with the message and the stack trace, and each test records its source file and line. The shape looks like this, with the values trimmed:

<testsuites>
  <testsuite name="Turian component tests" tests="2" failures="1" skipped="0" time="...">
    <testcase name="SpinnerTests.SpinsWhileSpaceIsHeld" classname="SpinnerTests" time="..." file="..." line="...">
      <failure message="Expected 18 degrees after two held frames; received ...">...</failure>
    </testcase>
    <testcase name="SpinnerTests.StaysStillWithoutInput" classname="SpinnerTests" time="..." file="..." line="..."/>
  </testsuite>
</testsuites>

classname is the test's name without its method, which is the grouping most CI viewers show. Because the file uses the JUnit format, CI report viewers can read it directly, so you can upload it as a test report.

6. Run the tests in CI

New projects include .github/workflows/component-tests.yml.example. Commit the project's brick lock, pin the container image to a Turian release that includes the test command, then rename the file to .yml:

name: Component tests
on: [push, pull_request]
jobs:
  test:
    runs-on: ubuntu-latest
    container: ghcr.io/mass4org/turian-cli:latest
    steps:
      - uses: actions/checkout@v7
      - run: turian-cli brick restore --locked --project .
      - run: turian-cli test . --results TestResults/results.xml
      - uses: actions/upload-artifact@v7
        if: always()
        with:
          name: component-test-results
          path: TestResults/*.xml

The job restores the bricks exactly as the lock records them, runs the tests and uploads the XML even when tests fail, so you can read the failure. Commit Tests/, and ignore .Cache/ and TestResults/.

On GitLab the same steps fit in one job. The job caches bricks the way the bricks page describes. Set TURIAN_CLI_IMAGE to a CLI image that includes the test command:

component-tests:
  image:
    name: $TURIAN_CLI_IMAGE
    entrypoint: [""]
  variables:
    GAYA_BRICKS: $CI_PROJECT_DIR/.bricks
  cache:
    key:
      files: [Bricks/packages-lock.json]
    paths: [.bricks/]
  script:
    - turian-cli brick restore --locked --project .
    - turian-cli test . --results TestResults/results.xml
  artifacts:
    when: always
    reports:
      junit: TestResults/results.xml

Next steps

  • Component testing — every TestScene member, the test rules and the Studio runner in reference form.
  • CLI — the other commands for building, inspecting and exporting a project.
  • Your first brick — put shared components in a brick that your tests and your game can both install.

Edit this page