Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question I cannot make Unity test-framework work with InputTestFixture

Discussion in 'Input System' started by IoannisNoukakis9390, Sep 3, 2022.

  1. IoannisNoukakis9390

    IoannisNoukakis9390

    Joined:
    Jan 13, 2017
    Posts:
    2
    Hello! I'm currently doing some integration tests, and I'm failing to make my test run properly:

    I'm on unity 2021.3.9f1, inputsystem: 1.4.2 and test-framework 2.0.1-exp.1

    Here is the code:

    Code (CSharp):
    1. using System.Collections;
    2. using NUnit.Framework;
    3. using UnityEditor;
    4. using UnityEngine;
    5. using UnityEngine.InputSystem;
    6. using UnityEngine.SceneManagement;
    7. using UnityEngine.TestTools;
    8.  
    9. namespace Tests
    10. {
    11.     [TestFixture]
    12.     public class MovementComponentTest
    13.     {
    14.         private InputTestFixture _fixture = new InputTestFixture();
    15.         private Keyboard _keyboard;
    16.  
    17.         private GameObject _sut;
    18.         private Transform _transform;
    19.         [UnitySetUp]
    20.         public IEnumerator USetUp()
    21.         {
    22.             yield return SceneManager.LoadSceneAsync(
    23.                 "TestScene",
    24.                 new LoadSceneParameters(LoadSceneMode.Single, LocalPhysicsMode.Physics3D)
    25.             );
    26.         }
    27.  
    28.         [SetUp]
    29.         public void SetUp()
    30.         {
    31.             _fixture.Setup();  
    32.             _keyboard = InputSystem.AddDevice<Keyboard>();
    33.            
    34.             LoadTestPlayerPrefab();
    35.         }
    36.        
    37.         [TearDown]
    38.         public void TearDown()
    39.         {
    40.             _fixture.TearDown();  
    41.         }
    42.  
    43.         [UnityTearDown]
    44.         public void UTearDown()
    45.         {
    46.             Debug.Log("Tearing dow");
    47.         }
    48.  
    49.         [UnityTest]
    50.         public IEnumerator ShouldNotMoveWhenNothingIsPressed()
    51.         {
    52.             yield return new WaitForFixedUpdate();
    53.  
    54.             AssertPositionRotation(
    55.                 new Vector3(0, 0, 0),
    56.                 Quaternion.Euler(0, 0, 0)
    57.             );
    58.         }
    59.  
    60.         [UnityTest]
    61.         public IEnumerator ShouldMoveForwardWhenTheCorrespondingInputIsPressed()
    62.         {
    63.             _fixture.Press(_keyboard.wKey);
    64.             yield return new WaitForFixedUpdate();
    65.             _fixture.Release(_keyboard.wKey);
    66.            
    67.             AssertPositionRotation(
    68.                 new Vector3(0, 0, Time.fixedDeltaTime),
    69.                 Quaternion.Euler(0, 0, 0)
    70.             );
    71.         }
    72.  
    73.         [UnityTest]
    74.         public IEnumerator ShouldMoveBackwardsWhenTheCorrespondingInputIsPressed()
    75.         {
    76.             _fixture.Press(_keyboard.sKey);
    77.             yield return new WaitForFixedUpdate();
    78.             _fixture.Release(_keyboard.sKey);
    79.            
    80.             AssertPositionRotation(
    81.                 new Vector3(0, 0, -Time.fixedDeltaTime),
    82.                 Quaternion.Euler(0, 0, 0)
    83.             );
    84.         }
    85.        
    86.         private void AssertPositionRotation(
    87.             in Vector3 position,
    88.             in Quaternion rotation
    89.         )
    90.         {
    91.             Assert.That(_transform.position, Is.EqualTo(position));
    92.             Assert.That(_transform.rotation, Is.EqualTo(rotation));
    93.         }
    94.  
    95.         private void LoadTestPlayerPrefab()
    96.         {
    97.             var prefab = AssetDatabase.LoadAssetAtPath(
    98.                 "Assets/Tests/TestPrefabs/TestPlayer.prefab",
    99.                 typeof(GameObject)
    100.             );
    101.  
    102.             _sut = Object.Instantiate(prefab, new Vector3(0, 0, 0), Quaternion.identity) as GameObject;
    103.             _transform = _sut.GetComponent<Transform>();
    104.         }
    105.     }
    106. }
    And here is the output I'm getting:


    ShouldMoveForwardWhenTheCorrespondingInputIsPressed (0.501s)
    ---
    SetUp : Unhandled log message: '[Exception] ArgumentOutOfRangeException: Cannot be negative
    Parameter name: value'. Use UnityEngine.TestTools.LogAssert.Expect
    ---
    UnityEngine.InputSystem.Users.InputUser.set_listenForUnpairedDeviceActivity (System.Int32 value) (at Library/PackageCache/com.unity.inputsystem@1.4.2/InputSystem/Plugins/Users/InputUser.cs:406)
    UnityEngine.InputSystem.PlayerInput.StopListeningForUnpairedDeviceActivity () (at Library/PackageCache/com.unity.inputsystem@1.4.2/InputSystem/Plugins/PlayerInput/PlayerInput.cs:1685)
    UnityEngine.InputSystem.PlayerInput.OnDisable () (at Library/PackageCache/com.unity.inputsystem@1.4.2/InputSystem/Plugins/PlayerInput/PlayerInput.cs:1720)
    ---
    ArgumentOutOfRangeException: Cannot be negative
    Parameter name: value
    UnityEngine.InputSystem.Users.InputUser.set_listenForUnpairedDeviceActivity (System.Int32 value) (at Library/PackageCache/com.unity.inputsystem@1.4.2/InputSystem/Plugins/Users/InputUser.cs:406)
    UnityEngine.InputSystem.PlayerInput.StopListeningForUnpairedDeviceActivity () (at Library/PackageCache/com.unity.inputsystem@1.4.2/InputSystem/Plugins/PlayerInput/PlayerInput.cs:1685)
    UnityEngine.InputSystem.PlayerInput.OnDisable () (at Library/PackageCache/com.unity.inputsystem@1.4.2/InputSystem/Plugins/PlayerInput/PlayerInput.cs:1720)


    upload_2022-9-3_16-11-13.png

    Has anyone had the same issue? Am I doing something wrong with the Test-framework or the input system API? Thx in advance !
     
  2. Syl667

    Syl667

    Joined:
    Mar 21, 2019
    Posts:
    1
    I had a similar issue which caused the same error message.

    My solution was to destroy the GameObject which contains the PlayerInput before InputTestFixture TearDown.

    So in your case, you would need to either unload the scene (note that TearDown is executed before UnityTearDown) or destroy
    _sut
    (depending on what contains PlayerInput components) before calling
    _fixture.TearDown()
     
  3. FableStudio

    FableStudio

    Joined:
    May 18, 2018
    Posts:
    2
    Hi, did you ever find a solution to this issue? I'm experiencing the same exact problem.

    I've tried destroying the PlayerInput gameObject as well as loading an empty scene in TearDown, but nothing seems to work. Does anyone know if there's a simple working example of a test that uses the PlayerInput component in the loaded scene?

    Thanks in advance.