private TestResult[] Invoke(ITestMethod testMethod) { if (_testMethodAttribute != null) { return(_testMethodAttribute.Execute(testMethod)); } return(new[] { testMethod.Invoke(null) }); }
/// <summary> /// Run a data driven test. Test case is executed once for each data row. /// </summary> /// <param name="testContext"> /// The test Context. /// </param> /// <param name="testMethodInfo"> /// The test Method Info. /// </param> /// <param name="testMethod"> /// The test Method. /// </param> /// <param name="executor"> /// The default test method executor. /// </param> /// <returns> /// The results after running all the data driven tests. /// </returns> public UTF.TestResult[] RunDataDrivenTest(UTF.TestContext testContext, UTF.ITestMethod testMethodInfo, ITestMethod testMethod, UTF.TestMethodAttribute executor) { Stopwatch watch = new Stopwatch(); watch.Start(); // Figure out where (as well as the current directory) we could look for data files // for unit tests this means looking at the the location of the test itself List <string> dataFolders = new List <string>(); dataFolders.Add(Path.GetDirectoryName(new Uri(testMethodInfo.MethodInfo.Module.Assembly.CodeBase).LocalPath)); List <UTF.TestResult> dataRowResults = new List <UTF.TestResult>(); // Connect to data source. TestDataConnectionFactory factory = new TestDataConnectionFactory(); string providerNameInvariant; string connectionString; string tableName; UTF.DataAccessMethod dataAccessMethod; try { this.GetConnectionProperties(testMethodInfo.GetAttributes <UTF.DataSourceAttribute>(false)[0], out providerNameInvariant, out connectionString, out tableName, out dataAccessMethod); } catch (Exception ex) { watch.Stop(); var result = new UTF.TestResult(); result.Outcome = UTF.UnitTestOutcome.Failed; result.TestFailureException = ex; result.Duration = watch.Elapsed; return(new UTF.TestResult[] { result }); } try { using (TestDataConnection connection = factory.Create(providerNameInvariant, connectionString, dataFolders)) { DataTable table = connection.ReadTable(tableName, null); DataRow[] rows = table.Select(); Debug.Assert(rows != null, "rows should not be null."); if (rows.Length == 0) { watch.Stop(); var inconclusiveResult = new UTF.TestResult(); inconclusiveResult.Outcome = UTF.UnitTestOutcome.Inconclusive; inconclusiveResult.Duration = watch.Elapsed; return(new UTF.TestResult[] { inconclusiveResult }); } IEnumerable <int> permutation = this.GetPermutation(dataAccessMethod, rows.Length); TestContextImplementation testContextImpl = testContext as TestContextImplementation; try { testContextImpl.SetDataConnection(connection.Connection); // For each data row... foreach (int rowIndex in permutation) { watch.Reset(); watch.Start(); testContextImpl.SetDataRow(rows[rowIndex]); UTF.TestResult[] currentResult = new UTF.TestResult[1]; try { currentResult = executor.Execute(testMethodInfo); } catch (Exception ex) { currentResult[0].Outcome = UTF.UnitTestOutcome.Failed; // Trace whole exception but do not show call stack to the user, only show message. EqtTrace.ErrorIf(EqtTrace.IsErrorEnabled, "Unit Test Adapter threw exception: {0}", ex); } currentResult[0].DatarowIndex = rowIndex; watch.Stop(); currentResult[0].Duration = watch.Elapsed; Debug.Assert(currentResult[0] != null, "current result should not be null."); dataRowResults.Add(currentResult[0]); // Clear the testContext's internal string writer to start afresh for the next datarow iteration testContextImpl.ClearMessages(); } } finally { testContextImpl.SetDataConnection(null); testContextImpl.SetDataRow(null); } } } catch (Exception ex) { string message = ExceptionExtensions.GetExceptionMessage(ex); UTF.TestResult failedResult = new UTF.TestResult(); failedResult.Outcome = UTF.UnitTestOutcome.Error; failedResult.TestFailureException = new Exception(string.Format(CultureInfo.CurrentCulture, Resource.UTA_ErrorDataConnectionFailed, ex.Message), ex); return(new UTF.TestResult[] { failedResult }); } return(dataRowResults.ToArray()); }