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?
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.
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.
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.
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.
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).
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 .
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?
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 .