Search Unity

Checking if a test is running

Discussion in 'Testing & Automation' started by Jonas-Neuston, Jan 24, 2022.

  1. Jonas-Neuston

    Jonas-Neuston

    Joined:
    Jun 10, 2017
    Posts:
    70
    Is there any code to see if a test is currently running?

    I'm working on a project with a lot of legacy code and there's a situation where we need to know if we are currently executing a test or "the real game".
     
  2. sbergen

    sbergen

    Joined:
    Jan 12, 2015
    Posts:
    53
    This should work:

    Code (CSharp):
    1. TestContext.CurrentTestExecutionContext?.ExecutionStatus == TestExecutionStatus.Running;
    There was a bug where this doesn't work correctly if domain reload is disabled. I'm not sure if it has been fixed yet, but this can be applied as a workaround:
    Code (CSharp):
    1. public static class DomainReloadWorkarounds
    2. {
    3.     [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
    4.     private static void ApplyWorkarounds()
    5.     {
    6.         TestContext.CurrentTestExecutionContext = null;
    7.     }
    8. }
     
    Jonas-Neuston likes this.
  3. Jonas-Neuston

    Jonas-Neuston

    Joined:
    Jun 10, 2017
    Posts:
    70
    Thanks @sbergen! I'll give that a try.