Component Testing

Component tests run your components inside a real, headless engine scene: no window, no render device and no audio. You advance time frame by frame, script keyboard, pointer, touch or gamepad input, and assert on the result. The same tests run from the command line, from Studio and in CI.

Writing a test

Put test sources in a top-level Tests/ folder next to Assets/. They are compiled in a separate project, so they never end up in your game or in an export. New projects already include a small passing example in Tests/SceneTests.cs.

Mark a method with [SceneTest]. It must be a synchronous void method that takes no parameters or a single TestScene. It can be static, or an instance method of a class with a parameterless constructor (a fixture).

using System.Text.Json.Serialization;
using Silk.NET.Input;
using Turian.Engine.Core;
using Turian.Testing;

public sealed class Counter : Component
{
    [InjectService, JsonIgnore] public IInputSource Input { get; private set; } = null!;
    public int Presses { get; private set; }

    public override void OnUpdate(float deltaTime)
    {
        if (Input.WasKeyPressed(Key.Space)) { Presses++; }
    }
}

public static class CounterTests
{
    [SceneTest]
    public static void OnePressIsConsumedOnce(TestScene scene)
    {
        var counter = scene.Add<Counter>();
        scene.Input.PushKeyDown(Key.Space);
        scene.Step(3);
        SceneAssert.Equal(1, counter.Presses);
        SceneAssert.NotNull(scene.Find<Counter>());
    }
}

Every test gets a fresh scene and a fresh fixture. A fixture that implements IDisposable is disposed after the test, even when it fails. A test fails when an assertion fails, when a component throws during a frame, when fixture cleanup fails, or when its signature is invalid. async and Task-returning tests are rejected; advance asynchronous component work with frames instead.

TestScene

TestScene is a headless scene driven by the same frame loop as a running game, so components get their normal Awake, service injection, Start and OnUpdate callbacks.

Member What it does
Add<T>() Creates a node with component T and returns the component.
Find<T>() Returns the first T in the scene, or null.
Step(frames, deltaTime, beforeFrame) Simulates frames. The default frame duration is the fixed time step; beforeFrame receives the frame number and the input buffer so you can script input for every frame.
Input The input buffer. Push key, pointer, touch or gamepad events before a frame; edge events such as "pressed this frame" clear at the end of the frame, as in the real game.
Root, Ticker, Services The scene hierarchy, the production frame driver with its clock and scheduler, and the scene's services.

new TestScene(services => ...) registers extra services or subsystems your components need. Application, platform, rendering and audio modules are never discovered, so register only what the test uses.

Assertions live in SceneAssert. The Turian.Testing library can also be referenced by an ordinary .NET test project.

Tests run one after another. Static state in your own code is shared across the run, so reset it in the test. Cancelling a run stops a build or prevents the next test from starting; a test body has to return on its own.

Running from the command line

turian-cli test MyProject
turian-cli test MyProject --list
turian-cli test MyProject --filter CounterTests --results artifacts/component-tests.xml
Option What it does
--filter <text> Runs tests whose fully qualified method name contains the text, ignoring case.
--list Lists the tests without running them or their fixtures.
--results <file> Writes the JUnit XML report there. The default is <project>/TestResults/results.xml.

A failing test, a compilation error or a run that finds no tests exits with status 1, so CI fails. Failures print the source file and line, and the JUnit report includes the message, stack trace and source location. Running tests does not import assets or start the project's startup scene. See CLI for the other commands.

Running from Studio

Open Project → Test Runner. Discover builds the tests and lists them without running anything; Run executes the tests that match the filter and shows an outcome for each one. Select a failure to see its message, the surrounding source lines with the failing line highlighted, and the stack trace. Open Source opens the file in your associated editor. Stop Play Mode or the standalone player before running tests. Builds and runs show up in Studio's background tasks, where they can be cancelled.

Running in CI

New projects include .github/workflows/component-tests.yml.example. Commit your project's brick lock, pin the container image to a Turian release that includes the test command, then rename the file to .yml to enable it. The workflow restores locked bricks, runs the tests and uploads the JUnit XML even when tests fail. Commit Tests/, and ignore .Cache/ and TestResults/.

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

← All docs Edit this page