Skip to content

Commit

Permalink
Add missing AutroRetry attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromelaban committed Jul 30, 2019
1 parent 78304ab commit 4d43497
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace SamplesApp.UITests.CommandBar
public partial class UnoSamples_Tests : SampleControlUITestBase
{
[Test]
[AutoRetry]
[ActivePlatforms(Platform.Android, Platform.iOS)]
public void CommandBar_LongTitle_Validation()
{
Expand Down
2 changes: 2 additions & 0 deletions src/SamplesApp/SamplesApp.UITests/RuntimeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using SamplesApp.UITests.TestFramework;
using Uno.UITest.Helpers;
using Uno.UITest.Helpers.Queries;

Expand All @@ -13,6 +14,7 @@ namespace SamplesApp.UITests
public partial class RuntimeTests : SampleControlUITestBase
{
[Test]
[AutoRetry]
public void RunRuntimeTests()
{
Run("SamplesApp.Samples.UnitTests.UnitTestsPage");
Expand Down
13 changes: 13 additions & 0 deletions src/SamplesApp/SamplesApp.UITests/SampleControlUITestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ static SampleControlUITestBase()


[SetUp]
[AutoRetry]
public void BeforeEachTest()
{
ValidateAutoRetry();

// Check if the test needs to be ignore or not
// If nothing specified, it is considered as a global test
var platforms = GetActivePlatforms();
Expand Down Expand Up @@ -69,6 +72,16 @@ public void BeforeEachTest()
Helpers.App = _app;
}

private static void ValidateAutoRetry()
{
var testType = Type.GetType(TestContext.CurrentContext.Test.ClassName);
var methodInfo = testType?.GetMethod(TestContext.CurrentContext.Test.MethodName);
if (methodInfo?.GetCustomAttributes(typeof(AutoRetryAttribute), true).Length == 0 && false)
{
Assert.Fail($"The AutoRetryAttribute is not defined for this test");
}
}

private Platform[] GetActivePlatforms()
{
if(TestContext.CurrentContext.Test.Properties["ActivePlatforms"].FirstOrDefault() is Platform[] platforms)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public static IApp ColdStartApp()
: null;

public static IApp AttachToApp()
=> StartApp(alreadyRunningApp: true);
// If the retry count is set, the test already failed. Retry the test with restarting the app.
=> StartApp(alreadyRunningApp: TestContext.CurrentContext.CurrentRepeatCount == 0);

private static IApp StartApp(bool alreadyRunningApp)
{
Expand Down Expand Up @@ -96,12 +97,13 @@ private static IApp CreateBrowserApp(bool alreadyRunningApp)

private static IApp CreateAndroidApp(bool alreadyRunningApp)
{
#if DEBUG
// To set in case of Xamarin.UITest errors
//
Environment.SetEnvironmentVariable("ANDROID_HOME", @"C:\Program Files (x86)\Android\android-sdk");
Environment.SetEnvironmentVariable("JAVA_HOME", @"C:\Program Files\Android\Jdk\microsoft_dist_openjdk_1.8.0.25");
#endif
if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("ANDROID_HOME")))
{
// To set in case of Xamarin.UITest errors
//
Environment.SetEnvironmentVariable("ANDROID_HOME", @"C:\Program Files (x86)\Android\android-sdk");
Environment.SetEnvironmentVariable("JAVA_HOME", @"C:\Program Files\Android\Jdk\microsoft_dist_openjdk_1.8.0.25");
}

var androidConfig = Xamarin.UITest.ConfigureApp
.Android
Expand Down
96 changes: 89 additions & 7 deletions src/SamplesApp/SamplesApp.UITests/TestFramework/AutoRetry.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,101 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;
using NUnit.Framework.Internal.Commands;

namespace SamplesApp.UITests.TestFramework
{
/// <summary>
/// Tentative workaround for https://github.com/microsoft/appcenter/issues/723
/// Specifies that a test method should be rerun on failure up to the specified
/// maximum number of times.
/// </summary>
public class AutoRetryAttribute : RetryAttribute
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
public class AutoRetryAttribute : NUnitAttribute, IWrapSetUpTearDown
{
public AutoRetryAttribute() : base(3)
private readonly int _tryCount;

/// <summary>
/// Construct a <see cref="RetryAttribute" />
/// </summary>
/// <param name="tryCount">The maximum number of times the test should be run if it fails</param>
public AutoRetryAttribute(int tryCount = 2)
{
_tryCount = tryCount;
}

#region IRepeatTest Members

/// <summary>
/// Wrap a command and return the result.
/// </summary>
/// <param name="command">The command to be wrapped</param>
/// <returns>The wrapped command</returns>
public TestCommand Wrap(TestCommand command)
{
return new RetryCommand(command, _tryCount);
}

#endregion

#region Nested RetryCommand Class

/// <summary>
/// The test command for the <see cref="RetryAttribute"/>
/// </summary>
public class RetryCommand : DelegatingTestCommand
{
private readonly int _tryCount;

/// <summary>
/// Initializes a new instance of the <see cref="RetryCommand"/> class.
/// </summary>
/// <param name="innerCommand">The inner command.</param>
/// <param name="tryCount">The maximum number of repetitions</param>
public RetryCommand(TestCommand innerCommand, int tryCount)
: base(innerCommand)
{
_tryCount = tryCount;
}

/// <summary>
/// Runs the test, saving a TestResult in the supplied TestExecutionContext.
/// </summary>
/// <param name="context">The context in which the test should run.</param>
/// <returns>A TestResult</returns>
public override TestResult Execute(TestExecutionContext context)
{
int count = _tryCount;

while (count-- > 0)
{
try
{
context.CurrentResult = innerCommand.Execute(context);
}
// Commands are supposed to catch exceptions, but some don't
// and we want to look at restructuring the API in the future.
catch (Exception ex)
{
if (context.CurrentResult == null) context.CurrentResult = context.CurrentTest.MakeTestResult();
context.CurrentResult.RecordException(ex);
}

if (context.CurrentResult.ResultState != ResultState.Failure && context.CurrentResult.ResultState.Status != ResultState.Failure.Status)
break;

// Clear result for retry
if (count > 0)
{
context.CurrentResult = context.CurrentTest.MakeTestResult();
context.CurrentRepeatCount++; // increment Retry count for next iteration. will only happen if we are guaranteed another iteration
}
}

return context.CurrentResult;
}
}

#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ public void TextBox_PageLoadedTest()
}

[Test]
[AutoRetry]
public void TextBox_TextChanging_Capitalize()
{
Run("UITests.Shared.Windows_UI_Xaml_Controls.TextBoxTests.TextBox_TextChanging");
Expand All @@ -308,6 +309,7 @@ public void TextBox_TextChanging_Capitalize()
}

[Test]
[AutoRetry]
public void TextBox_TextChanging_Limit()
{
Run("UITests.Shared.Windows_UI_Xaml_Controls.TextBoxTests.TextBox_TextChanging");
Expand Down Expand Up @@ -337,6 +339,7 @@ public void TextBox_TextChanging_Limit()
}

[Test]
[AutoRetry]
public void TextBox_BeforeTextChanging()
{
Run("UITests.Shared.Windows_UI_Xaml_Controls.TextBoxTests.TextBox_BeforeTextChanging");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using SamplesApp.UITests.TestFramework;
using Uno.UITest.Helpers;
using Uno.UITest.Helpers.Queries;
using Xamarin.UITest;
Expand All @@ -14,6 +15,7 @@ namespace SamplesApp.UITests
partial class UnoSamples_Tests : SampleControlUITestBase
{
[Test]
[AutoRetry]
public void StaticResource_XAML_Validation()
{
Run("UITests.Shared.Resources.StaticResource.StaticResource_Simple");
Expand All @@ -26,6 +28,7 @@ public void StaticResource_XAML_Validation()
}

[Test]
[AutoRetry]
public void StaticResource_CSharp_Validation()
{
Run("UITests.Shared.Resources.StaticResource.StaticResource_Simple");
Expand All @@ -38,6 +41,7 @@ public void StaticResource_CSharp_Validation()
}

[Test]
[AutoRetry]
public void StaticResource_Converter_Validation()
{
Run("UITests.Shared.Resources.StaticResource.StaticResource_Simple");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace SamplesApp.UITests.Windows_UI_Xaml_Controls.ComboBoxTests
public partial class ComboBoxTests_Tests : SampleControlUITestBase
{
[Test]
[AutoRetry]
[ActivePlatforms(Platform.iOS)]
public void ComboBoxTests_PickerDefaultValue()
{
Expand All @@ -27,6 +28,7 @@ public void ComboBoxTests_PickerDefaultValue()
}

[Test]
[AutoRetry]
public void ComboBoxTests_Kidnapping()
{
Run("UITests.Shared.Windows_UI_Xaml_Controls.ComboBox.ComboBox_ComboBoxItem_Selection");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace SamplesApp.UITests.Windows_UI_Xaml_Controls.DatePickerTests
public partial class DatePickerTests_Tests : SampleControlUITestBase
{
[Test]
[AutoRetry]
[ActivePlatforms(Platform.iOS, Platform.Android)]
public void DatePickerFlyout_HasDataContextTest()
{
Expand All @@ -34,6 +35,7 @@ public void DatePickerFlyout_HasDataContextTest()
}

[Test]
[AutoRetry]
[ActivePlatforms(Platform.iOS)]
public void DatePickerFlyout_HasContentTest()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace SamplesApp.UITests.Windows_UI_Xaml_Controls.ListViewTests
public partial class ListViewTests_Tests : SampleControlUITestBase
{
[Test]
[AutoRetry]
public void ListView_ListViewVariableItemHeightLong_InitializesTest()
{
Run("SamplesApp.Windows_UI_Xaml_Controls.ListView.ListViewVariableItemHeightLong");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public void TimePickerFlyout_ApplyChanges()
}

[Test]
[AutoRetry]
public void TimePickerFlyout_DoesntApplyDefaultTime()
{
Run("UITests.Shared.Windows_UI_Xaml_Controls.TimePicker.Sample1");
Expand All @@ -109,6 +110,7 @@ public void TimePickerFlyout_DoesntApplyDefaultTime()
}

[Test]
[AutoRetry]
[ActivePlatforms(Platform.iOS, Platform.Android)]
public void TimePickerFlyout_HasDataContextTest()
{
Expand All @@ -128,6 +130,7 @@ public void TimePickerFlyout_HasDataContextTest()
}

[Test]
[AutoRetry]
[ActivePlatforms(Platform.iOS)]
public void TimePickerFlyout_HasContentTest()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using SamplesApp.UITests.TestFramework;
using Uno.UITest.Helpers.Queries;
using Query = System.Func<Uno.UITest.IAppQuery, Uno.UITest.IAppQuery>;
using StringQuery = System.Func<Uno.UITest.IAppQuery, Uno.UITest.IAppTypedSelector<string>>;
Expand All @@ -13,7 +14,7 @@ namespace SamplesApp.UITests.Windows_UI_Xaml_Controls.TimePickerTests
public class DragCoordinates_Tests : SampleControlUITestBase
{
[Test]
[Ignore("https://github.com/unoplatform/uno/issues/1257")]
[ActivePlatforms(Platform.iOS, Platform.Browser)] // Android is disabled https://github.com/unoplatform/uno/issues/1257
public void DragBorder01()
{
Run("UITests.Shared.Windows_UI_Xaml_Input.Pointers.DragCoordinates_Automated");
Expand Down

0 comments on commit 4d43497

Please sign in to comment.