Search Unity

Assertions with UnityEngine.Assertions and NUnit.Framework

Discussion in 'Testing & Automation' started by maxxmudd, Jun 14, 2021.

  1. maxxmudd

    maxxmudd

    Joined:
    Jan 26, 2021
    Posts:
    2
    I am using Unity 2020.3.11f1 LTS and Visual Studio Community 2019, and working with EditMode testing. I am also new to unit testing in general.

    I would like to access the Assert.AreApproximatelyEqual() and Assert.AreNotApproximatelyEqual() methods found here. However, these two methods are part of UnityEngine.Assertions which are not included by default in generated test scripts. I can access them by using it as a prefix, but importing UnityEngine.Assertions creates ambiguity with NUnit.Framework, since they both have Assert classes. Removing NUnit.Framework removes the ability to use the [Test] attribute so testing breaks.

    I read something about defining UNITY_ASSERTIONS under define constraints, but have had no luck with that. I have tried checking Development Build since I saw some posts about that, but that doesn't change anything. I have also updated the Test Framework item in Package Manager, thinking that may have been relevant, but still nothing.

    I can always just go ahead and use the prefix each time, but I am wondering if this is a symptom of having something set up incorrectly, or if I am attempting to use these methods in the wrong context (the documentation shows them being used in Monobehaviours).

    Here is the assembly file that was created when creating the test assembly folder through Unity's Test Runner:

    Code (CSharp):
    1. {
    2.     "name": "EditModeTests",
    3.     "rootNamespace": "",
    4.     "references": [
    5.         "UnityEngine.TestRunner",
    6.         "UnityEditor.TestRunner",
    7.         "MainAssembly"
    8.     ],
    9.     "includePlatforms": [
    10.         "Editor"
    11.     ],
    12.     "excludePlatforms": [],
    13.     "allowUnsafeCode": false,
    14.     "overrideReferences": true,
    15.     "precompiledReferences": [
    16.         "nunit.framework.dll"
    17.     ],
    18.     "autoReferenced": false,
    19.     "defineConstraints": [
    20.         "UNITY_INCLUDE_TESTS"
    21.     ],
    22.     "versionDefines": [],
    23.     "noEngineReferences": false
    24. }
    Here is the assembly file created that sits in the folder with all my scripts:

    Code (CSharp):
    1. {
    2.     "name": "MainAssembly",
    3.     "rootNamespace": "",
    4.     "references": [],
    5.     "includePlatforms": [],
    6.     "excludePlatforms": [],
    7.     "allowUnsafeCode": false,
    8.     "overrideReferences": false,
    9.     "precompiledReferences": [],
    10.     "autoReferenced": true,
    11.     "defineConstraints": [],
    12.     "versionDefines": [],
    13.     "noEngineReferences": false
    14. }
    Thanks all, I hope this is enough info but I can provide more details if needed.
     
  2. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,657
    TheMemorius and maxxmudd like this.