/// <summary> /// Gets filter expression from discovery context. /// </summary> /// <param name="context">Discovery context</param> /// <returns>Filter expression.</returns> private ITestCaseFilterExpression GetTestCaseFilterFromDiscoveryContext(IDiscoveryContext context) { try { // GetTestCaseFilter is present in DiscoveryContext but not in IDiscoveryContext interface. MethodInfo methodGetTestCaseFilter = context.GetType().GetRuntimeMethod("GetTestCaseFilter", new[] { typeof(IEnumerable <string>), typeof(Func <string, TestProperty>) }); return((ITestCaseFilterExpression)methodGetTestCaseFilter?.Invoke(context, new object[] { this.supportedProperties.Keys, (Func <string, TestProperty>) this.PropertyProvider })); } catch (TargetInvocationException ex) { throw ex.InnerException; } }
bool GetTestCaseFilterExpressionFromDiscoveryContext(IDiscoveryContext discoveryContext, LoggerHelper logger, out ITestCaseFilterExpression filter) { filter = null; if (discoveryContext is IRunContext runContext) { try { filter = runContext.GetTestCaseFilter(supportedPropertyNames, null); return(true); } catch (TestPlatformException e) { logger.LogWarning("Exception filtering tests: {0}", e.Message); return(false); } } else { try { #if WINDOWS_UAP // on UWP .Net Native Tool Chain we are not able to run methods via invoke, act like no filter was specified for UWP #else // GetTestCaseFilter is present on DiscoveryContext but not in IDiscoveryContext interface var method = discoveryContext.GetType().GetRuntimeMethod("GetTestCaseFilter", new[] { typeof(IEnumerable <string>), typeof(Func <string, TestProperty>) }); filter = (ITestCaseFilterExpression)method?.Invoke(discoveryContext, new object[] { supportedPropertyNames, null }); #endif return(true); } catch (TargetInvocationException e) { if (e?.InnerException is TestPlatformException ex) { logger.LogWarning("Exception filtering tests: {0}", ex.InnerException?.Message); return(false); } throw e.InnerException; } } }
/// <summary> /// Gets filter expression from discovery context. /// </summary> /// <param name="context">Discovery context</param> /// <param name="logger">The logger to log exception messages too.</param> /// <returns>Filter expression.</returns> private ITestCaseFilterExpression GetTestCaseFilterFromDiscoveryContext(IDiscoveryContext context, IMessageLogger logger) { try { // GetTestCaseFilter is present in DiscoveryContext but not in IDiscoveryContext interface. MethodInfo methodGetTestCaseFilter = context.GetType().GetRuntimeMethod("GetTestCaseFilter", new[] { typeof(IEnumerable <string>), typeof(Func <string, TestProperty>) }); return((ITestCaseFilterExpression)methodGetTestCaseFilter?.Invoke(context, new object[] { this.supportedProperties.Keys, (Func <string, TestProperty>) this.PropertyProvider })); } catch (Exception ex) { // In case of UWP .Net Native Tool Chain compilation. Invoking methods via Reflection doesn't work, hence discovery always fails. // Hence throwing exception only if it is of type TargetInvocationException(i.e. Method got invoked but something went wrong in GetTestCaseFilter Method) if (ex is TargetInvocationException) { throw ex.InnerException; } logger.SendMessage(TestMessageLevel.Warning, ex.Message); } return(null); }