Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Can't access C# Script classes in Unity Test Framework (Unity 2020.2.0b7)

Discussion in 'Scripting' started by joseph114591452, Oct 28, 2020.

  1. joseph114591452

    joseph114591452

    Joined:
    Mar 14, 2020
    Posts:
    24
    I have a static C# class that I want to test but I'm having trouble accessing the class in my test files.
    I'm getting the error: The name 'SampleClass' does not exist in the current context [Tests]csharp(CS0103)
    Here is what I have done so far.

    1. Create "EditMode" `Tests.asmdef` file in `Tests` directory
    2. Create `GameAssembly` Assembly Definition in my `Scripts` directory
    3. Add the `GameAssembly` to Assembly Definition References list in Tests.asmdef.

    Is there something I'm missing?

    My test file:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using NUnit.Framework;
    4. using UnityEngine;
    5. using UnityEngine.TestTools;
    6.  
    7. public class SampleClassTest
    8. {
    9.     // A Test behaves as an ordinary method
    10.     [Test]
    11.     public void AddTest()
    12.     {
    13.         int result = SampleClass.Add(1, 1);
    14.         Assert.AreEqual(2, result);
    15.     }
    16. }
    17.  
    Tests Assembly config:
    upload_2020-10-28_9-37-9.png

    GameAssembly config:
    GameAssembly.PNG
     
    Last edited: Oct 28, 2020
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    So where exactly is SampleClass defined?
     
  3. joseph114591452

    joseph114591452

    Joined:
    Mar 14, 2020
    Posts:
    24
    It's defined in Assets/Scripts/ directory