Search Unity

Question Weird behaviour with parameterized TestFixture

Discussion in 'Testing & Automation' started by liortal, Apr 27, 2020.

  1. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    I am trying to use a parameterized TestFixture. The fixture is a class that contains many tests (validations) to be performed on prefabs in our project.

    The same set of tests should be executed on each of those prefabs (and we have over 300 of those).
    We are using 2018.4 so the Unity Test Framework from the package is not available to use at the moment.

    Clicking on the fixture name (e:g what's highlighted in the screenshot) does nothing.
    Clicking on individual tests under it starts "something" but seems to run for a long time (probably runs all tests with that name?) but eventually doesn't report any results.

    upload_2020-4-27_13-35-57.png

    Is there any open bug about this ? should it work?
    Code (CSharp):
    1. using System.Collections.Generic;
    2. using NUnit.Framework;
    3. using UnityEditor;
    4. using UnityEngine;
    5.  
    6. namespace ContentValidation.Tests
    7. {
    8.     [TestFixtureSource("PrefabPaths")]
    9.     [Category("Villages")]
    10.     public class VillagePrefabTests
    11.     {
    12.         private const string PREFAB_ROOT = "xxxxxx";
    13.        
    14.         private readonly string villagePrefabPath;
    15.         private GameObject villagePrefabInstance;
    16.  
    17.         public VillagePrefabTests(string villagePrefabPath)
    18.         {
    19.             this.villagePrefabPath = villagePrefabPath;
    20.            
    21.             // Instantiate, etc
    22.         }
    23.  
    24.         // Tests go here
    25.  
    26.         private static IEnumerable<string> PrefabPaths
    27.         {
    28.             get
    29.             {
    30.                 var guids = AssetDatabase.FindAssets("t:prefab", new[] { PREFAB_ROOT });
    31.  
    32.                 foreach (var guid in guids)
    33.                 {
    34.                     yield return AssetDatabase.GUIDToAssetPath(guid);
    35.                 }
    36.             }
    37.         }
    38.     }
    39. }
     
  2. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,659
    I'll have to let @Warnecke speak to whether this is expected to work in 2018 LTS, but just one thing about your code there - you're not doing instantiation etc inside the constructor, right? The constructor is run when populating the list of test cases, you shouldn't do any significant work inside it other than saving the parameters. Stuff like instantiating objects should go in a SetUp or OneTimeSetUp method.
     
  3. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    It was in OneTimeSetup, i was playing around trying to make it work.
     
    superpig likes this.
  4. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    I just verified this on Unity 2020.1 beta 6 - it works fine. I couldn't run 2019.3 for some reason, will give it another test.