• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by opertoonist · Nov 27, 2018 at 01:59 PM · customeditorunittest

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?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tulsisvt · Jan 06 at 08:00 PM 0
Share

Did you find any answers for this?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by SolidAlloy · Jan 09 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:

  1. Initialize a MonoBehaviour.

  2. Add some items to its list.

  3. Create an editor for the MonoBehaviour.

  4. Call an editor method that adds/removes an item from the list.

  5. Call customEditor.serializedObject.ApplyModifiedProperties().

  6. Check that the item was actually added/removed in the MonoBehaviour's list.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

97 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Custom Editor item list 1 Answer

How can I draw the default widget for a field in a CustomEditor? 2 Answers

Nesting custom inspectors 0 Answers

Silence Debug.Log for unit test? 2 Answers

DrawDefaultInspector crash 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges