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 end-to-end test framework inspired from iOS interactive tests #8110
Open
Mindfulplays
wants to merge
1
commit into
MonoGame:develop
Choose a base branch
from
Mindfulplays:e2e-test
base: develop
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,847
−2,548
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// MonoGame - Copyright (C) The MonoGame Team | ||
// This file is subject to the terms and conditions defined in | ||
// file 'LICENSE.txt', which is part of this source code package. | ||
|
||
namespace MonoGame.InteractiveTests | ||
{ | ||
/// <summary> | ||
/// Defines test app categories that is provided via a <code>class</code> | ||
/// attribute as well as a human-readable string shown in a UI or provided | ||
/// via command line arg. | ||
/// </summary> | ||
static class Categories | ||
{ | ||
public const string Default = General; | ||
|
||
public const string General = "General"; | ||
public const string Meta = "Meta Tests"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// MonoGame - Copyright (C) The MonoGame Team | ||
// This file is subject to the terms and conditions defined in | ||
// file 'LICENSE.txt', which is part of this source code package. | ||
|
||
using System.Collections.Generic; | ||
|
||
namespace MonoGame.InteractiveTests | ||
{ | ||
/// <summary> | ||
/// Allows tests to output console messages including spammy messages that | ||
/// may be throttled. | ||
/// | ||
/// On various platforms, this may be available via console output or via | ||
/// a special console viewer (Console app on Mac; `adb logcat` on Android etc). | ||
/// </summary> | ||
public partial class GameDebug | ||
{ | ||
/// <summary>Output a single console message.</summary> | ||
public static void C(string message) | ||
{ | ||
System.Console.WriteLine($"MGDBG: {message}"); | ||
} | ||
|
||
/// <summary>Output an error message to the console.</summary> | ||
public static void E(string message) | ||
{ | ||
System.Console.WriteLine($"****ERROR*****:MGDBG: {message}"); | ||
} | ||
|
||
/// <summary>Maintains the spam message counts to prevent spamming the console.</summary> | ||
private record MessageCount(int Count) | ||
{ | ||
public int Count { get; set; } = Count; | ||
} | ||
private static readonly Dictionary<string, MessageCount> MESSAGES_COUNTS_ = new(); | ||
|
||
/// <summary>Use this to output spammy messages.</summary> | ||
public static void Spam(string message, int maxNumTimes = 10) | ||
{ | ||
if (!MESSAGES_COUNTS_.TryGetValue(message, out var numTimes)) | ||
{ | ||
numTimes = new(0); | ||
MESSAGES_COUNTS_.Add(message, numTimes); | ||
} | ||
|
||
if (numTimes.Count >= maxNumTimes) { return; } | ||
|
||
System.Console.WriteLine($"MGDBG: {message} ...#{numTimes.Count}"); | ||
numTimes.Count++; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// MonoGame - Copyright (C) The MonoGame Team | ||
// This file is subject to the terms and conditions defined in | ||
// file 'LICENSE.txt', which is part of this source code package. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.Xna.Framework; | ||
using MonoGame.Framework.Utilities; | ||
|
||
namespace MonoGame.InteractiveTests | ||
{ | ||
/// <summary> | ||
/// Manages creating of interactive test(s) instance(s). | ||
/// </summary> | ||
public class InteractiveTest | ||
{ | ||
public static bool TryCreateFrom(Type type, out InteractiveTest test) | ||
{ | ||
test = null; | ||
if (!typeof(TestGame).IsAssignableFrom(type)) { return false; } | ||
|
||
var attrs = type.GetCustomAttributes(typeof(InteractiveTestAttribute), false); | ||
if (attrs.Length == 0) { return false; } | ||
|
||
var attr = (InteractiveTestAttribute)attrs[0]; | ||
|
||
test = new InteractiveTest( | ||
type, attr.Name ?? type.Name, attr.Category ?? Categories.Default, attr.Platforms); | ||
return true; | ||
} | ||
|
||
private InteractiveTest(Type type, string name, string category, MonoGamePlatform[] platforms) | ||
{ | ||
_type = type; | ||
_name = name; | ||
_category = category; | ||
_platforms = platforms; | ||
} | ||
|
||
private readonly Type _type; | ||
public Type Type { get { return _type; } } | ||
|
||
private readonly string _name; | ||
public string Name { get { return _name; } } | ||
|
||
private readonly string _category; | ||
public string Category { get { return _category; } } | ||
|
||
private readonly MonoGamePlatform[] _platforms; | ||
public MonoGamePlatform[] Platforms { get { return _platforms; } } | ||
|
||
public Game Create() | ||
{ | ||
return (Game)Activator.CreateInstance(_type); | ||
} | ||
|
||
public bool MatchesPlatform(MonoGamePlatform runtimePlatform) | ||
{ | ||
// Empty array matches everything. | ||
if (_platforms.Length == 0) { return true; } | ||
|
||
foreach (var testPlatform in _platforms) | ||
{ | ||
if (testPlatform == runtimePlatform) { return true; } | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// MonoGame - Copyright (C) The MonoGame Team | ||
// This file is subject to the terms and conditions defined in | ||
// file 'LICENSE.txt', which is part of this source code package. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using MonoGame.Framework.Utilities; | ||
|
||
namespace MonoGame.InteractiveTests | ||
{ | ||
/// <summary> | ||
/// Attribute specified on test classes that are automatically | ||
/// discovered and shown/provided via some UI / command-line arg. | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)] | ||
public class InteractiveTestAttribute : Attribute | ||
{ | ||
public InteractiveTestAttribute(string name, string category, | ||
MonoGamePlatform[] platforms = null) | ||
{ | ||
_name = name; | ||
_category = category; | ||
|
||
// Empty array matches everything. | ||
if (platforms == null) { platforms = new MonoGamePlatform[] { }; } | ||
|
||
_platforms = platforms; | ||
} | ||
|
||
/// <summary>Human-readable name of the test</summary> | ||
private readonly string _name; | ||
|
||
public string Name { get { return _name; } } | ||
|
||
/// <summary>Category of the test. See <see cref="Categories"/></summary> | ||
private readonly string _category; | ||
|
||
public string Category { get { return _category; } } | ||
|
||
/// <summary> | ||
/// Supported platforms that this test can run on (empty array | ||
/// allows running on any platform. | ||
/// </summary> | ||
private readonly MonoGamePlatform[] _platforms; | ||
|
||
public MonoGamePlatform[] Platforms { get { return _platforms; } } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// MonoGame - Copyright (C) The MonoGame Team | ||
// This file is subject to the terms and conditions defined in | ||
// file 'LICENSE.txt', which is part of this source code package. | ||
|
||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using Microsoft.Xna.Framework.Graphics; | ||
using MonoGame.Framework.Utilities; | ||
|
||
namespace MonoGame.InteractiveTests | ||
{ | ||
/// <summary> | ||
/// Creates a <see cref="InteractiveTest"/> from applicable types in our binary. | ||
/// Also allows filtering of tests based on platforms/command-line args. | ||
/// </summary> | ||
public class InteractiveTests | ||
{ | ||
private readonly List<InteractiveTest> _interactiveTests = new(); | ||
|
||
private readonly List<InteractiveTest> _filteredTests = new(); | ||
|
||
public InteractiveTests() | ||
{ | ||
_interactiveTests.Clear(); | ||
var assembly = Assembly.GetExecutingAssembly(); | ||
foreach (var type in assembly.GetTypes()) | ||
{ | ||
InteractiveTest test; | ||
if (!InteractiveTest.TryCreateFrom(type, out test)) { continue; } | ||
|
||
if (test.MatchesPlatform(PlatformInfo.MonoGamePlatform)) { _interactiveTests.Add(test); } | ||
} | ||
|
||
GameDebug.C($"--Discovered {_interactiveTests.Count} tests."); | ||
} | ||
|
||
public IReadOnlyList<InteractiveTest> Tests { get { return _interactiveTests; } } | ||
|
||
/// <summary> | ||
/// Parses the passed-in arg and returns an `InteractiveTest` game. See HelpStr for more details. | ||
/// </summary> | ||
public IReadOnlyList<InteractiveTest> Parse(string[] args) | ||
{ | ||
_filteredTests.Clear(); | ||
if (args == null || args.Length == 0) { return _filteredTests; } | ||
|
||
foreach (var test in _interactiveTests) | ||
{ | ||
foreach (var testName in args) | ||
{ | ||
var name = testName.ToLower(); | ||
if (test.Category.ToLower().Contains(name)) { _filteredTests.Add(test); } | ||
else if (test.Name.ToLower().Contains(name)) { _filteredTests.Add(test); } | ||
} | ||
} | ||
|
||
return _filteredTests; | ||
} | ||
|
||
public string HelpStr() | ||
{ | ||
var testStr = ""; | ||
foreach (var test in _interactiveTests) { testStr += $"{test.Name}\n"; } | ||
|
||
return $@"Interactive tests available: | ||
{testStr} | ||
"; | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
Tests/Interactive/Common/MonoGame.Interactive.Common.projitems
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<PropertyGroup> | ||
<MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects> | ||
<HasSharedItems>true</HasSharedItems> | ||
</PropertyGroup> | ||
<PropertyGroup Label="Configuration"> | ||
<Import_RootNamespace>MonoGame</Import_RootNamespace> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Compile Include="$(MSBuildThisFileDirectory)\**\*.cs" /> | ||
</ItemGroup> | ||
</Project> |
11 changes: 11 additions & 0 deletions
11
Tests/Interactive/Common/MonoGame.Interactive.Common.shproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<PropertyGroup Label="Globals"> | ||
<ProjectGuid>085B407C-726C-43FE-BB55-64E2B6D143FE</ProjectGuid> | ||
</PropertyGroup> | ||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> | ||
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.Default.props" /> | ||
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.props" /> | ||
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.CSharp.targets" /> | ||
<Import Project="MonoGame.Interactive.Common.projitems" Label="Shared" /> | ||
</Project> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we get more descriptive method names for these?
LogError
orLogDebug