Skip to content

Commit

Permalink
Merge pull request #46 from nowsprinting/chore/refactor_tests
Browse files Browse the repository at this point in the history
Refactor tests
  • Loading branch information
nowsprinting committed Nov 3, 2023
2 parents 8e1d750 + a76b98a commit 9a5514a
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 95 deletions.
10 changes: 5 additions & 5 deletions Tests/Runtime/Attributes/GizmosShowOnGameViewAttributeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,23 @@ public IEnumerator UnitySetUp()
[UnityTearDown]
public IEnumerator UnityTearDown()
{
yield return ScreenshotHelper.TakeScreenshot(); // Take screenshot before hide Gizmos.
yield return ScreenshotHelper.TakeScreenshot(); // Take screenshot before revert Gizmos.
}

[Test]
[CreateScene(camera: true, light: true)]
[GizmosShowOnGameView]
public void Attach_True_ShowGizmos()
{
// verify screenshot.
// See the screenshot yourself! Be a witness!!
}

[Test]
[CreateScene(camera: true, light: true)]
[GizmosShowOnGameView(false)]
public void Attach_False_HideGizmos()
{
// verify screenshot.
// See the screenshot yourself! Be a witness!!
}

[Test]
Expand All @@ -59,7 +59,7 @@ public void Attach_False_HideGizmos()
public async Task AttachToAsyncTest_ShowGizmos()
{
await Task.Yield();
// verify screenshot.
// See the screenshot yourself! Be a witness!!
}

[UnityTest]
Expand All @@ -68,7 +68,7 @@ public async Task AttachToAsyncTest_ShowGizmos()
public IEnumerator AttachToUnityTest_ShowGizmos()
{
yield return null;
// verify screenshot.
// See the screenshot yourself! Be a witness!!
}
}
}
156 changes: 140 additions & 16 deletions Tests/Runtime/Attributes/TakeScreenshotAttributeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ namespace TestHelper.Attributes
public class TakeScreenshotAttributeTest
{
private const string TestScene = "Packages/com.nowsprinting.test-helper/Tests/Scenes/ScreenshotTest.unity";
private const int FileSizeThreshold = 5441; // VGA size solid color file size
private const int FileSizeThreshold2X = 100 * 1024; // Normal size is 80 to 90KB

private readonly string _defaultOutputDirectory =
Path.Combine(Application.persistentDataPath, "TestHelper", "Screenshots");
Expand All @@ -35,11 +37,11 @@ public void SetUp()
[Test, Order(0)]
[LoadScene(TestScene)]
[TakeScreenshot]
public void Attach_TakeScreenshotAndSaveToDefaultPath()
public void Attach_SaveScreenshotToDefaultPath()
{
var path = Path.Combine(
_defaultOutputDirectory,
$"{nameof(Attach_TakeScreenshotAndSaveToDefaultPath)}.png");
$"{nameof(Attach_SaveScreenshotToDefaultPath)}.png");
if (File.Exists(path))
{
File.Delete(path);
Expand All @@ -51,22 +53,23 @@ public void Attach_TakeScreenshotAndSaveToDefaultPath()
}

[Test, Order(1)]
public void Attach_TakeScreenshotAndSaveToDefaultPath_AfterRunningTest_ExistFile()
public void Attach_SaveScreenshotToDefaultPath_ExistFile()
{
var path = Path.Combine(
_defaultOutputDirectory,
$"{nameof(Attach_TakeScreenshotAndSaveToDefaultPath)}.png");
$"{nameof(Attach_SaveScreenshotToDefaultPath)}.png");
Assert.That(path, Does.Exist);
Assert.That(File.ReadAllBytes(path), Has.Length.GreaterThan(FileSizeThreshold));
}

[Test, Order(0)]
[LoadScene(TestScene)]
[TakeScreenshot]
public async Task AttachToAsyncTest_TakeScreenshotAndSaveToDefaultPath()
public async Task AttachToAsyncTest_SaveScreenshotToDefaultPath()
{
var path = Path.Combine(
_defaultOutputDirectory,
$"{nameof(AttachToAsyncTest_TakeScreenshotAndSaveToDefaultPath)}.png");
$"{nameof(AttachToAsyncTest_SaveScreenshotToDefaultPath)}.png");
if (File.Exists(path))
{
File.Delete(path);
Expand All @@ -79,22 +82,23 @@ public async Task AttachToAsyncTest_TakeScreenshotAndSaveToDefaultPath()
}

[Test, Order(1)]
public void AttachToAsyncTest_TakeScreenshotAndSaveToDefaultPath_AfterRunningTest_ExistFile()
public void AttachToAsyncTest_SaveScreenshotToDefaultPath_ExistFile()
{
var path = Path.Combine(
_defaultOutputDirectory,
$"{nameof(AttachToAsyncTest_TakeScreenshotAndSaveToDefaultPath)}.png");
$"{nameof(AttachToAsyncTest_SaveScreenshotToDefaultPath)}.png");
Assert.That(path, Does.Exist);
Assert.That(File.ReadAllBytes(path), Has.Length.GreaterThan(FileSizeThreshold));
}

[UnityTest, Order(0)]
[LoadScene(TestScene)]
[TakeScreenshot]
public IEnumerator AttachToUnityTest_TakeScreenshotAndSaveToDefaultPath()
public IEnumerator AttachToUnityTest_SaveScreenshotToDefaultPath()
{
var path = Path.Combine(
_defaultOutputDirectory,
$"{nameof(AttachToUnityTest_TakeScreenshotAndSaveToDefaultPath)}.png");
$"{nameof(AttachToUnityTest_SaveScreenshotToDefaultPath)}.png");
if (File.Exists(path))
{
File.Delete(path);
Expand All @@ -107,21 +111,82 @@ public IEnumerator AttachToUnityTest_TakeScreenshotAndSaveToDefaultPath()
}

[Test, Order(1)]
public void AttachToUnityTest_TakeScreenshotAndSaveToDefaultPath_AfterRunningTest_ExistFile()
public void AttachToUnityTest_SaveScreenshotToDefaultPath_ExistFile()
{
var path = Path.Combine(
_defaultOutputDirectory,
$"{nameof(AttachToUnityTest_TakeScreenshotAndSaveToDefaultPath)}.png");
$"{nameof(AttachToUnityTest_SaveScreenshotToDefaultPath)}.png");
Assert.That(path, Does.Exist);
Assert.That(File.ReadAllBytes(path), Has.Length.GreaterThan(FileSizeThreshold));
}

[Test, Order(0)]
[LoadScene(TestScene)]
[TakeScreenshot]
public void AttachToParameterizedTest_SaveAllScreenshots(
[Values(0, 1)] int i,
[Values(2, 3)] int j)
{
var path = Path.Combine(
_defaultOutputDirectory,
$"{nameof(AttachToParameterizedTest_SaveAllScreenshots)}_{i}-{j}_.png");
if (File.Exists(path))
{
File.Delete(path);
}

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

// Take screenshot after running the test.
}

[Test, Order(1)]
public void AttachToParameterizedTest_SaveAllScreenshots_ExistFile(
[Values(0, 1)] int i,
[Values(2, 3)] int j)
{
var path = Path.Combine(
_defaultOutputDirectory,
$"{nameof(AttachToParameterizedTest_SaveAllScreenshots)}_{i}-{j}_.png");
Assert.That(path, Does.Exist);
}

private const string SpecifyRelativeDirectory = "Logs/TestHelper/Screenshots";

[Test, Order(0)]
[LoadScene(TestScene)]
[TakeScreenshot(directory: SpecifyRelativeDirectory)]
public void AttachWithDirectory_SaveScreenshotToSpecifyPath()
{
var path = Path.Combine(
Path.GetFullPath(SpecifyRelativeDirectory),
$"{nameof(AttachWithDirectory_SaveScreenshotToSpecifyPath)}.png");
if (File.Exists(path))
{
File.Delete(path);
}

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

// Take screenshot after running the test.
}

[Test, Order(1)]
public void AttachWithDirectory_SaveScreenshotToSpecifyPath_ExistFile()
{
var path = Path.Combine(
Path.GetFullPath(SpecifyRelativeDirectory),
$"{nameof(AttachWithDirectory_SaveScreenshotToSpecifyPath)}.png");
Assert.That(path, Does.Exist);
}

private const string SpecifyFilename =
nameof(AttachWithFilename_TakeScreenshotAndSaveToSpecifyPath) + "_Specified.png";
nameof(AttachWithFilename_SaveScreenshotToSpecifyPath) + "_Specified.png";

[Test, Order(0)]
[LoadScene(TestScene)]
[TakeScreenshot(filename: SpecifyFilename)]
public void AttachWithFilename_TakeScreenshotAndSaveToSpecifyPath()
public void AttachWithFilename_SaveScreenshotToSpecifyPath()
{
var path = Path.Combine(
_defaultOutputDirectory,
Expand All @@ -137,14 +202,72 @@ public void AttachWithFilename_TakeScreenshotAndSaveToSpecifyPath()
}

[Test, Order(1)]
public void AttachWithFilename_TakeScreenshotAndSaveToSpecifyPath_AfterRunningTest_ExistFile()
public void AttachWithFilename_SaveScreenshotToSpecifyPath_ExistFile()
{
var path = Path.Combine(
_defaultOutputDirectory,
SpecifyFilename);
Assert.That(path, Does.Exist);
}

[Test, Order(0)]
[LoadScene(TestScene)]
[TakeScreenshot(superSize: 2)]
public void AttachWithSuperSize_SaveSuperSizeScreenshot()
{
var path = Path.Combine(
_defaultOutputDirectory,
$"{nameof(AttachWithSuperSize_SaveSuperSizeScreenshot)}.png");
if (File.Exists(path))
{
File.Delete(path);
}

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

// Take screenshot after running the test.
}

[Test, Order(1)]
public void AttachWithSuperSize_SaveSuperSizeScreenshot_ExistFile()
{
var path = Path.Combine(
_defaultOutputDirectory,
$"{nameof(AttachWithSuperSize_SaveSuperSizeScreenshot)}.png");
Assert.That(path, Does.Exist);
Assert.That(File.ReadAllBytes(path), Has.Length.GreaterThan(FileSizeThreshold2X));
}

[Test, Order(0)]
[LoadScene(TestScene)]
[TakeScreenshot(stereoCaptureMode: ScreenCapture.StereoScreenCaptureMode.BothEyes)]
public void AttachWithStereoCaptureMode_SaveStereoScreenshot()
{
var path = Path.Combine(
_defaultOutputDirectory,
$"{nameof(AttachWithStereoCaptureMode_SaveStereoScreenshot)}.png");
if (File.Exists(path))
{
File.Delete(path);
}

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

// Take screenshot after running the test.
}

[Test, Order(1)]
public void AttachWithStereoCaptureMode_SaveStereoScreenshot_ExistFile()
{
var path = Path.Combine(
_defaultOutputDirectory,
$"{nameof(AttachWithStereoCaptureMode_SaveStereoScreenshot)}.png");
Assert.That(path, Does.Exist);
// Is it a stereo screenshot? See for yourself! Be a witness!!
// Note: Require stereo rendering settings.
// See: https://docs.unity3d.com/Manual/SinglePassStereoRendering.html
}

private class GizmoDemo : MonoBehaviour
{
private void OnDrawGizmos()
Expand All @@ -168,12 +291,13 @@ public void AttachWithGizmos_TakeScreenshotWithGizmos()

[Test, Order(1)]
[UnityPlatform(RuntimePlatform.OSXEditor, RuntimePlatform.WindowsEditor, RuntimePlatform.LinuxEditor)]
public void AttachWithGizmos_TakeScreenshotWithGizmos_AfterRunningTest_ExistFile()
public void AttachWithGizmos_TakeScreenshotWithGizmos_ExistFile()
{
var path = Path.Combine(
_defaultOutputDirectory,
$"{nameof(AttachWithGizmos_TakeScreenshotWithGizmos)}.png");
Assert.That(path, Does.Exist);
// Is Gizmos shown in the screenshot? See for yourself! Be a witness!!
}
}
}
77 changes: 3 additions & 74 deletions Tests/RuntimeInternals/ScreenshotHelperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,27 +56,10 @@ public IEnumerator TakeScreenshot_SaveToDefaultPath()
}

[UnityTest]
public IEnumerator TakeScreenshot_SpecifyRelativeDirectory_SaveToSpecifyPath()
public IEnumerator TakeScreenshot_Multiple_SaveToEachSpecifyPaths()
{
const string RelativePath = "Logs/TestHelper/Screenshots";
var path = Path.Combine(Path.GetFullPath(RelativePath),
$"{nameof(TakeScreenshot_SpecifyRelativeDirectory_SaveToSpecifyPath)}.png");
if (File.Exists(path))
{
File.Delete(path);
}

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

yield return ScreenshotHelper.TakeScreenshot(directory: RelativePath);
Assert.That(path, Does.Exist);
}

[UnityTest]
public IEnumerator TakeScreenshot_SpecifyFilename_SaveToSpecifyPath()
{
const string Filename1 = "TakeScreenshot_SpecifyFilename1.png";
const string Filename2 = "TakeScreenshot_SpecifyFilename2.png";
const string Filename1 = "TakeScreenshot_Multiple_SpecifyFilename1.png";
const string Filename2 = "TakeScreenshot_Multiple_SpecifyFilename2.png";

var path1 = Path.Combine(_defaultOutputDirectory, Filename1);
if (File.Exists(path1))
Expand Down Expand Up @@ -104,42 +87,6 @@ public IEnumerator TakeScreenshot_SpecifyFilename_SaveToSpecifyPath()
Assert.That(path2, Does.Exist);
}

[UnityTest]
public IEnumerator TakeScreenshot_SpecifySuperSize_SaveSuperSizeFile()
{
var path = Path.Combine(_defaultOutputDirectory,
$"{nameof(TakeScreenshot_SpecifySuperSize_SaveSuperSizeFile)}.png");
if (File.Exists(path))
{
File.Delete(path);
}

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

yield return ScreenshotHelper.TakeScreenshot(superSize: 2);
Assert.That(path, Does.Exist);
// Please visually check the file.
}

[UnityTest]
public IEnumerator TakeScreenshot_SpecifyStereoCaptureMode_SaveStereoFile()
{
var path = Path.Combine(_defaultOutputDirectory,
$"{nameof(TakeScreenshot_SpecifyStereoCaptureMode_SaveStereoFile)}.png");
if (File.Exists(path))
{
File.Delete(path);
}

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

yield return ScreenshotHelper.TakeScreenshot(
stereoCaptureMode: ScreenCapture.StereoScreenCaptureMode.BothEyes);
Assert.That(path, Does.Exist);
// Require stereo rendering settings.
// See: https://docs.unity3d.com/Manual/SinglePassStereoRendering.html
}

[UnityTest]
public IEnumerator TakeScreenshot_SpecifySuperSizeAndStereoCaptureMode_NotWork()
{
Expand All @@ -149,24 +96,6 @@ public IEnumerator TakeScreenshot_SpecifySuperSizeAndStereoCaptureMode_NotWork()
LogAssert.Expect(LogType.Error, "superSize and stereoCaptureMode cannot be specified at the same time.");
}

[UnityTest]
public IEnumerator TakeScreenshot_Parameterized_SaveAllFiles(
[Values(0, 1)] int i,
[Values(2, 3)] int j)
{
var path = Path.Combine(_defaultOutputDirectory,
$"{nameof(TakeScreenshot_Parameterized_SaveAllFiles)}_{i}-{j}_.png");
if (File.Exists(path))
{
File.Delete(path);
}

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

yield return ScreenshotHelper.TakeScreenshot();
Assert.That(path, Does.Exist);
}

[Test]
public async Task TakeScreenshot_FromAsyncTest()
{
Expand Down

0 comments on commit 9a5514a

Please sign in to comment.