public override TestResult[] Execute(ITestMethod testMethod) { var diagnosticAttributes = testMethod.GetAttributes <AssertionDiagnosticAttribute>(false); var codeFixAttributes = testMethod.GetAttributes <AssertionCodeFixAttribute>(false); var results = new List <TestResult>(); foreach (var diagnosticAttribute in diagnosticAttributes.Where(attribute => !attribute.Ignore)) { foreach (var assertion in GetTestCases(diagnosticAttribute)) { var result = testMethod.Invoke(new[] { assertion }); result.DisplayName = assertion; results.Add(result); } } foreach (var codeFixAttribute in codeFixAttributes.Where(attribute => !attribute.Ignore)) { foreach (var(oldAssertion, newAssertion) in GetTestCases(codeFixAttribute)) { var result = testMethod.Invoke(new[] { oldAssertion, newAssertion }); result.DisplayName = $"{Environment.NewLine}old: \"{oldAssertion}\" {Environment.NewLine}new: \"{newAssertion}\""; results.Add(result); } } return(results.ToArray()); }
/// <summary> /// Find all data rows and execute. /// </summary> /// <param name="testMethod"> /// The test Method. /// </param> /// <returns> /// The <see cref="TestResult[]"/>. /// </returns> public override TestResult[] Execute(ITestMethod testMethod) { List <DataRowAttribute> dataRows = new List <DataRowAttribute>(); var net46Rows = testMethod.GetAttributes <NET46TargetFramework>(false); if (net46Rows != null && net46Rows.Length > 0 && net46Rows[0].DataRows.Count > 0) { dataRows.AddRange(net46Rows[0].DataRows); } var netcoreappRows = testMethod.GetAttributes <NETCORETargetFramework>(false); if (netcoreappRows != null && netcoreappRows.Length > 0 && netcoreappRows[0].DataRows.Count > 0) { dataRows.AddRange(netcoreappRows[0].DataRows); } if (dataRows.Count == 0) { return(new TestResult[] { new TestResult() { Outcome = UnitTestOutcome.Failed, TestFailureException = new Exception(FrameworkMessages.NoDataRow) } }); } return(RunDataDrivenTest(testMethod, dataRows.ToArray())); }
/// <summary> /// Find all data rows and execute. /// </summary> /// <param name="testMethod"> /// The test Method. /// </param> /// <returns> /// The <see cref="TestResult[]"/>. /// </returns> public override TestResult[] Execute(ITestMethod testMethod) { List <DataRowAttribute> dataRows = new List <DataRowAttribute>(); var netFullRows = testMethod.GetAttributes <NETFullTargetFramework>(false); if (netFullRows != null && netFullRows.Length > 0 && netFullRows[0].DataRows.Count > 0) { dataRows.AddRange(netFullRows[0].DataRows); } var netcoreappRows = testMethod.GetAttributes <NETCORETargetFramework>(false); if (netcoreappRows != null && netcoreappRows.Length > 0 && netcoreappRows[0].DataRows.Count > 0) { dataRows.AddRange(netcoreappRows[0].DataRows); } if (dataRows.Count == 0) { return(new TestResult[] { new TestResult() { Outcome = UnitTestOutcome.Failed, TestFailureException = new Exception("No DataRowAttribute specified. Atleast one DataRowAttribute is required with DataTestMethodAttribute.") } }); } return(RunDataDrivenTest(testMethod, dataRows.ToArray())); }
private TestResult ExecuteWithServer(ITestMethod testMethod) { var arguments = ExtendArguments(testMethod.Arguments); var filesToCreate = testMethod.GetAttributes <CreateTestSpecificFileAttribute>(false); TestEnvironmentImpl.AddBeforeAfterTest(async() => { var interpreterConfiguration = GetInterpreterConfiguration(arguments); var rootUri = TestSpecificRootUri ? TestData.GetTestSpecificRootUri() : null; foreach (var file in filesToCreate) { await TestData.CreateTestSpecificFileAsync(file.RelativeFilePath, file.Content); } var server = await new Server().InitializeAsync(interpreterConfiguration, rootUri); if (DefaultTypeshedPath) { var limits = server.Analyzer.Limits; limits.UseTypeStubPackages = true; server.Analyzer.Limits = limits; server.Analyzer.SetTypeStubPaths(new[] { TestData.GetDefaultTypeshedPath() }); } arguments[0] = server; return(server); }); return(testMethod.Invoke(arguments)); }
/// <summary> /// Executes the test method on the UI Thread. /// </summary> /// <param name="testMethod"> /// The test method. /// </param> /// <returns> /// An array of <see cref="TestResult"/> instances. /// </returns> /// Throws <exception cref="NotSupportedException"> when run on an async test method. /// </exception> public override TestResult[] Execute(ITestMethod testMethod) { var attrib = testMethod.GetAttributes <AsyncStateMachineAttribute>(false); if (attrib.Length > 0) { throw new NotSupportedException(FrameworkMessages.AsyncUITestMethodNotSupported); } TestResult result = null; var dispatcher = DispatcherQueue ?? global::Microsoft.UI.Xaml.Window.Current?.DispatcherQueue; if (dispatcher == null) { throw new InvalidOperationException(FrameworkMessages.AsyncUITestMethodWithNoDispatcherQueue); } if (dispatcher.HasThreadAccess) { try { result = testMethod.Invoke(Array.Empty <object>()); } catch (Exception e) { return(new TestResult[] { new TestResult { TestFailureException = e } }); } } else { var taskCompletionSource = new global::System.Threading.Tasks.TaskCompletionSource <object>(); if (!dispatcher.TryEnqueue(Microsoft.UI.Dispatching.DispatcherQueuePriority.Normal, () => { try { result = testMethod.Invoke(Array.Empty <object>()); taskCompletionSource.SetResult(null); } catch (Exception e) { result = new TestResult { TestFailureException = e }; taskCompletionSource.SetException(e); } })) { taskCompletionSource.SetResult(null); } taskCompletionSource.Task.GetAwaiter().GetResult(); } return(new TestResult[] { result }); }
private IEnumerable <IgnoreIfAttribute> FindAttributes(ITestMethod testMethod) { // Look for an [IgnoreIf] on the method, including any virtuals this method overrides var ignoreAttributes = new List <IgnoreIfAttribute>(); ignoreAttributes.AddRange(testMethod.GetAttributes <IgnoreIfAttribute>(inherit: true)); return(ignoreAttributes); }
public override TestResult[] Execute( ITestMethod testMethod) { // NOTE // This implementation will need to be refactored as we add more // execution variations. int retryCount = 1; int repeatCount = 1; Attribute[] attr = testMethod.GetAllAttributes(false); if (attr == null) { Attribute[] r1 = testMethod.GetAttributes <RetryAttribute>(false); var attr2 = new List <Attribute>(); attr2.AddRange(r1); r1 = testMethod.GetAttributes <RepeatAttribute>(false); attr2.AddRange(r1); attr = attr2.ToArray(); } if (attr != null) { foreach (Attribute a in attr) { if (a is RetryAttribute retryAttr) { retryCount = retryAttr.Value; } if (a is RepeatAttribute repeatAttr) { repeatCount = repeatAttr.Value; } } } var res = executeWithRepeatAndRetry(testMethod, repeatCount, retryCount); return(res); }
public override TestResult[] Execute(ITestMethod testMethod) { var deploymentItems = testMethod.GetAttributes <DeploymentItemAttribute>(false); foreach (var item in deploymentItems) { item.Deploy(); } return(base.Execute(testMethod)); }
/// <summary> /// Executes a test method. /// </summary> /// <param name="testMethod">The test method to execute.</param> /// <returns>An array of TestResult objects that represent the outcome(s) of the test.</returns> /// <remarks>Extensions can override this method to customize running a TestMethod.</remarks> public virtual TestResult[] Execute(ITestMethod testMethod) { DataRowAttribute[] dataRows = testMethod.GetAttributes <DataRowAttribute>(false); if (dataRows == null || dataRows.Length == 0) { return(new TestResult[] { testMethod.Invoke(null) }); } return(DataTestMethodAttribute.RunDataDrivenTest(testMethod, dataRows)); }
/// <summary> /// Executes a test method. /// </summary> /// <param name="testMethod">The test method to execute.</param> /// <returns>An array of TestResult objects that represent the outcome(s) of the test.</returns> /// <remarks>Extensions can override this method to customize running a TestMethod.</remarks> public virtual TestResult[] Execute(ITestMethod testMethod) { ITestDataSource[] dataSources = testMethod.GetAttributes <Attribute>(true)?.Where(a => a is ITestDataSource).OfType <ITestDataSource>().ToArray(); if (dataSources == null || dataSources.Length == 0) { return(new TestResult[] { testMethod.Invoke(null) }); } return(DataTestMethodAttribute.RunDataDrivenTest(testMethod, dataSources)); }
/// <summary> /// Find all data rows and execute. /// </summary> /// <param name="testMethod"> /// The test Method. /// </param> /// <returns> /// An array of <see cref="TestResult"/>. /// </returns> public override TestResult[] Execute(ITestMethod testMethod) { DataRowAttribute[] dataRows = testMethod.GetAttributes <DataRowAttribute>(false); if (dataRows == null || dataRows.Length == 0) { return(new TestResult[] { new TestResult() { Outcome = UnitTestOutcome.Failed, TestFailureException = new Exception(FrameworkMessages.NoDataRow) } }); } return(RunDataDrivenTest(testMethod, dataRows)); }
/// <summary> /// Find all data rows and execute. /// </summary> /// <param name="testMethod"> /// The test Method. /// </param> /// <returns> /// An array of <see cref="TestResult"/>. /// </returns> public override TestResult[] Execute(ITestMethod testMethod) { ITestDataSource[] dataSources = testMethod.GetAttributes <Attribute>(true)?.Where(a => a is ITestDataSource).OfType <ITestDataSource>().ToArray(); if (dataSources == null || dataSources.Length == 0) { return(new TestResult[] { new TestResult() { Outcome = UnitTestOutcome.Failed, TestFailureException = new Exception(FrameworkMessages.NoDataRow) } }); } return(RunDataDrivenTest(testMethod, dataSources)); }
public override TestResult[] Execute(ITestMethod testMethod) { if (testMethod.GetAttributes <AsyncStateMachineAttribute>(false).Length != 0) { throw new NotSupportedException("async TestMethod with UITestMethodAttribute are not supported. Either remove async, wrap the code with RunAsync() or use TestMethodAttribute."); } TestResult result = null; CoreApplication.MainView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { result = testMethod.Invoke(new object[0]); }).AsTask().GetAwaiter().GetResult(); return(new TestResult[] { result }); }
private static IEnumerable <TAttribute> GetMethodAndClassHierarchyAttributes <TAttribute>(ITestMethod testMethod) where TAttribute : Attribute { var methodAttributes = testMethod.GetAttributes <TAttribute>(true).ToList(); var classAttributes = new List <TAttribute>(); var type = testMethod.MethodInfo.DeclaringType; while (type != null) { classAttributes.AddRange(type.GetCustomAttributes <TAttribute>(true)); type = type.DeclaringType; } return(methodAttributes.Concat(classAttributes)); }
private IEnumerable <IgnoreIfAttribute> FindAttributes(ITestMethod testMethod) { // Look for an [IgnoreIf] on the method, including any virtuals this method overrides var ignoreAttributes = new List <IgnoreIfAttribute>(); ignoreAttributes.AddRange(testMethod.GetAttributes <IgnoreIfAttribute>(inherit: true)); // Walk the class hierarchy looking for an [IgnoreIf] attribute var type = testMethod.MethodInfo.DeclaringType; while (type != null) { ignoreAttributes.AddRange(type.GetCustomAttributes <IgnoreIfAttribute>(inherit: true)); type = type.DeclaringType; } return(ignoreAttributes); }
/// <summary> /// Executes the test method on the UI Thread. /// </summary> /// <param name="testMethod"> /// The test method. /// </param> /// <returns> /// An array of <see cref="TestResult"/> instances. /// </returns> /// Throws <exception cref="NotSupportedException"> when run on an async test method. /// </exception> public override TestResult[] Execute(ITestMethod testMethod) { var attrib = testMethod.GetAttributes <AsyncStateMachineAttribute>(false); if (attrib.Length > 0) { throw new NotSupportedException(FrameworkMessages.AsyncUITestMethodNotSupported); } TestResult result = null; Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync( Windows.UI.Core.CoreDispatcherPriority.Normal, () => { result = testMethod.Invoke(new object[] { }); }).AsTask().GetAwaiter().GetResult(); return(new TestResult[] { result }); }
public TAttributeType[] GetAttributes <TAttributeType>(bool inherit) where TAttributeType : Attribute => _target.GetAttributes <TAttributeType>(inherit);
public TAttributeType[] GetAttributes <TAttributeType>(bool inherit) where TAttributeType : Attribute { return(_target.GetAttributes <TAttributeType>(inherit)); }