Search Unity

Possible to Enable Test DLLs in Editor, but Strip Them From Build?

Discussion in 'Testing & Automation' started by kromenak, Aug 5, 2020.

  1. kromenak

    kromenak

    Joined:
    Feb 9, 2011
    Posts:
    270
    My team has been using "Edit Mode" tests successfully for awhile, but we'd love to make use of "Play Mode" tests as well.

    Unfortunately, we are dealing with a large and relatively old project that cannot easily make use of Assembly Definitions without a major refactor. Therefore, we must write "Play Mode" tests that are part of the pre-defined assembly (Assembly-CSharp.dll). This means I had to enable "PlayMode Tests for All Assemblies".

    I only need to run tests in editor play mode, NOT in standalone builds. So, I never want test assemblies to be included in builds. To avoid the test scripts being included, I am simply wrapping them in UNITY_EDITOR.

    Now the problem is that builds contain test assemblies, since I turned them on for all assemblies. Ideally, I want test assemblies to ALWAYS be enabled in editor, but always be DISABLED when builds are made. But I don't see a clean way to do that.

    I tried using an AssemblyFilter (which is used by the Test Framework internally) to strip the test assemblies at build time:
    Code (CSharp):
    1. public string[] OnFilterAssemblies(BuildOptions buildOptions, string[] assemblies)
    2. {
    3.   return assemblies.Where(x => !x.Contains(nunitAssemblyName) && !x.Contains(unityTestRunnerAssemblyName)).ToArray();
    4. }
    The above actually seems to work on Android, but I get this error on iOS:
    Code (csharp):
    1. ArgumentException: The Assembly nunit.framework is referenced by Assembly-CSharp. But the dll is not allowed to be included or could not be found.
    From there, I'm stumped. I'm not aware of any way to programmatically change the referenced assemblies for Assembly-CSharp. Obviously, Unity can do it, but only if you manually flip the "PlayMode Tests for All Assemblies" and restart the editor.

    Is there any way I can use "Play Mode" tests in the pre-defined assembly without causing the test assemblies to be included builds? In other words, have test assemblies enabled for all assemblies, but only in editor?
     
  2. C-H-a-P

    C-H-a-P

    Joined:
    Feb 9, 2013
    Posts:
    4
    Were you able to solve it?

    Today, I run into the same issue beefing-up my project with ASMDEF's.
    (The issue occurs inside a Third Party package after building the project for Android.)

    EDIT: Well, okay... I figured it out.
    I had to redirect all unit asmdef references to their appropriated Editor folders. With that changes, I removed the unit refs from the non-editor asmdef's and et voilà, it works!
     
    Last edited: Feb 6, 2021