What Understudy weaves

When the Editor compiles your scripts, Understudy rewrites every user assembly: each assembly compiled from Assets or from an embedded or local package, runtime and Editor alike, Assembly-CSharp included. Each access to a member then checks whether a test has a spy, stub or fake on it.

Understudy never weaves:

  • Your test assemblies: every assembly that references Understudy, and every assembly whose code uses NUnit or the Test Framework, as tests written without Understudy do.
  • Understudy's own assemblies, and every assembly whose name ends in .CodeGen, such as compilation steps.
  • Packages from the package cache, such as registry and Git packages, and the Unity API. Their members are covered where woven code calls them.
  • The assemblies in the Excluded Assemblies list.

Player builds

The game you release is unchanged: a build without tests is never woven and carries no cost. Every such build checks its scripts for woven code, so woven code never ships unnoticed: if it finds any, for example from an assembly a custom build step reuses, the build fails with an error that names the assembly. To fix it, delete the Library/ScriptAssemblies folder and build again.

Understudy's runtime and Editor assemblies compile only with the Test Framework's UNITY_INCLUDE_TESTS define, as test assemblies do, so a build without tests contains none of Understudy.

A player build that includes tests, which the Test Runner makes to run Play mode tests on a platform, is woven like the Editor's scripts, so those tests use spies, stubs and fakes as in the Editor, on Mono and IL2CPP alike.

Outside a test

Outside a test, woven code behaves exactly as before: no double applies, nothing is recorded, and leak detection stays silent. Stack traces, Debug.Log locations and debugger stepping stay on your lines.

Performance cost

The game you release pays nothing, since it isn't woven. In the Editor and in player builds that include tests, a woven access outside a test run costs one check of a static flag and one branch, about 3 ns, and allocates nothing. In a benchmark frame that moves 1000 objects with vector math and clock reads, weaving adds 0.035 ms, 0.2% of a frame at 60 fps. Every Play mode session in the Editor runs woven code.

Weaving adds to compile time only for the assemblies Unity recompiles, usually the one holding the script you changed: about 1.5 s for an assembly of about 4500 lines. Assemblies that didn't change aren't woven again. To skip an assembly a test never needs, refer to Exclude an assembly. Weaving runs alongside Unity's other compilation steps, such as Burst's.

Exclude an assembly

To keep an assembly from being woven, such as an asset that breaks under weaving or takes long to compile, add it to Excluded Assemblies on the Understudy page of the Project Settings window. Spies, stubs and fakes then don't reach its own code, nor its calls to the Unity API and to packages. Its calls to the public and internal methods and properties of your other assemblies still reach their doubles; its reads and writes of their fields don't. Changing the list recompiles every script. For more information, refer to Project Settings reference.

Code Burst compiles

Code that Burst compiles isn't woven: a [BurstCompile] method or type, and everything it calls in the code under test. Its calls always run for real and aren't recorded. When a double covers a member that Burst code also calls, Understudy logs a warning when the double is created, naming each of those calls. A Real stays silent. Methods of your other assemblies that Burst code calls stay unwoven too; when that list changes, the Console says so and scripts recompile once.

What a double doesn't reach

  • The engine's own state: a faked transform.position moves nothing for physics or rendering.
  • Related members: faking position leaves localPosition real.
  • Fields read without C#: reflection, Unity's serializer (the Inspector, saved scenes and prefabs) and JsonUtility read the real field, not a fake's stored value.
  • An object built by code that isn't woven, such as AddComponent: a constructor stub records it, but the real object is built.
  • The test assembly itself, which isn't woven: its direct reads reach the real member.

Additional resources