Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add command line argument to specify screenshot save directory #63

Merged
merged 5 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ jobs:
unityVersion: ${{ matrix.unityVersion }} # Default is `auto`
checkName: test result (${{ matrix.unityVersion }})
projectPath: ${{ env.CREATED_PROJECT_PATH }}
customParameters: -testCategory "!IgnoreCI"
customParameters: -testCategory "!IgnoreCI" -testHelperScreenshotDirectory Screenshots
coverageOptions: generateAdditionalMetrics;generateTestReferences;generateHtmlReport;generateAdditionalReports;dontClear;assemblyFilters:${{ env.assembly_filters }}
# see: https://docs.unity3d.com/Packages/com.unity.testtools.codecoverage@1.2/manual/CoverageBatchmode.html
env:
Expand All @@ -113,6 +113,7 @@ jobs:
path: |
${{ steps.test.outputs.artifactsPath }}
${{ steps.test.outputs.coveragePath }}
Screenshots
if: always()

notify:
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ $(TEST_CATEGORY) \
$(TEST_FILTER) \
$(ASSEMBLY_NAMES) \
-testPlatform $(TEST_PLATFORM) \
-testResults $(LOG_DIR)/test_$(TEST_PLATFORM)_results.xml
-testResults $(LOG_DIR)/test_$(TEST_PLATFORM)_results.xml \
-testHelperScreenshotDirectory $(LOG_DIR)/Screenshots
endef

define test
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ public class MyTestClass

Default save path is "`Application.persistentDataPath`/TestHelper/Screenshots/`CurrentTest.Name`.png".
You can specify the save directory and/or filename by arguments.
Directory can also be specified by command line arguments `-testHelperScreenshotDirectory`.

This attribute can attached to test method only.
Can be used with sync Test, async Test, and UnityTest.
Expand Down Expand Up @@ -376,6 +377,7 @@ public class MyTestClass
Default save path is "`Application.persistentDataPath`/TestHelper/Screenshots/`CurrentTest.Name`.png".
(Replace `CurrentTest.Name` to caller method name when run in runtime context.)
You can specify the save directory and/or filename by arguments.
Directory can also be specified by command line arguments `-testHelperScreenshotDirectory`.

Usage:

Expand Down
12 changes: 8 additions & 4 deletions Runtime/Attributes/TakeScreenshotAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2023 Koji Hasegawa.
// Copyright (c) 2023-2024 Koji Hasegawa.
// This software is released under the MIT License.

using System;
Expand Down Expand Up @@ -26,7 +26,7 @@ public class TakeScreenshotAttribute : NUnitAttribute, IOuterUnityTestAction
/// <summary>
/// Take a screenshot and save it to file after running the test.
/// Default save path is $"{Application.persistentDataPath}/TestHelper/Screenshots/{CurrentTest.Name}.png".
/// If you want to take screenshots at any time, use the <c>ScreenshotHelper</c> class.
/// If you want to take screenshots at any time, use the <c>TestHelper.RuntimeInternals.ScreenshotHelper</c> class.
/// </summary>
/// <remarks>
/// Limitations:
Expand All @@ -35,8 +35,12 @@ public class TakeScreenshotAttribute : NUnitAttribute, IOuterUnityTestAction
/// <br/>
/// Using <c>ScreenCapture.CaptureScreenshotAsTexture</c> internally.
/// </remarks>
/// <param name="directory">Directory to save screenshots.</param>
/// <param name="filename">Filename to store screenshot.</param>
/// <param name="directory">Directory to save screenshots.
/// If omitted, the directory specified by command line argument "-testHelperScreenshotDirectory" is used.
/// If the command line argument is also omitted, <c>Application.persistentDataPath</c> + "/TestHelper/Screenshots/" is used.</param>
/// <param name="filename">Filename to store screenshot.
/// Default filename is <c>CurrentTest.Name</c> + ".png" when run in test-framework context.
/// Using caller method name when run in runtime context.</param>
/// <param name="superSize">The factor to increase resolution with.</param>
/// <param name="stereoCaptureMode">The eye texture to capture when stereo rendering is enabled.</param>
/// <param name="gizmos">True: show Gizmos on GameView</param>
Expand Down
58 changes: 58 additions & 0 deletions RuntimeInternals/CommandLineArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright (c) 2023-2024 Koji Hasegawa.
// This software is released under the MIT License.

using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

namespace TestHelper.RuntimeInternals
{
/// <summary>
/// Manage command line arguments for test-helper.
/// </summary>
public static class CommandLineArgs
{
internal static Dictionary<string, string> DictionaryFromCommandLineArgs(string[] args)
{
var result = new Dictionary<string, string>();
for (var i = 0; i < args.Length; i++)
{
if (args[i].StartsWith("-"))
{
var key = args[i];
var value = string.Empty;
if (i + 1 < args.Length && !args[i + 1].StartsWith("-"))
{
value = args[i + 1];
i++;
}

result[key] = value;
}
}

return result;
}

/// <summary>
/// Screenshot save directory.
/// Returns <c>Application.persistentDataPath</c> + "/TestHelper/Screenshots/" if not specified.
/// </summary>
/// <returns></returns>
public static string GetScreenshotDirectory(string[] args = null)
{
const string ScreenshotDirectoryKey = "-testHelperScreenshotDirectory";

try
{
args = args ?? Environment.GetCommandLineArgs();
return DictionaryFromCommandLineArgs(args)[ScreenshotDirectoryKey];
}
catch (KeyNotFoundException)
{
return Path.Combine(Application.persistentDataPath, "TestHelper", "Screenshots");
}
}
}
}
3 changes: 3 additions & 0 deletions RuntimeInternals/CommandLineArgs.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 6 additions & 8 deletions RuntimeInternals/ScreenshotHelper.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// Copyright (c) 2023 Koji Hasegawa.
// Copyright (c) 2023-2024 Koji Hasegawa.
// This software is released under the MIT License.

using System.Collections;
using System.IO;
using System.Runtime.CompilerServices;
using System.Threading;
using UnityEngine;
#if UNITY_INCLUDE_TESTS
using NUnit.Framework;
Expand All @@ -18,10 +17,7 @@ namespace TestHelper.RuntimeInternals
/// </summary>
public static class ScreenshotHelper
{
private static string DefaultDirectoryPath()
{
return Path.Combine(Application.persistentDataPath, "TestHelper", "Screenshots");
}
private static string DefaultDirectoryPath => CommandLineArgs.GetScreenshotDirectory();

private static string DefaultFilename(string callerMemberName)
{
Expand Down Expand Up @@ -50,7 +46,9 @@ private static string DefaultFilename(string callerMemberName)
/// <br/>
/// Using <c>ScreenCapture.CaptureScreenshotAsTexture</c> internally.
/// </remarks>
/// <param name="directory">Directory to save screenshots. Default save path is <c>Application.persistentDataPath</c> + "/TestHelper/Screenshots/".</param>
/// <param name="directory">Directory to save screenshots.
/// If omitted, the directory specified by command line argument "-testHelperScreenshotDirectory" is used.
/// If the command line argument is also omitted, <c>Application.persistentDataPath</c> + "/TestHelper/Screenshots/" is used.</param>
/// <param name="filename">Filename to store screenshot.
/// Default filename is <c>CurrentTest.Name</c> + ".png" when run in test-framework context.
/// Using caller method name when run in runtime context.</param>
Expand All @@ -76,7 +74,7 @@ private static string DefaultFilename(string callerMemberName)
}
else
{
directory = DefaultDirectoryPath(); // Not apply specific directory when running on player
directory = DefaultDirectoryPath; // Not apply specific directory when running on player
}

Directory.CreateDirectory(directory);
Expand Down
5 changes: 2 additions & 3 deletions Tests/Runtime/Attributes/TakeScreenshotAttributeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.IO;
using System.Threading.Tasks;
using NUnit.Framework;
using TestHelper.RuntimeInternals;
using UnityEngine;
using UnityEngine.TestTools;
using UnityEngine.UI;
Expand All @@ -18,9 +19,7 @@ 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");
private readonly string _defaultOutputDirectory = CommandLineArgs.GetScreenshotDirectory();

private Text _text;

Expand Down
43 changes: 43 additions & 0 deletions Tests/RuntimeInternals/CommandLineArgsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) 2023-2024 Koji Hasegawa.
// This software is released under the MIT License.

using System;
using System.IO;
using NUnit.Framework;
using UnityEngine;

namespace TestHelper.RuntimeInternals
{
[TestFixture]
public class CommandLineArgsTest
{
[Test]
public void DictionaryFromCommandLineArgs()
{
var args = new[] { "-flag1", "-key1", "value1", "-flag2" };
var expected = new System.Collections.Generic.Dictionary<string, string>
{
{ "-flag1", string.Empty }, //
{ "-key1", "value1" }, //
{ "-flag2", string.Empty },
};
var actual = CommandLineArgs.DictionaryFromCommandLineArgs(args);

Assert.That(actual, Is.EqualTo(expected));
}

[Test]
public void GetScreenshotDirectory_WithArgument_GotSpecifiedDirectory()
{
var actual = CommandLineArgs.GetScreenshotDirectory(new[] { "-testHelperScreenshotDirectory", "Test" });
Assert.That(actual, Is.EqualTo("Test"));
}

[Test]
public void GetScreenshotDirectory_WithoutArgument_GotDefaultDirectory()
{
var actual = CommandLineArgs.GetScreenshotDirectory(Array.Empty<string>());
Assert.That(actual, Is.EqualTo(Path.Combine(Application.persistentDataPath, "TestHelper", "Screenshots")));
}
}
}
3 changes: 3 additions & 0 deletions Tests/RuntimeInternals/CommandLineArgsTest.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions Tests/RuntimeInternals/ScreenshotHelperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ public class ScreenshotHelperTest
{
internal const string TestScene = "Packages/com.nowsprinting.test-helper/Tests/Scenes/ScreenshotTest.unity";
private const int FileSizeThreshold = 5441; // VGA size solid color file size

private readonly string _defaultOutputDirectory =
Path.Combine(Application.persistentDataPath, "TestHelper", "Screenshots");
private readonly string _defaultOutputDirectory = CommandLineArgs.GetScreenshotDirectory();

private Text _text;

Expand Down