private bool ProcessDataSourceTests(UnitTestElement test, TestMethodInfo testMethodInfo, ITestContext testContext, List <UnitTestElement> tests) { var dataRows = PlatformServiceProvider.Instance.TestDataSource.GetData(testMethodInfo, testContext); if (dataRows == null || !dataRows.Any()) { return(false); } try { int rowIndex = 0; foreach (var dataRow in dataRows) { // TODO: Test serialization rowIndex++; var displayName = string.Format(CultureInfo.CurrentCulture, Resource.DataDrivenResultDisplayName, test.DisplayName, rowIndex); var discoveredTest = test.Clone(); discoveredTest.DisplayName = displayName; discoveredTest.TestMethod.DataType = DynamicDataType.DataSourceAttribute; discoveredTest.TestMethod.SerializedData = DataSerializationHelper.Serialize(new[] { (object)rowIndex }); tests.Add(discoveredTest); } return(true); } finally { testContext.SetDataConnection(null); testContext.SetDataRow(null); } }
public IEnumerable <object> GetData(UTF.ITestMethod testMethodInfo, ITestContext testContext) { // 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) { throw ex; } 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."); // check for rowlength is 0 if (rows.Length == 0) { return(null); } IEnumerable <int> permutation = this.GetPermutation(dataAccessMethod, rows.Length); object[] rowsAfterPermutation = new object[rows.Length]; int index = 0; foreach (int rowIndex in permutation) { rowsAfterPermutation[index++] = rows[rowIndex]; } testContext.SetDataConnection(connection.Connection); return(rowsAfterPermutation); } } catch (Exception ex) { string message = ExceptionExtensions.GetExceptionMessage(ex); throw new Exception(string.Format(CultureInfo.CurrentCulture, Resource.UTA_ErrorDataConnectionFailed, ex.Message), ex); } }