In Unity3d test scripts, how to call a static function from another class?
I have two files in a Unity3d project. One of them is a test script that runs in edit mode. The other is one class with static functions that I would like to call from 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 is my tested function:
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 an error from the Unity compiler (I am not building outside of Unity):
Assets / TestS / NewTestScript.cs (12,17): Error CS0103: The name `NewBehaviourScript 'does not exist in the current context
They work in edit mode.
I have tried adding and removing the SuperTestNameSpace namespace from the function under test and the calling code.
I tried to add / remove files from the .asmdef file that was auto-generated with unity, although this usually leads to other compilation errors.
My previous experience with unit testing was mostly in Visual Studio or VSCode and I am trying to write my paper so that my experience in unity3d testing matches my previous experience in testing environment.
Is there any fundamentally limited functionality in the edit mode tests, or am I missing something?
Further development of participating assemblies. It looks like there are two assemblies here: Assembly-CSharp.dll contains the code under test and TestS.dll contains the test code. I believe my questions boil down to this: How to add a reference from the TestS.dll assembly to the Assembly-CSharp.dll. I would know how to do this in Visual Studio (either through the context menu in VS, or edit the csproj file directly), however I cannot see how to do this in Unity3d. Any changes I make to the csproj file are often overwritten by one, and although there is a "links" section in the inspector (see picture), I cannot add Assembly-CSharp.dll as a link.
Your answer
Follow this Question
Related Questions
Instantiating prefab causes assembly load error in unit test 0 Answers
Can I use LogAssert to check for errors in a unit test without cluttering the console? 0 Answers
Unable to see NUnit Assert message output 1 Answer
Unit test: PlayerPrefs, Attempted to access a missing method. 1 Answer
Testing on Genymotion: Grey Scene except Canvas items 1 Answer