agentleFS
Common-kaCommon-ka/ai-agent-unity-rules/.cursor/rules/unity-testing.mdc

ai-agent-unity-rules / rules

Cursor rule40 starsChanged 12 months agoMITView on GitHub

Unity Test Framework (Production Standards)

The file

We split this unity-testing.mdc into the rules it gives an agent

5 rules. About 0 are common, 2 are distinctive, and 3 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈2

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 3

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    When writing tests in Unity 6.2, adhere to the AAA (Arrange, Act, Assert) pattern and use the Constraint Model for assertions (Assert.That).

    line 8
  2. Very specific:

    Ensure your test script is located in a folder with an .asmdef (Assembly Definition) file that:

    line 11
  3. Distinctive:

    Use this for testing logic that is independent of the Engine Loop (e.g., Math, Data Parsing, State Machines).

    line 16
  4. Distinctive:

    Use this for testing GameObject interactions, Physics, and Coroutines.

    line 41
  5. Very specific:

    IMPORTANT: Always use [TearDown] to destroy created objects.

    line 42

≈ “Common” is an estimate: at least 1 in 5 of the 3,030 Cursor rule files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
unity-testing.mdc
---
description: "Unity Test Framework (Production Standards)"
alwaysApply: false
---

# Unity Test Framework Guidelines

When writing tests in Unity 6.2, adhere to the **AAA (Arrange, Act, Assert)** pattern and use the **Constraint Model** for assertions (`Assert.That`). This ensures readability and maintainability.

## 1. Assembly Definition Requirement
Ensure your test script is located in a folder with an `.asmdef` (Assembly Definition) file that:
1. References `UnityEngine.TestRunner` and `UnityEditor.TestRunner`.
2. References the Assembly Definition of your main project logic (Runtime code).

## 2. Edit Mode Test (Pure Logic)
Use this for testing logic that is independent of the Engine Loop (e.g., Math, Data Parsing, State Machines). These tests run very fast.

```
using NUnit.Framework;
// using MyProject.Systems; 

public class HealthSystemTests
{
    [Test]
    public void TakeDamage_DecreasesCurrentHealth()
    {
        // Arrange
        var healthSystem = new HealthSystem(maxHealth: 100);

        // Act
        healthSystem.TakeDamage(30);

        // Assert
        // Use Constraint Model for better readability
        Assert.That(healthSystem.CurrentHealth, Is.EqualTo(70));
    }
}
```

## 3. Play Mode Test (Integration & Physics)
Use this for testing GameObject interactions, Physics, and Coroutines.
**IMPORTANT:** Always use `[TearDown]` to destroy created objects. If you rely on `Object.Destroy` at the end of the test method and an assertion fails beforehand, the object will leak into the scene.

```
using NUnit.Framework;
using UnityEngine;
using UnityEngine.TestTools;
using System.Collections;

public class PlayerMovementTests
{
    // Standard Microsoft C# style: _camelCase for private fields
    private GameObject _player;
    private PlayerController _controller;

    [SetUp]
    public void Setup()
    {
        // Create an isolated environment for each test
        _player = new GameObject("TestPlayer");
        _controller = _player.AddComponent<PlayerController>();
    }

    [TearDown]
    public void Teardown()
    {
        // Guaranteed cleanup even if an assertion fails
        if (_player != null)
        {
            Object.Destroy(_player);
        }
    }

    [UnityTest]
    public IEnumerator Move_ForwardInput_ChangesPosition()
    {
        // Arrange
        var startPosition = _player.transform.position;
        
        // Act
        _controller.Move(Vector3.forward);
        
        // Wait for one frame (for Update logic)
        yield return null; 
        
        // OR wait for physics step if using Rigidbody
        // yield return new WaitForFixedUpdate(); 
        
        // Assert
        Assert.That(_player.transform.position.z, Is.GreaterThan(startPosition.z));
    }
}
```

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.