2

I'm using Unity 2019.4.4f1 with NUnit 3.5.

I have set up a very simple parallel test between methods to see if it actually works, but they run in sync. I've been looking into NUnit parallel testing for some days, but I feel like I'm missing some very obvious thing.

using NUnit.Framework;
using System.Threading;

namespace NUnitTest
{
    [TestFixture]
    public class ParallelTest
    {
        [Test]
        [Parallelizable]
        public void Test01()
        {
            Debug.Log("Test 1 started");
            Thread.Sleep(2000);
            Debug.Log("Test 1 ended");
            Assert.IsTrue(true);
        }

        [Test]
        [Parallelizable]
        public void Test02()
        {
            Debug.Log("Test 2 started");
            Thread.Sleep(2000);
            Debug.Log("Test 2 ended");
            Assert.IsTrue(true);
        }
    }
}

In TestRunner, I run ParallelTest to run them both at the same time. I'd imagine the total runtime of ParallelTest should be near 2 seconds, but instead it's 4 seconds.

Any advice will be appreciated.

2
  • What does the test output show? Do they both start at the same time and end at the same time, or does one finish before the other starts?
    – Kevin
    Aug 21, 2020 at 22:45
  • It seems to stay idle and they both get check marks at the same time. Aug 24, 2020 at 14:25

2 Answers 2

2

Unity has adapted the NUnit framework quite a bit. If there's one thing that Unity applications have never got along with, that would be async code.

The NUnit framework runs tests in parallel using multi-threaded test execution, which I imagine is the first thing Unity removed when creating their adaptation of the test runner.

If your code can be moved out of Unity and then imported via a DLL then I recommend you do that. That way your Unit Tests can use the raw NUnit framework! It's a pattern that I've adopted for all of my projects.

Long story short, I don't think you can.

2
  • Thanks for the heads up. There were not much information about Unity with Nunit 3.5 parallelizable. If I separate the code into a dll library and import it, are you saying that I can run in parallel? Aug 24, 2020 at 20:43
  • If you separate the code into a separate DLL and execute your tests NOT using Unity (I.e. use Visual Studio's test runner), you can run them asynchronously. But that means you can't run unit tests on anything that Unity provides, just your own code.
    – Fydar
    Aug 25, 2020 at 17:13
0

According to a Unity technology employee, this is still not supported even in recent version. But the answer he gave adds more details.

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.

[...] 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).

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.