Problem when using InitializeOnLoad with PlayMode tests

There is an editor script in my project that automatically loads the initial scene when developer enters play mode in unity. The script basically is based on InitializeOnLoad() call.

Later on, I was required to implement playmode tests in the project. And here is the problem;
When I hit “run” in the test runner window, Unity playmode tests call gets kind of intercepted by that script I mentioned before.

Is there any way I can distinguish what is initializing the playmode in Unity?
If not, perhaps somebody has an idea of how I might change the behaviour so I can have both of those two functionalities working?

It depends on what you are trying to test but if you want things to be clean you could provide a shutdown method and manually call the initialize method in your tests. Use the Setup and Teardown methods to call them so that the state is always the same when the test starts.

E.G

using UnityEngine;
using UnityEditor;

class MyClass
{
    [InitializeOnLoadMethod]
    public static void Initialize()
    {
        // Initialize
    }

    public static void Shutdown()
    {
        // Shut down everything
    }
}

Alternatively you could add a flag and set it via the test setup and teardown methods.