private void Aggregate(List <MainData> dataList) { Aggregator agg = new Aggregator(); for (int i = 0; i < dataList.Count; i++) { agg.Add(dataList[i].Process, dataList[i].Title); } var aggRes = agg.GetAggregationResult(); List <MainData> aggregatedDataList = new List <MainData>(); for (int i = 0; i < aggRes.GetLength(0); i++) { dataList[aggRes[i, 0]].Frequency = aggRes[i, 1]; aggregatedDataList.Add(dataList[aggRes[i, 0]]); } using (StreamWriter sw = File.AppendText(_persistPerMinFile)) { foreach (var item in aggregatedDataList) { sw.WriteLine(item.ToString()); } } }
protected override object CallTestMethod(object testClassInstance) { try { return(TestMethod.Invoke(testClassInstance, TestMethodArguments)); } catch (Exception e) { if (Settings.TakeScreenshotOnFailure) { try { TakeScreenShot(Test.DisplayName); } catch (Exception exception) { Aggregator.Add(exception); } } if (e is TargetInvocationException && e.InnerException is FakeException) { return(null); } throw; } }
/// <inheritdoc/> protected override Task AfterTestClassStartingAsync() { var ordererAttribute = Class.GetCustomAttributes(typeof(TestCaseOrdererAttribute)).SingleOrDefault(); if (ordererAttribute != null) { TestCaseOrderer = ExtensibilityPointFactory.GetTestCaseOrderer(ordererAttribute); } var testClassTypeInfo = Class.Type.GetTypeInfo(); if (testClassTypeInfo.ImplementedInterfaces.Any(i => i.GetTypeInfo().IsGenericType&& i.GetGenericTypeDefinition() == typeof(ICollectionFixture <>))) { Aggregator.Add(new TestClassException("A test class may not be decorated with ICollectionFixture<> (decorate the test collection class instead).")); } foreach (var interfaceType in testClassTypeInfo.ImplementedInterfaces.Where(i => i.GetTypeInfo().IsGenericType&& i.GetGenericTypeDefinition() == typeof(IClassFixture <>))) { CreateFixture(interfaceType); } if (TestClass.TestCollection.CollectionDefinition != null) { var declarationType = ((IReflectionTypeInfo)TestClass.TestCollection.CollectionDefinition).Type; foreach (var interfaceType in declarationType.GetTypeInfo().ImplementedInterfaces.Where(i => i.GetTypeInfo().IsGenericType&& i.GetGenericTypeDefinition() == typeof(IClassFixture <>))) { CreateFixture(interfaceType); } } return(Task.FromResult(0)); }
protected object?[] CreateTestClassConstructorArguments(IServiceProvider provider) { var unusedArguments = new List <Tuple <int, ParameterInfo> >(); Func <IReadOnlyList <Tuple <int, ParameterInfo> >, string>?formatConstructorArgsMissingMessage = null; var args = new object?[_constructorArguments.Length]; for (var index = 0; index < _constructorArguments.Length; index++) { if (_constructorArguments[index] is DependencyInjectionTestClassRunner.DelayArgument delay) { formatConstructorArgsMissingMessage = delay.FormatConstructorArgsMissingMessage; if (delay.TryGetConstructorArgument(provider, Aggregator, out var arg)) { args[index] = arg; } else { unusedArguments.Add(Tuple.Create(index, delay.Parameter)); } } else { args[index] = _constructorArguments[index]; } } if (unusedArguments.Count > 0 && formatConstructorArgsMissingMessage != null) { Aggregator.Add(new TestClassException(formatConstructorArgsMissingMessage(unusedArguments))); } return(args); }
protected override void CreateCollectionFixture(Type fixtureType) { var ctors = fixtureType.GetTypeInfo().DeclaredConstructors.Where(ci => !ci.IsStatic && ci.IsPublic).ToList(); if (ctors.Count != 1) { Aggregator.Add(new TestClassException("Collection fixture type '" + fixtureType.FullName + "' may only define a single public constructor.")); } else { var ctor = ctors[0]; var missingParameters = new List <ParameterInfo>(); var ctorArgs = ctor.GetParameters().Select(p => { if (!CollectionFixtureMappings.TryGetValue(p.ParameterType, out var arg)) { missingParameters.Add(p); } return(arg); }).ToArray(); if (missingParameters.Count > 0) { var parameters = string.Join(", ", missingParameters.Select(p => $"{p.ParameterType.Name} {p.Name}")); Aggregator.Add(new TestClassException( $"Class fixture type '{fixtureType.FullName}' had one or more unresolved constructor arguments: {parameters}" )); } else { Aggregator.Run(() => CollectionFixtureMappings[fixtureType] = ctor.Invoke(ctorArgs)); } } }
protected override void OnTestClassStarting() { var ordererAttribute = TestClass.GetCustomAttributes(typeof(TestCaseOrdererAttribute)).SingleOrDefault(); if (ordererAttribute != null) { TestCaseOrderer = GetXunitTestCaseOrderer(ordererAttribute); } if (TestClass.Type.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ICollectionFixture <>))) { Aggregator.Add(new TestClassException("A test class may not be decorated with ICollectionFixture<> (decorate the test collection class instead).")); } foreach (var interfaceType in TestClass.Type.GetInterfaces().Where(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IClassFixture <>))) { CreateFixture(interfaceType); } if (TestCollection.CollectionDefinition != null) { var declarationType = ((IReflectionTypeInfo)TestCollection.CollectionDefinition).Type; foreach (var interfaceType in declarationType.GetInterfaces().Where(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IClassFixture <>))) { CreateFixture(interfaceType); } } }
protected override void AfterTestStarting() { base.AfterTestStarting(); if (Aggregator.HasExceptions || SkipReason != null) { return; } try { var scenarioDiscoverer = ScenarioDiscovererFactory.GetDiscoverer(DiagnosticMessageSink, ScenarioTest.ScenarioIdentifier); Scenario = scenarioDiscoverer.GetScenario(ScenarioTest.ScenarioIdentifier); if (Scenario == null) { throw new InvalidOperationException("Scenario is null (ScenarioDiscoverer returned no scenario)."); } if (Scenario is IExecutableScenario executableScenario) { Aggregator .RunAsync(() => executableScenario.ExecuteAsync()) .Wait(); } } catch (Exception e) { Aggregator.Add(e); } }
public void FilteredNest() { var agg = new Aggregator(); var ret1 = new Router { Key = "k1", Retriever = (o, s) => new RouteKey((o as Obj).Key1) }; var ret2 = new Router { Parent = ret1, Key = "k2", Retriever = (o, s) => new RouteKey((o as Obj).Key2), FilterFunc = (o, node, scope) => (o as Obj).Sum > 1 }; ret1.Children.Add(ret2); agg.Register(ret1); agg.Register(new SumCollector { Key = "s", ValueFunction = (o, c, node, s) => (o as Obj).Sum }); foreach (var obj in objs) { agg.Add(obj); } var n = agg.GetResult(); Assert.AreEqual(10, n.GetValue("s")); Assert.AreEqual(10, n.GetValue("k1", "s")); Assert.AreEqual(3, n.GetValue("k1", "a", "s")); Assert.AreEqual(7, n.GetValue("k1", "b", "s")); Assert.AreEqual(2, n.GetValue("k1", "a", "k2", "s")); Assert.AreEqual(null, n.GetValue("k1", "a", "k2", "x", "s")); Assert.AreEqual(4, n.GetValue("k1", "b", "k2", "y", "s")); }
protected override Task <decimal> InvokeTestMethodAsync(object testClassInstance) { Aggregator.Run(() => Timer.Aggregate(() => { var parameterCount = TestMethod.GetParameters().Length; var valueCount = TestMethodArguments == null ? 0 : TestMethodArguments.Length; if (parameterCount != valueCount) { Aggregator.Add( new ArgumentException( $"The test method expected {parameterCount} parameter value{(parameterCount == 1 ? "" : "s")}, but {valueCount} parameter value{(valueCount == 1 ? "" : "s")} {(valueCount == 1 ? "was" : "were")} provided." ) ); } else { var result = CallTestMethod(testClassInstance); var task = result as Task; if (task != null) { task.Wait(); } } })); return(Task.FromResult(Timer.Total)); }
protected MethodInfo SelectTestClassFixtureInjectionMethod(IReflectionTypeInfo Class) { var methods = Class .Type .GetTypeInfo() .GetMethods(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public) .Where(ci => !ci.IsGenericMethod && ci.Name == "SetFixture") .ToList(); var methodGroup = methods .GroupBy(ci => ci.GetParameters().Length) .OrderByDescending(g => g.Count()) .FirstOrDefault(); if (methodGroup == null) { return(null); } if (methodGroup.Count() > 1) { Aggregator.Add( new TestClassException( $"Ambiguous SetFixture method on test class '{Class.Type.FullName}'. " + "Remember that the overload with more parameters is selected." ) ); return(null); } return(methods .OrderByDescending(ci => ci.GetParameters().Length) .FirstOrDefault()); }
protected override async Task <RunSummary> RunTestClassAsync(ITestClass testClass, IReflectionTypeInfo @class, IEnumerable <ObservationTestCase> testCases) { var timer = new ExecutionTimer(); var specification = Activator.CreateInstance(testClass.Class.ToRuntimeType()) as Specification; if (specification == null) { Aggregator.Add(new InvalidOperationException(String.Format("Test class {0} cannot be static, and must derive from Specification.", testClass.Class.Name))); return(FailedSummary); } Aggregator.Run(specification.OnStart); if (Aggregator.HasExceptions) { return(FailedSummary); } var result = await new ObservationTestClassRunner(specification, testClass, @class, testCases, diagnosticMessageSink, MessageBus, TestCaseOrderer, new ExceptionAggregator(Aggregator), CancellationTokenSource).RunAsync(); Aggregator.Run(specification.OnFinish); var disposable = specification as IDisposable; if (disposable != null) { timer.Aggregate(disposable.Dispose); } return(result); }
protected override async Task <RunSummary> RunTestAsync() { try { using (var process = GetOrStartProcess(_ExecutablePath)) using (CascadeDebugging(process.Id)) { var result = await Policy .Handle <DebuggerException>() .Or <EndpointNotFoundException>() .WaitAndRetryAsync(Enumerable.Repeat(TimeSpan.FromMilliseconds(100), 500)) .ExecuteAndCaptureAsync(RunTest); if (result.Outcome != OutcomeType.Successful) { throw result.FinalException; } return(result.Result); } } catch (Exception e) { Aggregator.Add(e); MessageBus.QueueMessage(new ErrorMessage(new[] { TestCase }, e)); throw; } }
/// <summary> /// Invokes the test method on the given test class instance. This method sets up support for "async void" /// test methods, ensures that the test method has the correct number of arguments, then calls <see cref="CallTestMethod"/> /// to do the actual method invocation. It ensure that any async test method is fully completed before returning, and /// returns the measured clock time that the invocation took. /// </summary> /// <param name="testClassInstance">The test class instance</param> /// <returns>Returns the time taken to invoke the test method</returns> protected virtual async Task <decimal> InvokeTestMethodAsync(object testClassInstance) { var oldSyncContext = SynchronizationContext.Current; try { var asyncSyncContext = new AsyncTestSyncContext(oldSyncContext); SetSynchronizationContext(asyncSyncContext); await Aggregator.RunAsync( () => Timer.AggregateAsync( async() => { var parameterCount = TestMethod.GetParameters().Length; var valueCount = TestMethodArguments == null ? 0 : TestMethodArguments.Length; if (parameterCount != valueCount) { Aggregator.Add( new InvalidOperationException( $"The test method expected {parameterCount} parameter value{(parameterCount == 1 ? "" : "s")}, but {valueCount} parameter value{(valueCount == 1 ? "" : "s")} {(valueCount == 1 ? "was" : "were")} provided." ) ); } else { var result = CallTestMethod(testClassInstance); var task = GetTaskFromResult(result); if (task != null) { if (task.Status == TaskStatus.Created) { throw new InvalidOperationException("Test method returned a non-started Task (tasks must be started before being returned)"); } await task; } else { var ex = await asyncSyncContext.WaitForCompletionAsync(); if (ex != null) { Aggregator.Add(ex); } } } } ) ); } finally { SetSynchronizationContext(oldSyncContext); } return(Timer.Total); }
public void explicitly_added_step_as_action() { theAggregator.Add <MonsterSlayed>((party, slayed) => { party.Slayed.Fill(slayed.Name); }); theAggregator.AppliesTo(new EventStream(Guid.NewGuid(), false).Add(new MonsterSlayed())) .ShouldBeTrue(); }
private RunSummary RegisterFailedRunSummary(IXunitTestCase testCase, decimal time, Exception exception) { Aggregator.Add(exception); var caseSummary = new RunSummary { Total = 1, Failed = 1, Time = time }; MessageBus.QueueMessage(new TestFailed(new XunitTest(testCase, testCase.DisplayName), caseSummary.Time, string.Empty, exception)); return(caseSummary); }
/// <summary> /// Selects the constructor to be used for the test class. By default, chooses the parameterless /// constructor. Override to change the constructor selection logic. /// </summary> /// <returns>The constructor to be used for creating the test class.</returns> protected virtual ConstructorInfo SelectTestClassConstructor() { var result = Class.Type.GetTypeInfo().DeclaredConstructors.FirstOrDefault(ci => !ci.IsStatic && ci.GetParameters().Length == 0); if (result == null) { Aggregator.Add(new TestClassException("A test class must have a parameterless constructor.")); } return(result); }
/// <summary> /// Selects the constructor to be used for the test class. By default, chooses the parameterless /// constructor. Override to change the constructor selection logic. /// </summary> /// <returns>The constructor to be used for creating the test class.</returns> protected virtual ConstructorInfo SelectTestClassConstructor() { var result = TestClass.Type.GetConstructor(new Type[0]); if (result == null) { Aggregator.Add(new TestClassException("A test class must have a parameterless constructor.")); } return(result); }
/// <inheritdoc/> protected override ConstructorInfo SelectTestClassConstructor() { var ctors = TestClass.Type.GetConstructors(); if (ctors.Length == 1) { return(ctors[0]); } Aggregator.Add(new TestClassException("A test class may only define a single public constructor.")); return(null); }
protected override object CallTestMethod(object testClassInstance) { var asyncSyncContext = (AsyncTestSyncContext)SynchronizationContext.Current; // // Run the test method inside of our iterator. Note that BenchmarkIterator.Run ensures that only one test // method is running at any given time, so we don't need extra synchronization here. // var benchmarkAttr = (BenchmarkAttribute)TestMethod.GetCustomAttribute(typeof(BenchmarkAttribute)); var iterator = new BenchmarkIteratorImpl(DisplayName, benchmarkAttr.InnerIterationCount); return(iterator.RunAsync(async() => { var success = false; BenchmarkEventSource.Log.BenchmarkStart(BenchmarkConfiguration.Instance.RunId, DisplayName); try { var result = TestMethod.Invoke(testClassInstance, TestMethodArguments); var task = result as Task; if (task != null) { await task; success = true; } else { var ex = await asyncSyncContext.WaitForCompletionAsync(); if (ex == null) { success = true; } else { Aggregator.Add(ex); } } if (iterator.IterationStopReason == "NoIterations") { success = false; throw new Exception("Benchmark did not execute any iterations. Please use one of the iteration methods in Microsoft.Xunit.Performance.Benchmark"); } } finally { var stopReason = success ? iterator.IterationStopReason : "TestFailed"; BenchmarkEventSource.Log.BenchmarkStop(BenchmarkConfiguration.Instance.RunId, DisplayName, stopReason); BenchmarkEventSource.Log.Flush(); } })); }
/// <inheritdoc/> protected override async Task AfterTestClassStartingAsync() { var ordererAttribute = Class.GetCustomAttributes(typeof(TestCaseOrdererAttribute)).SingleOrDefault(); if (ordererAttribute != null) { try { var testCaseOrderer = ExtensibilityPointFactory.GetTestCaseOrderer(DiagnosticMessageSink, ordererAttribute); if (testCaseOrderer != null) { TestCaseOrderer = testCaseOrderer; } else { var args = ordererAttribute.GetConstructorArguments().Cast <string>().ToList(); DiagnosticMessageSink.OnMessage(new DiagnosticMessage($"Could not find type '{args[0]}' in {args[1]} for class-level test case orderer on test class '{TestClass.Class.Name}'")); } } catch (Exception ex) { var innerEx = ex.Unwrap(); var args = ordererAttribute.GetConstructorArguments().Cast <string>().ToList(); DiagnosticMessageSink.OnMessage(new DiagnosticMessage($"Class-level test case orderer '{args[0]}' for test class '{TestClass.Class.Name}' threw '{innerEx.GetType().FullName}' during construction: {innerEx.Message}{Environment.NewLine}{innerEx.StackTrace}")); } } var testClassTypeInfo = Class.Type.GetTypeInfo(); if (testClassTypeInfo.ImplementedInterfaces.Any(i => i.GetTypeInfo().IsGenericType&& i.GetGenericTypeDefinition() == typeof(ICollectionFixture <>))) { Aggregator.Add(new TestClassException("A test class may not be decorated with ICollectionFixture<> (decorate the test collection class instead).")); } var createClassFixtureAsyncTasks = new List <Task>(); foreach (var interfaceType in testClassTypeInfo.ImplementedInterfaces.Where(i => i.GetTypeInfo().IsGenericType&& i.GetGenericTypeDefinition() == typeof(IClassFixture <>))) { createClassFixtureAsyncTasks.Add(CreateClassFixtureAsync(interfaceType.GetTypeInfo().GenericTypeArguments.Single())); } if (TestClass.TestCollection.CollectionDefinition != null) { var declarationType = ((IReflectionTypeInfo)TestClass.TestCollection.CollectionDefinition).Type; foreach (var interfaceType in declarationType.GetTypeInfo().ImplementedInterfaces.Where(i => i.GetTypeInfo().IsGenericType&& i.GetGenericTypeDefinition() == typeof(IClassFixture <>))) { createClassFixtureAsyncTasks.Add(CreateClassFixtureAsync(interfaceType.GetTypeInfo().GenericTypeArguments.Single())); } } await Task.WhenAll(createClassFixtureAsyncTasks); }
/// <summary> /// Invokes the test method on the given test class instance. /// </summary> /// <param name="testClassInstance">The test class instance</param> /// <returns>Returns the time taken to invoke the test method</returns> public virtual async Task <decimal> InvokeTestMethodAsync(object testClassInstance) { var oldSyncContext = SynchronizationContext.Current; try { var asyncSyncContext = new AsyncTestSyncContext(oldSyncContext); SetSynchronizationContext(asyncSyncContext); await Aggregator.RunAsync( () => Timer.AggregateAsync( async() => { var parameterCount = TestMethod.GetParameters().Length; var valueCount = TestMethodArguments == null ? 0 : TestMethodArguments.Length; if (parameterCount != valueCount) { Aggregator.Add( new InvalidOperationException( String.Format("The test method expected {0} parameter value{1}, but {2} parameter value{3} {4} provided.", parameterCount, parameterCount == 1 ? "" : "s", valueCount, valueCount == 1 ? "" : "s", valueCount == 1 ? "was" : "were")) ); } else { var result = TestMethod.Invoke(testClassInstance, TestMethodArguments); var task = result as Task; if (task != null) { await task; } else { var ex = await asyncSyncContext.WaitForCompletionAsync(); if (ex != null) { Aggregator.Add(ex); } } } } ) ); } finally { SetSynchronizationContext(oldSyncContext); } return(Timer.Total); }
private void TestTarget(string[,] input, int[,] expected) { Aggregator target = new Aggregator(); for (int i = 0; i < input.GetLength(0); i++) { target.Add(input[i, 0], input[i, 1]); } int[,] actual = target.GetAggregationResult(); CollectionAssert.AreEqual(expected, actual); }
/// <summary> /// Creates the instance of a class fixture type to be used by the test class. If the fixture can be created, /// it should be placed into the <see cref="ClassFixtureMappings"/> dictionary; if it cannot, then the method /// should record the error by calling <code>Aggregator.Add</code>. /// </summary> /// <param name="fixtureType">The type of the fixture to be created</param> protected virtual void CreateClassFixture(Type fixtureType) { if (fixtureType is null) { throw new ArgumentNullException(nameof(fixtureType)); } var ctors = fixtureType.GetTypeInfo() .DeclaredConstructors .Where(ci => !ci.IsStatic && ci.IsPublic) .ToList(); if (ctors.Count != 1) { Aggregator.Add(new TestClassException($"Class fixture type '{fixtureType.FullName}' may only define a single public constructor.")); return; } var ctor = ctors[0]; var missingParameters = new List <ParameterInfo>(); var ctorArgs = ctor.GetParameters().Select(p => { object arg; if (p.ParameterType == typeof(IMessageSink)) { arg = DiagnosticMessageSink; } else if (!collectionFixtureMappings.TryGetValue(p.ParameterType, out arg)) { arg = this.serviceProvider?.GetService(p.ParameterType); } if (arg == null) { missingParameters.Add(p); } return(arg); }).ToArray(); if (missingParameters.Count > 0) { Aggregator.Add(new TestClassException( $"Class fixture type '{fixtureType.FullName}' had one or more unresolved constructor arguments:" + $" {string.Join(", ", missingParameters.Select(p => $"{p.ParameterType.Name} {p.Name}"))}")); } else { Aggregator.Run(() => ClassFixtureMappings[fixtureType] = ctor.Invoke(ctorArgs)); } }
public void GetSampleFrequencyAddPoints() { if (FilePath == null) { return; } Debug.WriteLine("GetSampleFrequencyAddPoints"); PointsCount = 0; try { using (var wavFileReader = new WaveFileReader(FilePath)) { AudioVM.WaveFormat = wavFileReader.WaveFormat; audioVM.Duration = wavFileReader.TotalTime; Aggregator.NotificationCount = (int)((AudioVM.WaveFormat.SampleRate / (1000 * scale)) * AudioVM.WaveFormat.Channels); // * 22; // 100 * 22) / 22 ; //(int)(wavFileReader.WaveFormat.AverageBytesPerSecond * 8/2 * scale) / 100; Aggregator.RaiseRestart(); while (true) { var frame = wavFileReader.ReadNextSampleFrame(); // Read(buff, 0, 400); if (frame == null) { break; } foreach (var b in frame) { try { Aggregator.Add(b); } catch (Exception e) { Debug.WriteLine(e.ToString()); } } } } RaisePropertyChanged("PointsCount"); RaisePropertyChanged("SampledFrequency"); } catch (Exception ex) { Debug.WriteLine(ex.ToString()); } }
/// <inheritdoc/> protected override async Task AfterTestCaseStartingAsync() { await base.AfterTestCaseStartingAsync(); try { var dataAttributes = TestCase.TestMethod.Method.GetCustomAttributes(typeof(DataAttribute)); foreach (var dataAttribute in dataAttributes) { var discovererAttribute = dataAttribute.GetCustomAttributes(typeof(DataDiscovererAttribute)).First(); var args = discovererAttribute.GetConstructorArguments().Cast <string>().ToList(); var discovererType = SerializationHelper.GetType(args[1], args[0]); var discoverer = ExtensibilityPointFactory.GetDataDiscoverer(diagnosticMessageSink, discovererType); IEnumerable <object[]> data = discoverer.GetData(dataAttribute, TestCase.TestMethod.Method); if (data == null) { Aggregator.Add(new InvalidOperationException($"Test data returned null for {TestCase.TestMethod.TestClass.Class.Name}.{TestCase.TestMethod.Method.Name}. Make sure it is statically initialized before this test method is called.")); continue; } foreach (var dataRow in data) { toDispose.AddRange(dataRow.OfType <IDisposable>()); ITypeInfo[] resolvedTypes = null; var methodToRun = TestMethod; var convertedDataRow = methodToRun.ResolveMethodArguments(dataRow); if (methodToRun.IsGenericMethodDefinition) { resolvedTypes = TestCase.TestMethod.Method.ResolveGenericTypes(convertedDataRow); methodToRun = methodToRun.MakeGenericMethod(resolvedTypes.Select(t => ((IReflectionTypeInfo)t).Type).ToArray()); } var parameterTypes = methodToRun.GetParameters().Select(p => p.ParameterType).ToArray(); convertedDataRow = Reflector.ConvertArguments(convertedDataRow, parameterTypes); var theoryDisplayName = TestCase.TestMethod.Method.GetDisplayNameWithArguments(DisplayName, convertedDataRow, resolvedTypes); var test = new XunitTest(TestCase, theoryDisplayName); var skipReason = SkipReason ?? dataAttribute.GetNamedArgument <string>("Skip"); testRunners.Add(new XunitTestRunner(test, MessageBus, TestClass, ConstructorArguments, methodToRun, convertedDataRow, skipReason, BeforeAfterAttributes, Aggregator, CancellationTokenSource)); } } } catch (Exception ex) { // Stash the exception so we can surface it during RunTestAsync dataDiscoveryException = ex; } }
/// <summary> /// Creates the arguments for the test class constructor. Attempts to resolve each parameter /// individually, and adds an error when the constructor arguments cannot all be provided. /// If the class is static, does not look for constructor, since one will not be needed. /// </summary> /// <returns>The test class constructor arguments.</returns> protected virtual object[] CreateTestClassConstructorArguments() { var isStaticClass = Class.Type.GetTypeInfo().IsAbstract&& Class.Type.GetTypeInfo().IsSealed; if (!isStaticClass) { var ctor = SelectTestClassConstructor(); if (ctor != null) { var unusedArguments = new List <Tuple <int, ParameterInfo> >(); var parameters = ctor.GetParameters(); object[] constructorArguments = new object[parameters.Length]; for (int idx = 0; idx < parameters.Length; ++idx) { var parameter = parameters[idx]; object argumentValue; if (TryGetConstructorArgument(ctor, idx, parameter, out argumentValue)) { constructorArguments[idx] = argumentValue; } else if (parameter.HasDefaultValue) { constructorArguments[idx] = parameter.DefaultValue; } else if (parameter.IsOptional) { constructorArguments[idx] = parameter.ParameterType.GetTypeInfo().GetDefaultValue(); } else if (parameter.GetCustomAttribute <ParamArrayAttribute>() != null) { constructorArguments[idx] = Array.CreateInstance(parameter.ParameterType, 0); } else { unusedArguments.Add(Tuple.Create(idx, parameter)); } } if (unusedArguments.Count > 0) { Aggregator.Add(new TestClassException(FormatConstructorArgsMissingMessage(ctor, unusedArguments))); } return(constructorArguments); } } return(new object[0]); }
private bool FindTestsForTypeAndWrapExceptions(ITypeInfo type, bool includeSourceInformation, IMessageBus messageBus) { try { return(FindTestsForType(type, includeSourceInformation, messageBus)); } catch (Exception ex) { Aggregator.Add(new EnvironmentalWarning { Message = String.Format("Exception during discovery:{0}{1}", Environment.NewLine, ex) }); return(true); // Keep going on to the next type } }
/// <inheritdoc/> protected override ConstructorInfo SelectTestClassConstructor() { var ctors = Class.Type.GetTypeInfo() .DeclaredConstructors .Where(ci => !ci.IsStatic && ci.IsPublic) .ToList(); if (ctors.Count == 1) { return(ctors[0]); } Aggregator.Add(new TestClassException("A test class may only define a single public constructor.")); return(null); }
protected virtual void CreateAssemlbyFixture(Type fixtureType) { ConstructorInfo[] ctors = fixtureType .GetTypeInfo() .DeclaredConstructors .Where(ci => !ci.IsStatic && ci.IsPublic) .ToArray(); if (ctors.Length != 1) { Aggregator .Add( new TestClassException( $"Assembly fixture type '{fixtureType.FullName}' may only define a single public constructor.")); return; } ConstructorInfo ctor = ctors[0]; var missingParameters = new List <ParameterInfo>(); object[] ctorArgs = ctor .GetParameters() .Select(p => { if (p.ParameterType == typeof(IMessageSink)) { return((object)DiagnosticMessageSink); } missingParameters.Add(p); return(null); }) .ToArray(); if (missingParameters.Count > 0) { Aggregator.Add( new TestClassException( $"Collection fixture type '{fixtureType.FullName}' had one or more unresolved constructor arguments: " + string.Join(", ", missingParameters.Select(p => $"{p.ParameterType.Name} {p.Name}")))); } else { Aggregator.Run(() => AssemblyFixtureMappings[fixtureType] = ctor.Invoke(ctorArgs)); } }
protected override async Task BeforeTestCollectionFinishedAsync() { if (_scenarioRunner != null) { try { await _scenarioRunner.Complete(_report); } catch (Exception ex) { Aggregator.Add(ex); } } await base.BeforeTestCollectionFinishedAsync(); }