I’m using C# code which I am unable to change which relies on UnityEngine.Input. Is there any way for me to override the input to fake a button press or a mouse move so I can build automated UI tests?
It can only be done in the editor, but have a look at this:
It will make a fake input event be handled by the game window.
Since Shkarface Noori’s answer was incredibly unhelpful, I’ll elaborate:
What you can do is build what’s known as a “wrapper”. These are classes that contain a few simple functions that you can call from your project, and you can swap out the contents of the wrapper to alter the implementation. To do this, you’d use interfaces
. You create a general “ControlWrapper” interface, specifying what functions you need (such as, perhaps, “ButtonDown”), then, you can create multiple classes implementing this interface. One might return values based on the UnityEngine.Input, but another might return false values that are predefined by you. The beauty of this is that your project doesn’t need to know about how anything is done by the wrapper, it just needs to have an instantiation of the interface somewhere, and it reads all its values from there. Hopefully this has given you some idea of how to do this. If you’re still stuck, the concept of a “wrapper” is a common one, and not just in games, so it should be fairly easy to find more information about it.
I don’t know of a way to do this. I think your best bet will be making a wrapper class to abstract the input and accessing it via an interface. Then you can write a test class using the same interface and then swap in the test class for your testing.