-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathTestFixtures.cs
More file actions
85 lines (69 loc) · 2.52 KB
/
TestFixtures.cs
File metadata and controls
85 lines (69 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using System.Collections.Generic;
using System.IO;
using NUnit.Framework;
namespace UnityDataTools.TestCommon;
// Base class that facilitates iterating through sub-sub-folders
// inside the Data location. E.g. GetContexts("AssetBundles")
// finds "TestCommon/Data/AssetBundles/2019.4.0f1", "TestCommon/Data/AssetBundles/2020.3.0f1" etc.
public class BaseTestFixture
{
protected Context Context { get; }
private static Dictionary<string, List<Context>> m_Cache = new();
public BaseTestFixture(Context context)
{
Context = context;
}
// Tests that have files that record the expected results for each version
// of Unity can override this method to regenerate those expected results.
protected virtual void OnLoadExpectedData(Context context)
{
}
[OneTimeSetUp]
public void LoadExpectedData()
{
OnLoadExpectedData(Context);
// Load json file with the expected results for a test based on
// folder structure convention (e.g. ExpectedData/<UnityVersion>/ExpectedVersions.json)
Context.ExpectedData.Load(Context.ExpectedDataFolder);
}
protected static IEnumerable<Context> GetContexts(string dataFolder)
{
if (m_Cache.TryGetValue(dataFolder, out var cases))
{
return cases;
}
cases = new List<Context>();
m_Cache[TestContext.CurrentContext.TestDirectory] = cases;
var subfolder = Path.Combine(TestContext.CurrentContext.TestDirectory, "Data", dataFolder);
foreach (var folder in Directory.EnumerateDirectories(subfolder))
{
cases.Add(new Context(folder));
}
return cases;
}
}
// Test fixture that repeats the tests for each folder inside TestCommon/Data/AssetBundles.
// Each sub-folder is expected to have results of an AssetBundle build repeated with a
// different version of Unity.
[TestFixtureSource(typeof(AssetBundleTestFixture), nameof(GetContexts))]
public class AssetBundleTestFixture : BaseTestFixture
{
public AssetBundleTestFixture(Context context) : base(context)
{
}
public static IEnumerable<Context> GetContexts()
{
return BaseTestFixture.GetContexts("AssetBundles");
}
}
[TestFixtureSource(typeof(PlayerDataTestFixture), nameof(GetContexts))]
public class PlayerDataTestFixture : BaseTestFixture
{
public PlayerDataTestFixture(Context context) : base(context)
{
}
public static IEnumerable<Context> GetContexts()
{
return BaseTestFixture.GetContexts("PlayerData");
}
}