I am trying to write unit tests in C# for the scripts that need to run inside Unity3D, later. My C# scripts are in the folder "Assets/Scripts" of the Unity project.
I am using Unity version 2019.1.0f2 with Visual Studio Community for Mac V8.0.5 (build 9), under Mac OS X version 10.14.4.
Unity has a test runner window that I used: Window > General > Test runner
. Inside that test runner window, I clicked on "Play mode" and on "Create Playmode Test Assembly Folder". Unity created a folder called "Tests" for me in which I am supposed to put my tests. So far, so good.
Now here is the problem:
My test code cannot see the classes under test that are in the parent folder of that test folder!
This is the folder hierarchy:
Assets
Scripts
MyFirstScript.cs
Tests
MyTestScript.cs
Tests.asmdef
When I double-click on MyTestScript.cs, Visual Studio opens the solution and the source file OK. Now, I am trying to write
var x = new MyFirstScript();
and the compiler complains "The type or namespace name 'MyFirstScript' could not be found".
It seems as if the classes in the Scripts folder are completely unknown inside the classes that are in the Tests folder.
This is MyFirstScript.cs:
using UnityEngine;
public class MyFirstScript : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
And this is MyTestScript.cs:
using System.Collections;
using System.Collections.Generic;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.TestTools;
namespace Tests
{
public class MyTestScript
{
// A Test behaves as an ordinary method
[Test]
public void MyTestScriptSimplePasses()
{
var x = new MyFirstScript(); // problem occurs HERE!
// Use the Assert class to test conditions
}
// A UnityTest behaves like a coroutine in Play Mode. In Edit Mode you can use
// `yield return null;` to skip a frame.
[UnityTest]
public IEnumerator MyTestScriptWithEnumeratorPasses()
{
// Use the Assert class to test conditions.
// Use yield to skip a frame.
yield return null;
}
}
}
I expected that I could use the class MyFirstScript in my test code (MyTestScript). What can I do?