- Home /
Testing editor scripting
Hello, I created a tool for Unity using editor scripting (my tool is a window which allows the user to create certain gameobjects). I want to create unit tests to test the functionality of this window - but my problem is that in order to properly test the functionality of the window I need the OnGUI() function to run (to simulate a mouse movement or something similar). This is an example of a test I want to run:
[UnityTest]
public IEnumerator testMonoScriptInit()
{
GameObject go = (GameObject)Resources.Load("Tests/Sphere");
AssetCreatorWindow acw = (AssetCreatorWindow)ScriptableObject.CreateInstance(typeof(AssetCreatorWindow));
acw.Construct(go);
yield return null; //continue 1 frame
acw.DrawAssetSettings();
Assert.AreNotEqual(acw.getMonoscripts(), null);
}
In my example - AssetCreatorWindow acw is an instance of the window I am testing. I used yield return null even though I'm not sure what it does, anyway the problem is that I need to run acw.DrawAssetSettings() and this function utilizes GUI functions - so the tester won't run them (because GUI functions can only be called from OnGUI()) Is there anyway to run OnGUI() from code? I can't just call it because it is not a public method... Thanks all.
Answer by NoBrainer-David · Apr 14, 2018 at 10:07 AM
What exact functionality do you want to test that happens inside of DrawAssetSettings()
? I would suggest pulling that functionality out of OnGUI()
into it's own function. Then, both your AssetCreatorWindow and the test method testMonoScriptInit can call it.