- Home /
Any way to write unit tests for a custom editor?
Is there any good way to unit test a custom editor? For example, if I've got a custom editor that adds an item to a MonoBehavior's list, I have to make that addition via the editor's serializedObject, which is read-only, so it can't be assigned on the fly to an instance of the editor created within the test. Should I just give up trying to do this, or is there another way?
Answer by SolidAlloy · Jan 09, 2021 at 06:21 PM
The general approach for testing a custom editor is the humble object pattern. Move all the logic to a separate class and make the editor use that class. This way the custom editor itself doesn't have to be tested because it doesn't contain any logic and just calls methods on a class that can be tested.
As for serializedObject, you can use Update() and ApplyModifiedProperties() methods to keep it in sync with the Unity.Object instance it represents. The workflow is like this:
Initialize a MonoBehaviour.
Add some items to its list.
Create an editor for the MonoBehaviour.
Call an editor method that adds/removes an item from the list.
Call
customEditor.serializedObject.ApplyModifiedProperties()
.Check that the item was actually added/removed in the MonoBehaviour's list.
Your answer
![](https://koobas.hobune.stream/wayback/20220612182854im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
BCE0019 errors in Custom Editor script 1 Answer
Call EditorGUILayout.PropertyField of an element of an array 0 Answers
'CustomEditor' is not a attribute class 1 Answer
Unit Test command line results contains unexpected passes 1 Answer
Show few Original Properties beside Custom Editor items . 0 Answers