EasyEcs

Testing systems

Test EasyEcs systems with a world per test: arrange entities and singletons, call Update, assert on data. Shared setup, errors, allocation budgets, saves.

A system has no dependencies to mock: it reads and writes data in its world, and it never calls another system. So a test builds a small world of its own, adds the entities and singletons the system needs, calls world.Update() and checks the data the system left. The same holds for a whole tick: run the server's systems in the server's order and assert on the outcome.

When to use it

Use it for

Every system's rules (damage, expiry, death), the order of a whole tick, what a save keeps, and a budget of zero bytes allocated per tick. Tests run in milliseconds: a world is cheap to create and dispose.

Skip it for

Timing: a test that asserts how long a tick takes is flaky; measure with BenchmarkDotNet instead (performance). Testing EasyEcs itself (queries, structural changes): the library's own tests cover it, and your tests should state your game's rules.

How to use it

The examples use NUnit, as the dotnet new easyecs --tests template does; xUnit and MSTest work the same way. The components and systems are those of the full example: Health, Poison, the Dead tag, the Score singleton, PoisonSystem and DeathSystem.

Arrange, update, assert

One world per test, disposed at the end. Add only the system under test and the data it needs, run one update, then read components through the handles you kept:

[Test]
public void HurtsEveryTickThenWearsOff()
{
    using var world = new World();
    world.AddSystem<PoisonSystem>();
    var unit = world.CreateEntity(new Health(10), new Poison(DamagePerTick: 3, Ticks: 2));

    world.Update();
    Assert.That(unit.Health.Value, Is.EqualTo(7));
    Assert.That(unit.Poison.Ticks, Is.EqualTo(1));

    world.Update();
    Assert.That(unit.Health.Value, Is.EqualTo(4));
    Assert.That(unit.HasPoison, Is.False, "the last tick removes the poison");
}

Test the filters too: an entity the system must leave alone is one more CreateEntity and one more assertion. Every structural change applies at once, so what a system did is visible right after Update() returns: there is no command buffer to flush.

Share the setup with the server

Systems run in registration order. When the server and the tests each add the systems by hand, the tests can pass in an order the server never runs. Put the setup in one method that Program.cs and the tests both call:

public static class Battle
{
    public static void Setup(World world)
    {
        world.AddSingleton<Score>();
        world.AddSystem<PoisonSystem>();
        world.AddSystem<DeathSystem>();
    }
}

[Test]
public void APoisonedUnitDiesAndScores()
{
    using var world = new World();
    Battle.Setup(world);
    var unit = world.CreateEntity(new Health(5), new Poison(5, 3));

    world.Update();

    Assert.That(unit.IsDead, Is.True);
    Assert.That(world.Score.Kills, Is.EqualTo(1));
}

Control time and input with data

A system that reads DateTime.Now, a random generator or a socket cannot be tested exactly. Give it a singleton instead (a clock with a tick and a step, a seed, a config) and set that singleton in the test. Input is data too: a test creates the request entities a network system would have created, or writes into the queue the input system drains (hosting).

  • Intervals: a system with UpdateInterval => 4 runs on every fourth Update(); call Update() that many times.
  • Groups: systems in [UpdateInGroup<G>] run only on world.Update<G>(); call it in the test as the host would.
  • Change tracking: a [Changed<T>] method sees the writes made since its system last ran; write the component between two updates and assert on the second one (change tracking).

Test errors

By default an exception in a system propagates out of Update(), so a test asserts it with Assert.Throws. Under SystemErrorPolicy.Disable the system is switched off and keeps the exception in Error:

// Prints the score; it reads the singleton without adding it.
public class ScoreReport : EcsSystem
{
    protected override void OnUpdate() => Console.WriteLine($"kills: {World.Score.Kills}");
}

[Test]
public void AMissingSingletonThrowsOutOfUpdate()
{
    using var world = new World();
    world.AddSystem<ScoreReport>();

    Assert.Throws<InvalidOperationException>(() => world.Update());
}

[Test]
public void UnderDisableTheSystemIsSwitchedOff()
{
    using var world = new World(new WorldOptions { SystemErrors = SystemErrorPolicy.Disable });
    var report = new ScoreReport();
    world.AddSystem(report);

    world.Update();

    Assert.That(report.Enabled, Is.False);
    Assert.That(report.Error, Is.InstanceOf<InvalidOperationException>());
}

Hold a budget of zero bytes

A tick that allocates feeds the garbage collector, and pauses follow. Assert the budget: build the world, run one update to warm up the query caches and tables, then count the bytes the next updates allocate on this thread:

[Test]
public void ATickAllocatesNothing()
{
    using var world = new World();
    Battle.Setup(world);
    var units = new Entity[1_000];
    world.CreateEntities(units, new Health(1_000_000), new Poison(1, int.MaxValue));

    // The first update builds the query caches: measure the ones after it.
    world.Update();
    long before = GC.GetAllocatedBytesForCurrentThread();
    for (int i = 0; i < 100; i++)
    {
        world.Update();
    }

    long allocated = GC.GetAllocatedBytesForCurrentThread() - before;
    Assert.That(allocated, Is.Zero);
}

GC.GetAllocatedBytesForCurrentThread() counts this thread only: with ParallelSystems or parallel [ForEach] methods, use GC.GetTotalAllocatedBytes(precise: true) and keep other tests from running at the same time. A structural change into a table that does not exist yet allocates the table once, so warm up with the same kinds of changes the measured ticks make.

Test what a save keeps

Save the world, load the bytes into a new one and look for the state you care about. Entities keep their ids, so a handle found by its EntityId in the new world is the same entity:

[Test]
public void ABattleSurvivesARestart()
{
    using var world = new World();
    Battle.Setup(world);
    var unit = world.CreateEntity(new Health(5), new Poison(5, 3));
    world.Update();
    var output = new ArrayBufferWriter<byte>();
    world.Save(output);

    using var restarted = new World();
    Battle.Setup(restarted);
    // After Setup: Load replaces the singletons Setup added.
    restarted.Load(output.WrittenSpan);

    var loaded = restarted.GetEntity(unit.Id);
    Assert.That(loaded.IsDead, Is.True);
    Assert.That(restarted.Score.Kills, Is.EqualTo(1));
}

A test like this, run against a save file kept from the previous release, also tells you when a component change would make old saves fail to load (saves).

Full example

A poison that wears off, a death that scores, and five tests: two of one system alone, then one each for the whole tick, a zero-byte budget and a save. The game is a class library; the tests are an NUnit project that references it (dotnet new nunit, then dotnet add reference to the game and dotnet add package EasyEcs).

dotnet test --logger "console;verbosity=normal" prints:

  Passed ABattleSurvivesARestart [118 ms]
  Passed APoisonedUnitDiesAndScores [< 1 ms]
  Passed ATickAllocatesNothing [43 ms]
  Passed HurtsEveryTickThenWearsOff [2 ms]
  Passed LeavesTheDeadAlone [1 ms]
Total tests: 5
     Passed: 5

The times are mostly the JIT compiling each path once. Notice the budget test: a hundred ticks of a thousand poisoned units allocate nothing once the first tick has run.

How to use it fast

Instead ofDoWhy
A world shared by all testsA world per test, in usingTests stay independent and can run in parallel
Systems added by hand in each testThe server's own setup methodThe test runs the tick's real order
Waiting for real timeA clock singleton set by the testExact, and as fast as the code
Asserting how long a tick takesA zero-byte budget in the test, timings in BenchmarkDotNetAllocation counts are exact; times are not
Comparing whole worlds by handCounts (world.Count<T>()) and the components of kept handlesOne line per rule

What it costs

  • A world: one new World() and its Dispose(): the entity array and an empty table.
  • The analyzer: it also runs on the test project, so a test system with state or a race fails the build there too (analyzer rules).
  • Nothing in the game: tests use the public API only; there is no test mode to switch on.

Pitfalls

MistakeWhat happensCaught by
Forgetting a singleton the system reads (a [ForEach] parameter, World.X or GetSingleton<T>())Update() throws InvalidOperationException naming the singletonThe test fails: call the server's setup method, or set the singleton in the test
AddSingleton after Load in a save testThe loaded value is resetAdd singletons before Load
Measuring allocations without a warm-up updateThe query caches and new tables count as allocationsWarm up first
A ref held across world.Update()The update may move rows: the ref reads stale memoryRead through the handle after the update (refs)
Edit on GitHub

Last updated on

How is this guide?

On this page