Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question [SOLVED] Unit Test of Editor code

Discussion in 'Testing & Automation' started by Nighten_, Jan 17, 2023.

  1. Nighten_

    Nighten_

    Joined:
    May 15, 2022
    Posts:
    2
    Hello everyone,
    I have a simple editor tool that create assets from the content of a CSV. I use the UnityEditor namespace to access the [MenuItem()] and the AssetDatabase, so the script is placed in the "Editor" folder.

    I want to test this code with the Test Runner, but I can't instantiate my class to test it; it does "not exist in the current context".
    I tried moving the EditMode Test Assembly Folder in the Editor folder, and used UnityEditor in the test as well, but that didn't resolved the error, and I really can't find any information about this online.
    From what I understand from this post, it might be related to the script compilation order, but I don't know how to do something about it when it comes to test specifically.

    Have you implemented test for Editor tools like that? Is it even possible? Please tell me if I misunderstood something, I'm still new to unit test and Unity.

    Take care,
    Nighten
     
  2. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,660
    It's certainly possible to test Editor code, yes (we do so extensively).

    Putting the code in a folder named 'Editor' is not enough; you need to use an Assembly Definition File for your Editor code (set to include the 'Editor' platform only).

    Then you need to configure the Assembly Definition File for your tests folder to 1) include the Editor platform only and 2) reference the Assembly Definition File for your Editor code.
     
  3. Nighten_

    Nighten_

    Joined:
    May 15, 2022
    Posts:
    2
    Thank you so much for those instruction! I really missed those steps, but now it's working properly.
     
    superpig likes this.