Zenject icon indicating copy to clipboard operation
Zenject copied to clipboard

ZenjectIntegrationTestFixture.Setup destroys objects that prevent UnityTests from running.

Open underscoreglook opened this issue 3 years ago • 0 comments

Repro steps:

This is on Unity 2020.3.8f1

  1. Create a class:
public class BlahBlah : ZenjectIntegrationTestFixture
{
    [UnityTest]
    public IEnumerator RunTest1()
    {
        // Setup initial state by creating game objects from scratch, loading prefabs/scenes, etc

        PreInstall();

        // Call Container.Bind methods

        PostInstall();

        // Add test assertions for expected state
        // Using Container.Resolve or [Inject] fields
        yield break;
    }
}
  1. Run RunTest1 in PlayMode in Unity's test runner. Notice that it hangs.
  2. Put breakpoint on PreInstall() in Rider or whatever IDE you have and attach to Unity. Run test again, notice that breakpoint never gets hit.
  3. Comment out the line GameObject.DestroyImmediate(obj); in ZenjectIntegrationTestFixture.SetUp. Run test again, notice that the breakpoint IS hit this time.

What is expected instead

Expected is that the test runner doesn't hang and the actual test is run (and the breakpoint is hit) even as listed in step 3.

Other notes

It seems that the only object that's destroyed is "Code-based tests runner", which seems to be the thing actually running the tests.

Workaround

I replaced the offending line with

                if (obj.name != UnitTestRunnerGameObjectName)
                {
                    Object.DestroyImmediate(obj);
                }

and my tests work now.

underscoreglook avatar Jun 29 '22 01:06 underscoreglook