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 https://answers.unity.com/questions/1540387/missing-reference-between-assembly-csharp-and-test.html and here https://stackoverflow.com/questions/53845650/in-unity3d-test-scripts-how-do-i-call-a-static-function-from-another-class.
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
Answer by Domihansa · May 07, 2019 at 09:20 AM
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.
Answer by HaraldNielsen · Apr 30, 2019 at 07:13 AM
Hi, 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 https://docs.unity3d.com/Manual/testing-editortestsrunner.html
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.
Your answer
Follow this Question
Related Questions
Can't reference other namespaces from within TextMeshPro (TMPro) namespace. 0 Answers
Test runner errors InvalidOperationException and TreeView root item is null 1 Answer
Error CS1061 type Valve.VR.SteamVR_Render - Got code from another project? 0 Answers
Visual studio cannot find references to dlls from custom assembly definitions 2 Answers
Unity and external plugins 0 Answers