Unity Test Runner --> unable to reference my code from the test

Hi @ll,

I have a Unity 2018.3.12/C# project called Assembly-CSharp using VS 2017 as IDE.

I want to create a test which I can call from the Unity Test Runner.

When I create the test, the code there is unable to reference the code in my main project.

For this problem, there are some suggestions on the internet here Missing reference between 'Assembly-CSharp' and 'Tests' assembly, created with Assembly Definition - Questions & Answers - Unity Discussions and here unit testing - In Unity3d test scripts, how do I call a static function from another class? - Stack Overflow.

There, I am told to create an assembly definition on the top of my code (folder Assets/scripts) and reference this assembly definition from the Tests.asmdef. Problem: When I create such an assembly definition, Visual Studio creates an entirely new project from only the code files and extracts my code files from my actual project “Assembly-CSharp”. This leads to numerous other compile errors.
Alternative: I created a Reference from the “Tests” project (containing the Unity tests) to the “Assembly-CSharp” project in VISUAL STUDIO. Unfortunately, Unity doesn’t want me to do that and kills this reference every time I start the editor.

So basically, I am only trying to create a Unity Test referencing my code which I can run with the Test Runner and it just doesn’t work! Can anyone help me?

Thank you, Domihansa

Hi, thanks for the answer.
I got it to work with the asmdef version of the tests, simply by putting the assembly definition “NewAssembly.asmdef” one layer ahead in the folder hierarchy: I put it in the folder “Assets” instead of “Assets/scripts”.
Now, Visual Studio still creates a new project from that, but in Visual Studio, it just replaces the “Assembly-C-Sharp” project by the “NewAssembly” project and tests as well as game work fine. Maybe not the most proper solution, but it works now.

There are two ways to have tests in your project:

  • Using asmdef’s. This will indeed create a new project because that code will be in a separate Assembly. This way there is a clear separation of your tests and your game code. There should be good guidence how to do it here Unity - Manual: Unit Testing
  • Enable tests for everything. This setting was added before asmdef’s got into the world. This will then add references to nunit and the TestRunner assemblies. The downside here is that your test code can be included in your final player builds and increase the size.