ECS Physics in edit-mode tests

We have some systems that depend on physics we would like to write unit tests for. This has proven quite complicated, at least in figuring out how to correctly initialise the physics / collision / dynamics worlds, register them correctly, and which physics systems are required to run.

I had a peek at Unity.Physics test package, and it appears many of these tests depend on Play Mode - I assume this might be easier, as all the physics systems would be set up.

But these are slow and cumbersome to run. Is there any information about how to most easily “fake” physics in edit-mode tests?

Update:
It turned out that correctly setting up a PhysicsWorldSingleton in the test world was enough.

Just that this is kind of tricky to do correctly, as it is not obvious how to correctly create and populate the PhysicsWorld.

For anyone stumbling on this in the future, here’s some steps I needed to take to get it working for systems that rely on collision checks. Tried to make it short, so forgive me for any errors:

// 1. Create collision world with anticipated num bodies
var collisionWorld = new CollisionWorld(staticBodyCount, dynamicBodyCount);

// 2. Create dynamics world matching anticipated num dynamic bodies
var dynamicsWorld =  new DynamicsWorld(dynamicBodyCount, 0);

// 3. Create the singleton 
var singleton = new PhysicsWorldSingleton
    {
        PhysicsWorld = new PhysicsWorld
        {
            CollisionWorld = collisionWorld,
            DynamicsWorld = dynamicsWorld
        }
};

// 4. Set a body
var dynamicBodies = singleton.PhysicsWorld.CollisionWorld.DynamicBodies;
dynamicBodies[0] = new RigidBody
{
    Collider = yourCollider,
    WorldFromBody = new RigidTransform(yourRotation, yourPosition),
    Entity = yourEntity,
    Scale = 1f
};

// 5. Update body index map
singleton.PhysicsWorld.CollisionWorld.UpdateBodyIndexMap();

// 6. Build broadphase
singleton.PhysicsWorld.CollisionWorld.BuildBroadphase(
    ref singleton.PhysicsWorld,
    1f,    // Desired timestep
    9f     // Desired gravity
);

Something like that. Our own internal tooling for setting this up is already hundreds of lines long. It would be great to have some utils from Unity to help achieve this more easily!