Can't assert log message when press mouse using input system

I want to simulate input with unity’s InputTestFixture of input system.So I write a sample.It has a script and a test script response to.
Script code is:

public class InputSystemScript : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown((int)MouseButton.LeftMouse))
        {
            Debug.Log("a is clicked!");
        }
    }
}

Test code is:

public class InputSystemTest : InputTestFixture
{
        private Keyboard _keyboard;
        private Mouse _mouse;
        
        public override void Setup()
        {
            base.Setup();
            SceneManager.LoadScene("Scenes/SampleScene");
            
            _keyboard = InputSystem.AddDevice<Keyboard>();
            _mouse = InputSystem.AddDevice<Mouse>();
        }
        
        [UnityTest]
        public IEnumerator if_a_key_is_pressed()
        {
            Press(_mouse.leftButton);
            yield return new WaitForSecondsRealtime(3);
            
            Assert.That(_mouse.press.isPressed, Is.True);
            LogAssert.Expect(LogType.Log, "a is clicked!");
        }
        
        public override void TearDown()
        {
            base.TearDown();
        }
}

when I run test,the result is “Expected log did not appear: [Log] a is clicked!”.But the script is work when I run Unity.So I am very confused!What is wrong is in it?Who can help me.

The old UnityEngine.Input class has nothing to do with the new input system.

1 Like

Wow,It is work now!I rewite script now like this,It is work right immediately!
Now the script code like this:

public class InputSystemScript : MonoBehaviour
{
    private Mouse _mouse;
    void Start()
    {
        _mouse = Mouse.current;
    }

    // Update is called once per frame
    void Update()
    {
        // if (Input.GetMouseButtonDown((int)MouseButton.LeftMouse))
        // {
        //     Debug.Log("a is clicked!");
        // }

        if (_mouse.leftButton.isPressed)
        {
            Debug.Log("a is clicked!");
        }
    }
}

Thank you very very much!So that’s what happened.This is my fist time to request in this platform,and get the answer so quickly.Amazing!Thank you very much!

1 Like

A few things worth mentioning:

  • you only need to yield return null - there’s no need to artificially prolong the test execution to extend to a whopping three seconds!
  • asserting that the button is pressed is somewhat pointless because you can expect the input system to be fully tested
  • avoid using LogAssert because that makes your tests very brittle (depending on a string output). Also the logs won’t stay in production code. Instead, get the component you work with and check that it has the excpected state after the mouse was pressed. This could be a bool being set or the transform changing.
1 Like

Wow,I had seen your blogs many times,your coding skill is amazing!I haven’t thought I can talk with you.
Recently I automated a test,I want to automatly click a key to start some code,because debug prodution code is so slowly.So I wrote this sample code.I will continue to update my coding skill.
Thank you very much to see your reply,and very happy to meet you in the net :smiley: