2

I want to write xUnit tests for my Unity project. While I am fairly new to Unity itself, I have experience with xUnit, hence this test framework and not another one.

Now I created a new solution in Visual Studio 2019 and created a xUnit project. The test solution is in a folder structure next to my Unity project, like proposed in this thread: Unit tests are inconclusive when adding xUnit project to Unity project. I also added the (temporary) Unity project csproj file (it seems to be created new whenever Unity builds the project new).

Now when I want to run a xUnit test, I get error whenever I run "Unity specific code", such as UnityEngine.Debug.Log or UnityEngine.JsonUtility.FromJson. The error message reads the following for example

Message: 
    System.Security.SecurityException : ECall methods must be packaged into a system module.
Stack Trace: 
    DebugLogHandler.LogFormat(LogType logType, Object context, String format, Object[] args)
    Logger.Log(LogType logType, Object message)
    Debug.Log(Object message)
    Settings.get_AppSettings() line 25
    SettingsTest.AppSettings_Default_Test() line 25

What can I do in order to run my xUnit tests in Visual Studio?

user3079834
  • 2,009
  • 2
  • 31
  • 63
  • Got the same issue creating unit tests for a Godot project. – Syndog Aug 18 '22 at 14:39
  • I ended up with a test project for Unity tests that had no Unity specific functionality and also some ugly `#ifdef`'s. A lot of code with Unity specific methods is untested. Not a happy solution but a workaround for now. – user3079834 Aug 24 '22 at 11:23

0 Answers0