Search Unity

Bug TestFixtureSource doesn't work with test runner

Discussion in 'Testing & Automation' started by liortal, Jan 16, 2020.

  1. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    Our project is using Unity 2018.4.0f1, so i don't have access to UTF (the one that is packaged via UPM).

    I want to set up a parameterized TestFixture but it does not work - the fixture never shows up in the test runner window.

    My test class (fixture) should receive a string parameter (denoting a path to a prefab), load that prefab and execute different tests on it.

    Here's a (simplified) example of the code i'm trying to use. Nothing shows up in the test runner window:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using NUnit.Framework;
    4. using UnityEditor;
    5. using UnityEngine;
    6.  
    7. namespace Tests.ContentValidation
    8. {
    9.     [TestFixture]
    10.     [TestFixtureSource(typeof(VillagePrefabProvider))]
    11.     public class VillagePrefabTests
    12.     {
    13.         private readonly GameObject villagePrefabPath;
    14.  
    15.         public VillagePrefabTests(string villagePrefabPath)
    16.         {
    17.             var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(villagePrefabPath);
    18.             this.villagePrefabPath = Object.Instantiate(prefab);
    19.         }
    20.  
    21.         [Test]
    22.         public void TestSomething()
    23.         {
    24.         }
    25.  
    26.         [Test]
    27.         public void TestAnotherThing()
    28.         {
    29.         }
    30.     }
    31.  
    32.     public class VillagePrefabProvider : IEnumerable<string>
    33.     {
    34.         private const string PREFAB_ROOT = "Assets/Prefabs/Tests";
    35.  
    36.         public IEnumerator<string> GetEnumerator()
    37.         {
    38.             var guids = AssetDatabase.FindAssets("t:prefab", new[] { PREFAB_ROOT });
    39.  
    40.             foreach (var guid in guids)
    41.             {
    42.                 yield return AssetDatabase.GUIDToAssetPath(guid);
    43.             }
    44.         }
    45.  
    46.         IEnumerator IEnumerable.GetEnumerator()
    47.         {
    48.             return GetEnumerator();
    49.         }
    50.     }
    51. }
     
    Last edited: Jan 17, 2020
  2. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    BTW i just tested the same code now on Unity 2019.3, the tests show up in the test runner window.
     
  3. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    Bump? @superpig or anyone from the team? should this be supported?
     
  4. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,660
    The fixture is in a namespace, like in your example?
     
  5. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    @superpig i'm such a noob... the test appeared in the test tree with the namespace so i had to look them up in a different place hahaha :)

    On another note - can i modify the displayed test name? the path is just too long and gets truncated.
     
  6. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,660
    Yes, I believe the standard NUnit mechanisms for setting test names should work. This is also a useful doc.