Having a reference to nunit framework doesn't mean that test is actually running. For example in Unity when you activate play mode tests the nunit references are added to the project. And when you run a game the references are exist, so UnitTestDetector would not work correctly.
Instead of checking for nunit assembly we can ask nunit api to check is code under executing test now or not.
using NUnit.Framework; // ... if (TestContext.CurrentContext != null) { // nunit test detected // Do some setup to avoid error } Edit:
Beware that the TestContext may be automatically generated if it's required.