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.
// In MyGame.Runtime.asmdef
private int m_someInt;
public int SomeInt
{
get { return m_someInt; }
#if UNITY_INCLUDE_TESTS
set { m_someInt = value; }
#endif
}
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.
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
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
private static void Create()
{
#if !UNITY_INCLUDE_TESTS
// Initialization code for game only
#endif
}
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.
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.
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.
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.
I’ve found a workaround that works in the editor and player. Basically you need to have a public static field/property somewhere in your main scripts assembly that defaults to “game mode”.
Then from within a [SetUp] or [OneTimeSetUp] method in your test code, set this static to whatever value you want to represent “test mode”.
Here’s the component that bootstraps my game
public class GameWorld : MonoBehaviour
{
public static bool ReadyToStartGame = true;
void Update()
{
if (!ReadyToStartGame)
return;
// other code that actually starts the game
}
}
Then from within the test
public class TestClass
{
[SetUp]
public void SetUp()
{
GameWorld.ReadyToStartGame = false;
// load scene and other stuff here
}
[UnityTest]
public IEnumerator TestCode()
{
// any per-test setup logic
GameWorld.ReadyToStartGame = true;
// assertions for test here
}
}
I’ve also ran into this right now. I created a new local package and all my tests do not show up. Furthermore, Rider doesn’t highlight/analyze the files in this assembly.
It does work when I remove the UNITY_INCLUDE_TESTS constraint. But I’ll eventually forget about that and all the test code ends up in the player, so I’m looking for a fix.
I double-checked the asmdefs and package.json of my other (local) packages where I have working unit tests or am able to quickly set them up. But I did not find a notable difference between them, they all also have the hideInEditor=false settings so that’s not it. Just this one package is affected and it’s only because of the constraint that the tests aren’t working/recognized.
UPDATE with fix:
I went to Preferences => External Tools and ticked almost all of the “Generate .csproj files for:” boxes. For me that meant ticking two additional boxes: the “Local tarball” and “Packages from unknown sources” checkboxes.
Then I closed Unity and Rider and deleted all .csproj and .sln files from the project folder.
After relaunching both Unity and Rider code analysis in test scripts is working and the tests show up in Test Runner. I bet this particular package must be considered to be “from an unknown source” for some reason (it’s not in a tarball).
In case this helps anyone, this is the package.json (location is the same base path as other local packages I use, in a subfolder named “GMesh”):
Yes in the meantime someone from Rider pointed that out. It‘s so easy to forget about that. The issue I was having was that tests did not show in Rider but worked fine in Unity.
I met the same problem,and your method not worked for me. To me this problem occupys when the local package is added into manifest file use file:…/xxx。If I copy the package to the packages folder, everything looks ok! I don’t know how to resolve this problem, and it’s ok when open with visual studio.