Search Unity

Question Parallel Tests

Discussion in 'Testing & Automation' started by mvaz_p, Mar 9, 2021.

  1. mvaz_p

    mvaz_p

    Joined:
    Aug 22, 2018
    Posts:
    80
    I've been trying to run some unit tests in parallel, but it doesn't seem to be working.
    At every test I'm checking the System.Threading.Thread.CurrentThread.ManagedThreadId, and it's always the same.

    After some searching I've ended up on this stackoverflow post which says that it's currently impossible in Unity.
    Is that correct?
     
  2. tsibiski

    tsibiski

    Joined:
    Jul 11, 2016
    Posts:
    604
    Sorry if this question sounds obnoxious. Not my intent. But why would you need unit tests to run in parallel? Unit tests are normally exceptionally fast tests. I've worked on projects that were truly enormous, and never encountered a suite that took over 30 seconds to run. Parallelization normally comes into the equation for load and UI automation tests (In my experience. Not to imply that's a universal fact.).

    That being said, NUnit has parallelization capabilities inherent to it (check out the Parallelizable class. You probably already have, though). I just haven't used it outside of Selenium tests using NUnit, and haven't used it in Unity at all. If the most modern version of NUnit is available in Unity, then it should be possible for compile-time execution (in theory).

    But parallelization uses async logic, which Unity is only recently working to support as a viable alternative to the UnityEngine's reliance on the main thread. I suspect that NUnit has no concept of this reliance, and anything you did to make it work around that problem would have to be custom. At least until async becomes ubiquitous.
     
    Last edited: Apr 1, 2021
  3. mvaz_p

    mvaz_p

    Joined:
    Aug 22, 2018
    Posts:
    80
    That is true. It's not exactly a need, but if we could make them faster by simply allowing them to run in parallel, why not?
    From what I've researched it seems totally possible (and easy to implement) using vanilla NUnit and our tests are already parallelizable.
    If we could run them 30% faster or something like it would be very nice already, specially on our CI.
     
  4. tsibiski

    tsibiski

    Joined:
    Jul 11, 2016
    Posts:
    604
    I wonder if the ultimate answer is in DOTS. It is a rewrite of the core of Unity logic to support multithreading. It may be the answer to allowing UnityEngine-based logic to be available to all threads simultaneously when running NUnit in parallel. Granted, I haven't explored it. Just wanted to post it in case you weren't aware of its existence yet.
     
  5. mvaz_p

    mvaz_p

    Joined:
    Aug 22, 2018
    Posts:
    80
    I'm aware, but I don't think it's the case. The tests are completely decoupled from Unity and I could probably run them with a standalone NUnit, but that wouldn't be very practical.
    I just would like to know if this feature was removed when they adapted NUnit to Unity, because it doesn't seem to work and there's no documentation about it.
     
    tsibiski likes this.
  6. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,659
    It is currently true that we don't support running parallelised tests inside Unity, because the custom runner we developed (to support things like [UnityTest]) doesn't have it implemented.

    As @tsibiski notes, the UnityEngine API has a reliance on the main thread - calling Unity APIs from background threads will (almost) always throw - so it hasn't been a priority to support this, as it would only be usable for tests that don't call Unity APIs. (Async doesn't help, because the synchronisation context in Unity only executes async tasks on the main thread, specifically because of this restriction on the API).
     
    mvaz_p likes this.
  7. Kobix

    Kobix

    Joined:
    Jan 23, 2014
    Posts:
    146
    Hey, would be cool if you guys would add support for Parallelizing non-unity tests, there is no reason not to.. it's up to user to mark his tests [Parallelizable].

    My tests already take 30 seconds and they all are parallelizable :).
     
  8. tsibiski

    tsibiski

    Joined:
    Jul 11, 2016
    Posts:
    604
    Can you explain your opinion/visualization on how that might work? A separate solution for any tests that lack a reference to any Unity dll's? How would it guarantee that no parallelize attribute is applied to a test that references a Unity dll?
     
  9. Kobix

    Kobix

    Joined:
    Jan 23, 2014
    Posts:
    146
    Doesn't even have to have no reference to Unity dll's imho; just run non-unity tests parked with [Parallel] as they should.

    Structs like Vector3 from unity.dll is still multithreadable, and so on.

    And if it breaks something, as any multithreaded code can, so let it be - users should be warned that [Parallel] can have such effects. If there is multithreading exception thrown, just stop all tests.

    Second variant could be using Job System, this way it would be possible to trully write parallel tests that can run with Unity engine itself :).
     
    Rabadash8820 likes this.