private void RunOnCurrentThread() { var previousState = SandboxedThreadState.Capture(); try { Context.CurrentTest = this.Test; Context.CurrentResult = this.Result; Context.Listener.TestStarted(this.Test); Context.StartTime = DateTime.UtcNow; Context.StartTicks = Stopwatch.GetTimestamp(); #if PARALLEL Context.TestWorker = this.TestWorker; #endif Context.EstablishExecutionEnvironment(); State = WorkItemState.Running; PerformWork(); } finally { previousState.Restore(); } }
private IEnumerable <ITestCaseData> GetTestCasesFor(IMethodInfo method) { List <ITestCaseData> data = new List <ITestCaseData>(); try { IEnumerable source; var previousState = SandboxedThreadState.Capture(); try { source = GetTestCaseSource(method); } finally { previousState.Restore(); } if (source != null) { foreach (object item in source) { // First handle two easy cases: // 1. Source is null. This is really an error but if we // throw an exception we simply get an invalid fixture // without good info as to what caused it. Passing a // single null argument will cause an error to be // reported at the test level, in most cases. // 2. User provided an ITestCaseData and we just use it. ITestCaseData parms = item == null ? new TestCaseParameters(new object[] { null }) : item as ITestCaseData; if (parms == null) { object[] args = null; // 3. An array was passed, it may be an object[] // or possibly some other kind of array, which // TestCaseSource can accept. var array = item as Array; if (array != null) { // If array has the same number of elements as parameters // and it does not fit exactly into single existing parameter // we believe that this array contains arguments, not is a bare // argument itself. var parameters = method.GetParameters(); var argsNeeded = parameters.Length; if (argsNeeded > 0 && argsNeeded == array.Length && parameters[0].ParameterType != array.GetType()) { args = new object[array.Length]; for (var i = 0; i < array.Length; i++) { args[i] = array.GetValue(i); } } } if (args == null) { args = new object[] { item }; } parms = new TestCaseParameters(args); } if (this.Category != null) { foreach (string cat in this.Category.Split(new char[] { ',' })) { parms.Properties.Add(PropertyNames.Category, cat); } } data.Add(parms); } } else { data.Clear(); data.Add(new TestCaseParameters(new Exception("The test case source could not be found."))); } } catch (Exception ex) { data.Clear(); data.Add(new TestCaseParameters(ex)); } return(data); }