Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Exceptions when unit testing ECS in editor in a Tiny Project

Discussion in 'Project Tiny' started by Sarkahn, Nov 20, 2020.

  1. Sarkahn

    Sarkahn

    Joined:
    Jan 9, 2013
    Posts:
    440
    Hello, I was trying to run editor tests on some ECS stuff using the Editor Test Runner. When I run my tests I get this error:

    Code (CSharp):
    1. Destroy may not be called from edit mode! Use DestroyImmediate instead.
    2. Destroying an object in edit mode destroys it permanently.
    Part of running ECS tests in the editor involves creating and disposing a world as part of the set up and tear down process. However with Tiny when disposing a world in the editor it causes the above error. The error points to this line as the culprit from
    Library/PackageCache/com.unity.tiny@0.31.0-preview.24/Unity.Tiny.Input.Hybrid/InputHybrid.cs:63)


    devenv_ciL3CndDZK.png
     
    Last edited: Nov 20, 2020
    NagaChiang likes this.
  2. Sarkahn

    Sarkahn

    Joined:
    Jan 9, 2013
    Posts:
    440
    A simplified test that produces the error:
    Code (CSharp):
    1.     [Test]
    2.     public void DoTest()
    3.     {
    4.         var world = new World("TestWorld");
    5.  
    6.         var systems = DefaultWorldInitialization.GetAllSystems(
    7.             WorldSystemFilterFlags.Default,
    8.             requireExecuteAlways: false);
    9.         DefaultWorldInitialization.AddSystemsToRootLevelSystemGroups(world, systems);
    10.  
    11.         world.Dispose();
    12.     }
    It also prints "0 0" to the log every time for some reason.

    Unity_ao7gbrjFjI.png
     
    Last edited: Nov 20, 2020
  3. AbdulAlgharbi

    AbdulAlgharbi

    Unity Technologies

    Joined:
    Jul 27, 2018
    Posts:
    319
    Hi
    We don't support Editor/Playmode in Tiny
    So that won't work for you
    this test runs hybrid dots and not tiny
     
  4. Sarkahn

    Sarkahn

    Joined:
    Jan 9, 2013
    Posts:
    440
    Hi, thanks for the reply! Since Tiny is so new to me writing tests for my systems/components I'm adding will help a lot since having to build and play after each incremental change is pretty painful. I was able to get it working by using this setup:

    Code (CSharp):
    1. using NUnit.Framework;
    2. using System.Runtime.InteropServices;
    3. using Unity.Entities;
    4. using UnityEngine;
    5.  
    6. public class ECSTestBase
    7. {
    8.     protected World World { get; private set; }
    9.     protected EntityManager EntityManager => World.EntityManager;
    10.  
    11.     protected EntityQuery CreateEntityQuery(params ComponentType[] components) => EntityManager.CreateEntityQuery(components);
    12.  
    13.     protected EntityQuery GetEntityQuery(params ComponentType[] components) =>
    14.         CreateEntityQuery(components);
    15.  
    16.     [SetUp]
    17.     public void SetUpBase()
    18.     {
    19.         DefaultWorldInitialization.DefaultLazyEditModeInitialize();
    20.         World = World.DefaultGameObjectInjectionWorld;
    21.     }
    22.  
    23.     [TearDown]
    24.     public void TearDown()
    25.     {
    26.         World.Dispose();
    27.     }
    28.  
    29.     protected T AddSystemToWorld<T>() where T : SystemBase
    30.     {
    31.         DefaultWorldInitialization.AddSystemsToRootLevelSystemGroups(
    32.             World,
    33.             typeof(T));
    34.         return World.GetExistingSystem<T>();
    35.     }
    36. }
    I just wanted a quick and easy way to set up ECS-friendly tests in a Tiny project while I'm figuring things out. This way I can create tests that add systems/components and I can just call
    World.Update()
    in the test and it will work as expected, including executing command buffers. Is there a better way to do it?
     
  5. AbdulAlgharbi

    AbdulAlgharbi

    Unity Technologies

    Joined:
    Jul 27, 2018
    Posts:
    319
    Hi,
    You can run unit tests in tiny/ Dots runtime but it's still very experimental
    Check the attached project and see how I made the "Assets\Scripts\Test.cs"
    There are a few steps that you sjould be aware of:
    - I've added Unity.Tiny.Tests.Common to the project, currently we don't ship this with the package but we should so I'll try and get it out with tiny 0.33 but for now I've just copied it
    - Your asmdef name must end with .Tests in order for the build system to add the proper dependencies
    - Keep in mind that we don't support try/catch or exceptions in dots runtime, so on failure we just exit with code 1
     

    Attached Files:

    Sarkahn likes this.
  6. mark_sanders308

    mark_sanders308

    Joined:
    Apr 13, 2018
    Posts:
    1
    Hi,
    Is there anything different we need to do to run the tests? The example test is not showing up in the Unity Test Runner for me when I load that project, and it's the same in my own project. Thanks!