Skip to content

Commit a22fecc

Browse files
authored
Merge pull request #8 from equinor/EasyUnitTests
Add AsyncManager.InitializeForEditorTests()
2 parents 15ee6b1 + e01e2dc commit a22fecc

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

README.MD

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,3 +244,19 @@ public struct WaitForTimeSpan : IAwaitInstruction
244244
}
245245
}
246246
```
247+
248+
# Usage with Unity EditorTests (unit tests with NUnit)
249+
250+
When running unit tests, `AsyncManager` is not automatically initialized. Call `AsyncManager.InitializeForEditorTests()` in your test OneTimeSetUp method, for example like this:
251+
252+
```c#
253+
// In your test class
254+
255+
[OneTimeSetUp]
256+
public void OneTimeSetUp()
257+
{
258+
AsyncManager.InitializeForEditorTests();
259+
260+
// Other setup code here
261+
}
262+
```

UnityAsync/Assets/UnityAsync/Manager/AsyncManager.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,19 @@ static void Initialize()
5555
fixedUpdates = new ContinuationProcessorGroup();
5656

5757
Instance = new GameObject("Async Manager").AddComponent<AsyncManager>();
58-
DontDestroyOnLoad(Instance);
58+
if(!Application.isEditor) // DontDestroyOnLoad can not be called in editor mode
59+
DontDestroyOnLoad(Instance);
60+
}
61+
62+
/// <summary>
63+
/// Initializes the manager explicitly. Typically used when running Unity Editor tests (NUnit unit tests).
64+
/// </summary>
65+
public static void InitializeForEditorTests()
66+
{
67+
Initialize();
68+
69+
// Do not run tasks in background when testing:
70+
BackgroundSyncContext = UnitySyncContext;
5971
}
6072

6173
/// <summary>

0 commit comments

Comments
 (0)