How to organise test code?

I’m having a little trouble with organising tests in my project. I keep my code in small packages, and they usually follow this structure.

/
--/Authoring (separate assembly)
--/Runtime (separate assembly)
--/... (other resources, e.g. models, prefabs, all in folders / subfolders)
--/Tests (separate assembly, autoReferenced: false)

Problem is, that test code (systems and components) leaks in the global context (those systems will be visible in the World, in all Editor windows, and clutter the space (they don’t cause issues, as they only run with test components, but still).

How do I avoid it? Ideally I want to still be able to run my tests, but don’t have them leak in the “production” code. I tried separate test setup, where they are in a project of their own and invisible to Unity, but that had some problems making it incompatible with DOTS, and i mostly need to test systems and their interactions.

Another similar question is about samples, as they do have exactly same problem, but I think with those it’s kinda inevitable, and I’d better just put them in the package of their own.

Well tests should live in an editor only assembly so they won’t make production. But it is annoying to have test systems appear in play mode in the editor so you can put this little snippet of code in an AssemblyInfo.cs file in the test assembly

using Unity.Entities;

[assembly: DisableAutoCreation]

which stops all systems being auto created in the test assembly and your tests are just responsible for creating the systems they need to test.

2 Likes

Thank you, Tertle! That solves majority of the problems!