Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Info on UNITY_INCLUDE_TESTS define

Discussion in 'Documentation' started by mbatutis, May 14, 2020.

  1. mbatutis

    mbatutis

    Joined:
    Jan 3, 2017
    Posts:
    4
    Hi, I am trying to figure out when UNITY_INCLUDE_TESTS define is... defined... and can't seem to find any documentation in docs.unity3d.com. Could someone shed some light on it?

    From the tests I've run it seems to always be defined in the editor whether just running a scene, or unit tests. And its not defined in any standalone builds. I am asking because I want to know if I can use this define to implement test time only setters, i.e.
    Code (CSharp):
    1. // In MyGame.Runtime.asmdef
    2. private int m_someInt;
    3. public int SomeInt
    4. {
    5.     get { return m_someInt; }
    6. #if UNITY_INCLUDE_TESTS
    7.     set { m_someInt = value; }
    8. #endif
    9. }
    Edit: It would also be good to know which Unity versions this is available in. For me they are available in 2018.4.11 & 2019.3.3.
     
    Last edited: May 14, 2020
    EmberLuis likes this.
  2. EmberLuis

    EmberLuis

    Joined:
    May 22, 2019
    Posts:
    5
    I was also hoping that UNITY_INCLUDE_TESTS was only defined when running the Test Runner, but this does not appear to be the case, as you mentioned. I have not yet found a way to differentiate between code that is being run from a test in Play Mode from code being run in the Editor normally.

    In my case there is some initialization code that I don't want/need to happen when running unit tests
    Code (CSharp):
    1. [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
    2. private static void Create()
    3. {
    4. #if !UNITY_INCLUDE_TESTS
    5.     // Initialization code for game only
    6. #endif
    7. }
     
  3. Julian_Gamble

    Julian_Gamble

    Joined:
    Feb 26, 2020
    Posts:
    7
    We've also been hitting the same problem in trying to extend UTF.

    In our scenario, we have a package that is split into Editor & Runtime components, both of which contain several helpers use in post result processing etc.

    If I have the asmdef of both these components have a Define Constraints on 'UNITY_INCLUDE_TESTS', and they are in the Assets folder, irrespective of whether or not I've added UNITY_INCLUDE_TESTS to the scripting define symbols they are always included.

    If I move this code to a local package, and keep the define constraints for both components, no matter what I do with UNITY_INCLUDE_TESTS, the code is never included. This define behaves incredibly sporadically; clarity would be helpful.

    The insanely confusing thing is if I use some arbitrary (and made up) define in the exact same pattern, it behaves correctly. UNITY_INCLUDE_TESTS must behave similarly to this as it has the very critical aspect of including the NUnit.dll in the runtime.
     
  4. Warnecke

    Warnecke

    Unity Technologies

    Joined:
    Nov 28, 2017
    Posts:
    60
    Hey. The purpose of UNITY_INCLUDE_TESTS is to signal whether an assembly contains tests. The constraint is added to the asmdef for the test assembly automatically when it is created. It is used by the build system to ensure that your tests does not get included in the player build, when building normally.
     
  5. EmberLuis

    EmberLuis

    Joined:
    May 22, 2019
    Posts:
    5
    Thanks for the update. This at least explains how that value is intended to be used. I'm still unaware however of a way to check whether code is being run as part of a Play Mode test versus a normal play session in the editor.
     
  6. Julian_Gamble

    Julian_Gamble

    Joined:
    Feb 26, 2020
    Posts:
    7
    From our perspective, I'd amend that statement to include 'whether an assembly contains tests, or UTF extension code'. Furthermore, it's important to note that if we are speaking about a package containing theses tests (or again, in our case UTF extension code) it must be added to the testables dictionary of the consuming project's manifest in order for it to behave idiomatically w.r.t. UNITY_INCLUDE_TESTS define.
     
unityunity