In Unity3d test scripts, how do I call a static function from another class?

Refresh

April 2019

Views

245 time

1

I have two files in a Unity3d project. One is a test script that runs in edit mode. The other is a single class with static functions that I'd like to call from the test scripts.

here's my test script:

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

public class NewTestScript 
{

    [Test]
    public void TestAnotherStaticFunction() 
    {
        int a = NewBehaviourScript.FunctionUnderTest(1);
        int b = 1;

        // Use the Assert class to test conditions.
        Assert.IsTrue(a == b);
    }
}

here's my function under test:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewBehaviourScript : MonoBehaviour 
{
    /// <summary>
    /// the stupidest function in the world, 
    /// used to verify tests.
    /// </summary>
    public static int FunctionUnderTest(int a)
    {
        return a;
    }
}

This gives me the error from the Unity compiler (I'm not building outside of Unity):

Assets/TestS/NewTestScript.cs(12,17): error CS0103: The name `NewBehaviourScript' does not exist in the current context

These are running in edit-mode.

I've tried adding and removing the SuperTestNameSpace namespace from the function under test and the calling code.

I've attempted adding/removing files from the .asmdef file that was autogenerated by unity, although this usually leads to other compile errors.

My previous unit test experience is largely in Visual Studio or VSCode, and I'm trying to get my unity3d test experience to match my prior test environment experiences.

Is there a fundamentally limited functionality in the edit-mode tests, or am I missing something stupid?

Further elaboration on the assemblies involved. It looks like there are two assemblies at play here: Assembly-CSharp.dll contains my code under test and TestS.dll contains my testing code. I believe my questions boils down to: how do I add a reference from the TestS.dll assembly to the Assembly-CSharp.dll. I'd know how to do this in Visual Studio (either via the context menu in VS or directly editing the csproj file), however I don't see how to do it in Unity3d. Any edit I make to the csproj file is frequently overwritten by unity, and while there is a 'references' section in the inspector (see picture) I can't add Assembly-CSharp.dll as a reference.

These are the inspector settings for TestS.asmdef. While there's an option to add references, I can't add a reference to Assembly-CSharp.dll, which is where my code-under-test lives.

3 answers

1

The main issue is currently you are trying to call the

NewBehaviourScript(1);

constructor which does not exist...

instead of the method

using SuperTestNameSpace;

//...

NewBehaviourScript.FunctionUnderTest(1);

or alternatively with the namespace in the call directly

SuperTestNameSpace.NewBehaviourScript.FunctionUnderTest(1);

Also make sure the filename matches exactly the class name. So in your case it should be

NewBehaviourScript.cs

Note that the .cs is not printed by Unity in the Project view so there it should say NewBehaviourScript.


Why does it not compile with the using SuperTestNameSpace;? What is the error?

If that exception

Assets/TestS/NewTestScript.cs(14,17): error CS0103: The name `NewBehaviourScript' does not exist in the current context

is only shown in VisualStudio but the script compiling fine in Unity especially after adding a new script it helps to simply close and restart VS.

In some cases it also helps to close Unity and VS, remove all files and folders except Assets and ProjectSettings (and if you are under version control anything that belongs to it like e.g. .git, .gitignore, .gitattributes etc) in particular delete the Library, .vs folder and all .csproj and .sln files.

Than open Unity again and let it recompile everything.

2

Ok, I figured this out. There were two things going on:

  1. Editor tests need to be underneath a folder called editor. It's really annoying that the unity editor doesn't do this for you.
  2. You need to have an assembly definition for the code under test and add a reference from the test code to the newly created assembly definition. This must be done in the Unity editor UI.

by default, unity adds your script code to an assembly called Assembly-CSharp.dll, and, for reasons unknown, this assembly isn't referenced by my edit mode test code. I'm not sure if this is a bug in Unity or if it's by design, but explicitly creating and referencing the assembly definition has cleared things up.

0

Make sure the file that contains your NewBehaviourScript class IS NOT inside an Editor folder.

Move both the scripts in the Assets (root) folder and try again.

Lyr