How can I configure my UnityTest in order to use/test my own classes?

I am trying to use the testing framework for the first time. I am using Unity 2019.3.0a6.

I have created a Test folder by right clicking my asset folder and choosing Create → Testing → Tests Assembly Folder. Inside the folder I have created a test by right clicking and choosing Create → Testing → Test Script. I open the test and want to create an instance of the class I want to test like this. (The created assembly folder has default Import Settings).

using System.Collections;
using System.Collections.Generic;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.TestTools;

namespace Tests
{
    public class TestActionSequence
    {
        // A Test behaves as an ordinary method
        [Test]
        public void TestActionSequenceSimplePasses()
        {
            // 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 TestActionSequenceWithEnumeratorPasses()
        {
            [B]var objectToTest = new GameObject().AddComponent<ActionSequence>(); //error CS0246: The type or namespace name 'ActionSequence' could not be found (are you missing a using directive or an assembly reference?)[/B]
            // Use the Assert class to test conditions.
            // Use yield to skip a frame.
            yield return null;
        }
    }
}

As fare as I can tell there is no reference to the “Assembly-CSharp” where all my classes are. There is the option to add a Assembly reference from the .asmdef file but my project assembly is not listed in the list. What am i missing here?

In test assemblies you aren’t allowed to reference Assembly-CSharp, but you can create other asmdefs and reference them. In case you don’t know how they work, when you create an asmdef within a folder, scripts that have that folder as a common root will be a part of that assembly. If there is a there is subfolder with an asmdef somewhere down the tree, scripts with that root will be a part of that new assembly. Two asmdefs cannot exist in the same folder for this reason.

Theoretically this means that you can create a single asmdef for every script in your project and reference it, but that’s a bad idea. For each module I usually have an assembly folder for them with a subfolder with their test assembly. If you’re doing integration tests across modules you’ll need to get creative.

2 Likes

Thank you so much!