Search Unity

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

Discussion in 'Scripting' started by Ivemic, Jun 22, 2019.

  1. Ivemic

    Ivemic

    Joined:
    Feb 4, 2018
    Posts:
    5
    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).

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using NUnit.Framework;
    4. using UnityEngine;
    5. using UnityEngine.TestTools;
    6.  
    7. namespace Tests
    8. {
    9.     public class TestActionSequence
    10.     {
    11.         // A Test behaves as an ordinary method
    12.         [Test]
    13.         public void TestActionSequenceSimplePasses()
    14.         {
    15.             // Use the Assert class to test conditions
    16.         }
    17.  
    18.         // A UnityTest behaves like a coroutine in Play Mode. In Edit Mode you can use
    19.         // `yield return null;` to skip a frame.
    20.         [UnityTest]
    21.         public IEnumerator TestActionSequenceWithEnumeratorPasses()
    22.         {
    23.             [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]
    24.             // Use the Assert class to test conditions.
    25.             // Use yield to skip a frame.
    26.             yield return null;
    27.         }
    28.     }
    29. }
    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?
     
  2. wllfr

    wllfr

    Joined:
    Jan 27, 2015
    Posts:
    9
    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.
     
    Ivemic likes this.
  3. Ivemic

    Ivemic

    Joined:
    Feb 4, 2018
    Posts:
    5
    Thank you so much!