Skip to content

Commit

Permalink
Merge pull request #39 from nowsprinting/chore/takescreenshot_from_as…
Browse files Browse the repository at this point in the history
…ynctest

Add tests using ScreenshotHelper.TakeScreenshot method from async test
  • Loading branch information
nowsprinting committed Oct 31, 2023
2 parents ff49e7b + e098a74 commit e567c6a
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 8 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ jobs:
npm install -g openupm-cli
openupm add -f com.unity.test-framework
openupm add -f com.unity.testtools.codecoverage
openupm add -f com.cysharp.unitask
openupm add -ft "${{ env.package_name }}"@file:../../
working-directory: ${{ env.CREATED_PROJECT_PATH }}

Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ create_project:
touch UnityProject~/Assets/.gitkeep
openupm -c $(PROJECT_HOME) add -f com.unity.test-framework
openupm -c $(PROJECT_HOME) add -f com.unity.testtools.codecoverage
openupm -c $(PROJECT_HOME) add -f com.cysharp.unitask
openupm -c $(PROJECT_HOME) add -ft $(PACKAGE_NAME)@file:../../

.PHONY: remove_project
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,15 @@ public class MyTestClass
{
yield return ScreenshotHelper.TakeScreenshot();
}

[Test]
public async Task MyTestMethodAsync()
{
var coroutineRunner = new GameObject().AddComponent<CoroutineRunner>();
await ScreenshotHelper.TakeScreenshot().ToUniTask(coroutineRunner);
}

private class CoroutineRunner : MonoBehaviour { }
}
```

Expand All @@ -398,6 +407,7 @@ public class MyTestClass
> - Must be called from main thread.
> - `GameView` must be visible. Use [FocusGameViewAttribute](#FocusGameView) or [GameViewResolutionAttribute](#GameViewResolution) if running on batch mode.
> - Files with the same name will be overwritten. Please specify filename argument when calling over twice in one method.
> - UniTask is required to be used from the async test. And also needs coroutineRunner (any MonoBehaviour) because TakeScreenshot method uses WaitForEndOfFrame inside. See more information: https://github.com/Cysharp/UniTask#ienumeratortounitask-limitation

### Editor Extensions
Expand Down Expand Up @@ -479,6 +489,10 @@ make create_project
UNITY_VERSION=2019.4.40f1 make -k test
```

> **Warning**
> Required install packages for running tests (when adding to the `testables` in package.json), as follows:
> - [UniTask](https://github.com/Cysharp/UniTask) package v2.3.3 or later


## Release workflow
Expand Down
3 changes: 2 additions & 1 deletion Tests/Runtime/TestHelper.Tests.asmdef
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"references": [
"UnityEngine.TestRunner",
"UnityEditor.TestRunner",
"TestHelper"
"TestHelper",
"UniTask"
],
"includePlatforms": [],
"excludePlatforms": [],
Expand Down
27 changes: 27 additions & 0 deletions Tests/Runtime/Utils/ScreenshotHelperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

using System.Collections;
using System.IO;
using System.Threading.Tasks;
using Cysharp.Threading.Tasks;
using NUnit.Framework;
using TestHelper.Attributes;
using UnityEngine;
Expand Down Expand Up @@ -187,5 +189,30 @@ public IEnumerator TakeScreenshot_SpecifySuperSizeAndStereoCaptureMode_NotWork()
yield return ScreenshotHelper.TakeScreenshot();
Assert.That(path, Does.Exist);
}

[Test]
[LoadScene(TestScene)]
public async Task TakeScreenshot_FromAsyncTest()
{
var path = Path.Combine(_defaultOutputDirectory, $"{nameof(TakeScreenshot_FromAsyncTest)}.png");
if (File.Exists(path))
{
File.Delete(path);
}

Assume.That(path, Does.Not.Exist);

var coroutineRunner = new GameObject().AddComponent<CoroutineRunner>();
await ScreenshotHelper.TakeScreenshot().ToUniTask(coroutineRunner);
// Note: UniTask is required to be used from the async test.
// And also needs coroutineRunner (any MonoBehaviour) because TakeScreenshot method uses WaitForEndOfFrame inside.
// See more information: https://github.com/Cysharp/UniTask#ienumeratortounitask-limitation

Assert.That(path, Does.Exist);
}

private class CoroutineRunner : MonoBehaviour
{
}
}
}
43 changes: 38 additions & 5 deletions Tests/RuntimeInternals/ScreenshotHelperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@

using System.Collections;
using System.IO;
using System.Threading.Tasks;
using Cysharp.Threading.Tasks;
using NUnit.Framework;
using TestHelper.Attributes;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.TestTools;
using UnityEngine.UI;
#if UNITY_EDITOR
using UnityEditor.SceneManagement;
#endif

namespace TestHelper.RuntimeInternals
{
[TestFixture]
[GameViewResolution(GameViewResolution.VGA)]
[UnityPlatform(RuntimePlatform.OSXEditor, RuntimePlatform.WindowsEditor, RuntimePlatform.LinuxEditor)]
public class ScreenshotHelperTest
{
private const string TestScene = "Packages/com.nowsprinting.test-helper/Tests/Scenes/ScreenshotTest.unity";
Expand All @@ -23,9 +28,14 @@ public class ScreenshotHelperTest

private Text _text;

[SetUp]
public void SetUp()
[UnitySetUp]
public IEnumerator SetUp()
{
#if UNITY_EDITOR
yield return EditorSceneManager.LoadSceneAsyncInPlayMode(
TestScene,
new LoadSceneParameters(LoadSceneMode.Single));
#endif
var textObject = GameObject.Find("Text");
Assume.That(textObject, Is.Not.Null);

Expand All @@ -34,7 +44,6 @@ public void SetUp()
}

[UnityTest]
[LoadScene(TestScene)]
public IEnumerator TakeScreenshot_SaveToDefaultPath()
{
var path = Path.Combine(_defaultOutputDirectory, $"{nameof(TakeScreenshot_SaveToDefaultPath)}.png");
Expand All @@ -50,5 +59,29 @@ public IEnumerator TakeScreenshot_SaveToDefaultPath()
Assert.That(path, Does.Exist);
Assert.That(File.ReadAllBytes(path), Has.Length.GreaterThan(FileSizeThreshold));
}

[Test]
public async Task TakeScreenshot_FromAsyncTest()
{
var path = Path.Combine(_defaultOutputDirectory, $"{nameof(TakeScreenshot_FromAsyncTest)}.png");
if (File.Exists(path))
{
File.Delete(path);
}

Assume.That(path, Does.Not.Exist);

var coroutineRunner = new GameObject().AddComponent<CoroutineRunner>();
await ScreenshotHelper.TakeScreenshot().ToUniTask(coroutineRunner);
// Note: UniTask is required to be used from the async test.
// And also needs coroutineRunner (any MonoBehaviour) because TakeScreenshot method uses WaitForEndOfFrame inside.
// See more information: https://github.com/Cysharp/UniTask#ienumeratortounitask-limitation

Assert.That(path, Does.Exist);
}

private class CoroutineRunner : MonoBehaviour
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"references": [
"UnityEngine.TestRunner",
"UnityEditor.TestRunner",
"TestHelper",
"TestHelper.RuntimeInternals"
"TestHelper.RuntimeInternals",
"UniTask"
],
"includePlatforms": [],
"excludePlatforms": [],
Expand Down

0 comments on commit e567c6a

Please sign in to comment.