Input and Controller Testing for Games: Handling Every Device Correctly

Input and Controller Testing for Games: Handling Every Device Correctly

Input handling is where most cross-platform bugs live. A game that works perfectly with keyboard and mouse might silently drop inputs on a gamepad, fail with certain controller layouts, or have dead zones so aggressive that analog movement feels like a digital stick. These bugs are hard to catch manually because most developers test with their own setup.

Automated input testing is underutilized in game development. This post covers how to test input systems — Unity's new Input System, custom input abstractions, controller dead zones, rebinding, and accessibility inputs.

Abstracting Input for Testability

The first step to testable input is abstracting the hardware layer behind an interface:

// Bad: directly reads hardware input — untestable
public class PlayerController : MonoBehaviour
{
    void Update()
    {
        if (Input.GetKey(KeyCode.Space))
            Jump();
    }
}

// Good: injectable input provider
public interface IInputProvider
{
    Vector2 GetMoveInput();
    bool GetJumpDown();
    bool GetAttackDown();
    bool GetInteractDown();
}

public class HardwareInputProvider : IInputProvider
{
    private PlayerInput _playerInput;
    
    public HardwareInputProvider(PlayerInput input) => _playerInput = input;
    
    public Vector2 GetMoveInput() => 
        _playerInput.actions["Move"].ReadValue<Vector2>();
    public bool GetJumpDown() => 
        _playerInput.actions["Jump"].WasPressedThisFrame();
    public bool GetAttackDown() => 
        _playerInput.actions["Attack"].WasPressedThisFrame();
    public bool GetInteractDown() => 
        _playerInput.actions["Interact"].WasPressedThisFrame();
}

public class TestInputProvider : IInputProvider
{
    public Vector2 MoveInput;
    public bool JumpPressed;
    public bool AttackPressed;
    public bool InteractPressed;
    
    public Vector2 GetMoveInput() => MoveInput;
    public bool GetJumpDown() => JumpPressed;
    public bool GetAttackDown() => AttackPressed;
    public bool GetInteractDown() => InteractPressed;
    
    public void SimulateMove(Vector2 dir) => MoveInput = dir;
    public void SimulateJump() { JumpPressed = true; }
    public void ReleasJump() { JumpPressed = false; }
}

With this pattern, tests inject TestInputProvider and drive the player without hardware:

[TestFixture]
public class PlayerMovementInputTests
{
    private TestInputProvider _input;
    private PlayerController _player;

    [SetUp]
    public void SetUp()
    {
        _input = new TestInputProvider();
        _player = new PlayerController(_input);
    }

    [Test]
    public void FullRightInput_MovesPlayerRight()
    {
        _input.SimulateMove(Vector2.right);
        _player.Update(deltaTime: 0.016f);
        
        Assert.Greater(_player.Velocity.x, 0f);
    }

    [Test]
    public void ZeroInput_PlayerDecelerates()
    {
        // Give player momentum first
        _input.SimulateMove(Vector2.right);
        _player.Update(deltaTime: 0.5f);
        float initialVelocity = _player.Velocity.x;
        
        // Release input
        _input.SimulateMove(Vector2.zero);
        _player.Update(deltaTime: 0.1f);
        
        Assert.Less(_player.Velocity.x, initialVelocity,
            "Player should decelerate when no input");
    }
}

Unity Input System: Action-Based Testing

Unity's new Input System can be tested with simulated devices:

using UnityEngine;
using UnityEngine.InputSystem;
using NUnit.Framework;

[TestFixture]
public class InputSystemTests
{
    private Keyboard _keyboard;
    private Gamepad _gamepad;
    private InputTestFixture _inputFixture;

    [SetUp]
    public void SetUp()
    {
        _inputFixture = new InputTestFixture();
        _keyboard = InputSystem.AddDevice<Keyboard>();
        _gamepad = InputSystem.AddDevice<Gamepad>();
    }

    [Test]
    public void KeyboardJump_TriggersJumpAction()
    {
        var actions = new PlayerInputActions();
        bool jumpFired = false;
        actions.Player.Jump.performed += _ => jumpFired = true;
        actions.Enable();
        
        // Simulate spacebar press
        _inputFixture.Press(_keyboard.spaceKey);
        
        InputSystem.Update();
        
        Assert.IsTrue(jumpFired, "Jump action should fire on spacebar");
        
        actions.Disable();
    }

    [Test]
    public void GamepadAnalogStick_ProducesCorrectMoveVector()
    {
        var actions = new PlayerInputActions();
        Vector2 lastMove = Vector2.zero;
        actions.Player.Move.performed += ctx => lastMove = ctx.ReadValue<Vector2>();
        actions.Enable();
        
        // Simulate gamepad left stick at 45 degrees
        _inputFixture.Set(_gamepad.leftStick, new Vector2(0.707f, 0.707f));
        InputSystem.Update();
        
        Assert.AreApproximatelyEqual(0.707f, lastMove.x, 0.01f);
        Assert.AreApproximatelyEqual(0.707f, lastMove.y, 0.01f);
        
        actions.Disable();
    }

    [TearDown]
    public void TearDown()
    {
        InputSystem.RemoveDevice(_keyboard);
        InputSystem.RemoveDevice(_gamepad);
    }
}

Dead Zone Testing

Dead zones define the minimum stick input that registers. Too small and stick drift causes phantom movement; too large and the response feels sluggish.

[TestFixture]
public class DeadZoneTests
{
    private TestInputProvider _input;
    private PlayerController _player;
    
    private const float DeadZone = 0.15f;  // expected dead zone

    [Test]
    [TestCase(0.05f, 0f)]   // below dead zone — should not move
    [TestCase(0.10f, 0f)]   // below dead zone
    [TestCase(0.14f, 0f)]   // just below dead zone
    [TestCase(0.16f, 1f)]   // just above dead zone — should move
    [TestCase(0.50f, 1f)]   // mid-stick — should move
    [TestCase(1.00f, 1f)]   // full stick — should move
    public void AnalogInput_DeadZoneAppliedCorrectly(float inputMagnitude, float expectsMovement)
    {
        _input = new TestInputProvider();
        _player = new PlayerController(_input);
        
        _input.SimulateMove(new Vector2(inputMagnitude, 0f));
        _player.Update(0.016f);
        
        if (expectsMovement > 0f)
            Assert.Greater(_player.Velocity.magnitude, 0f,
                $"Input {inputMagnitude} above dead zone should produce movement");
        else
            Assert.AreApproximatelyEqual(0f, _player.Velocity.magnitude, 0.001f,
                $"Input {inputMagnitude} below dead zone should produce no movement");
    }

    [Test]
    public void AnalogInput_ResponseIsLinearAboveDeadZone()
    {
        // Verify no sudden jump at dead zone boundary
        float speed05 = GetSpeedForInput(0.5f);
        float speed07 = GetSpeedForInput(0.7f);
        float speed10 = GetSpeedForInput(1.0f);
        
        Assert.Less(speed05, speed07, "Speed should increase with input");
        Assert.Less(speed07, speed10, "Speed should increase with input");
    }

    private float GetSpeedForInput(float magnitude)
    {
        _input = new TestInputProvider();
        _player = new PlayerController(_input);
        _input.SimulateMove(new Vector2(magnitude, 0f));
        _player.Update(0.016f);
        return _player.Velocity.magnitude;
    }
}

Input Rebinding Tests

Custom key binding is a quality-of-life feature that's easy to break:

[TestFixture]
public class InputRebindingTests
{
    [Test]
    public void RebindJump_NewKeyTriggerJump()
    {
        var rebindService = new InputRebindService();
        bool jumpFired = false;
        
        // Rebind jump from Space to Z
        rebindService.RebindAction("Jump", "<Keyboard>/z");
        
        var player = CreatePlayerWithMockInput(rebindService);
        player.OnJump += () => jumpFired = true;
        
        // Simulate Z key press
        player.HandleInput("<Keyboard>/z", pressed: true);
        
        Assert.IsTrue(jumpFired, "Jump should fire on rebound key Z");
    }

    [Test]
    public void RebindJump_OldKeyNoLongerTriggersJump()
    {
        var rebindService = new InputRebindService();
        bool jumpFired = false;
        
        rebindService.RebindAction("Jump", "<Keyboard>/z");
        
        var player = CreatePlayerWithMockInput(rebindService);
        player.OnJump += () => jumpFired = true;
        
        // Old key (Space) should no longer work
        player.HandleInput("<Keyboard>/space", pressed: true);
        
        Assert.IsFalse(jumpFired, "Jump should NOT fire on old key after rebind");
    }

    [Test]
    public void RebindConflict_SwapsBindings()
    {
        var rebindService = new InputRebindService();
        
        // Attack is bound to Z initially; rebind Jump to Z
        rebindService.RebindAction("Jump", "<Keyboard>/z");
        
        // Attack should have been moved away from Z
        string attackBinding = rebindService.GetBinding("Attack");
        Assert.AreNotEqual("<Keyboard>/z", attackBinding,
            "Attack should not share binding with Jump");
    }

    [Test]
    public void SaveAndLoad_BindingsPersistAcrossSessions()
    {
        var rebindService = new InputRebindService();
        rebindService.RebindAction("Jump", "<Keyboard>/x");
        rebindService.Save();
        
        // Create fresh service and load
        var newService = new InputRebindService();
        newService.Load();
        
        Assert.AreEqual("<Keyboard>/x", newService.GetBinding("Jump"),
            "Rebound key should persist after save/load");
    }
}

Touch and Accessibility Input

[TestFixture]
public class TouchInputTests
{
    [Test]
    public void TouchJoystick_ProducesCorrectDirection()
    {
        var joystick = new VirtualJoystick(center: new Vector2(100f, 100f), radius: 50f);
        
        // Simulate touch drag to the right
        joystick.OnTouchMove(new Vector2(150f, 100f));  // 50 units right = full right
        
        var output = joystick.GetInput();
        
        Assert.AreApproximatelyEqual(1f, output.x, 0.01f, 
            "Full right touch should produce (1, 0)");
        Assert.AreApproximatelyEqual(0f, output.y, 0.01f);
    }

    [Test]
    public void TouchJoystick_ClampsMagnitudeTo1()
    {
        var joystick = new VirtualJoystick(center: new Vector2(100f, 100f), radius: 50f);
        
        // Drag far beyond radius
        joystick.OnTouchMove(new Vector2(300f, 100f));
        
        var output = joystick.GetInput();
        
        Assert.LessOrEqual(output.magnitude, 1.001f,
            "Joystick output magnitude should never exceed 1");
    }
}

[TestFixture]
public class AccessibilityInputTests
{
    [Test]
    public void AutoAim_SnapsToNearestEnemy()
    {
        var player = CreateTestPlayer(position: Vector3.zero);
        var enemy1 = CreateTestEnemy(position: new Vector3(5f, 0f, 0f));
        var enemy2 = CreateTestEnemy(position: new Vector3(10f, 0f, 0f));
        
        var aimAssist = new AimAssistSystem();
        aimAssist.SnapAngle = 30f;
        
        // Aim roughly toward enemy1 (within snap angle)
        var aimDirection = new Vector3(5.2f, 0f, 1f).normalized;
        var corrected = aimAssist.ApplyAssist(player.transform, aimDirection, 
            new[] { enemy1, enemy2 });
        
        // Should snap to enemy1
        var toEnemy1 = (enemy1.transform.position - player.transform.position).normalized;
        Assert.AreApproximatelyEqual(toEnemy1.x, corrected.x, 0.1f,
            "Aim assist should snap to closest enemy");
    }
}

Cross-Platform Input Validation

Validate that input actions work correctly across all target platforms:

[TestFixture]
public class CrossPlatformInputTests
{
    [Test]
    [TestCase("Keyboard", "<Keyboard>/space")]
    [TestCase("Gamepad", "<Gamepad>/buttonSouth")]
    [TestCase("Gamepad (PS4)", "<Gamepad>/buttonSouth")]
    public void JumpBinding_ExistsForDevice(string deviceType, string expectedBinding)
    {
        var actions = new PlayerInputActions();
        var jumpAction = actions.Player.Jump;
        
        bool bindingFound = false;
        foreach (var binding in jumpAction.bindings)
        {
            if (binding.path == expectedBinding || 
                InputSystem.MatchesPrefix(deviceType, binding.path))
            {
                bindingFound = true;
                break;
            }
        }
        
        Assert.IsTrue(bindingFound, 
            $"Jump action missing binding for {deviceType}");
    }
}

Good input testing means your game feels correct on every device your players use. The abstraction layer costs nothing at runtime and makes input bugs visible in CI instead of in player reviews.

Read more

Start now free