hellboy
hellboy

Reputation: 1618

DataSource attribute for unit test method

I try to use CSV data source in Device unit test (WinCE/Pocket PC2003 Emulator)

I have added source in using wizard in Data Connection String property:

using Microsoft.VisualStudio.TestTools.UnitTesting;
....
[TestMethod()]
[DeploymentItem("Options.txt")]
[DeploymentItem("Options_1.txt")]
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "C:\\...\\Tests\\Data\\LoadSettingsTest.csv", "LoadSettingsTest#csv", DataAccessMethod.Sequential)]
public void LoadSettingsTest()
{
  ...
}

I have following compiler error:

Error 1 The type or namespace name 'DataSource' could not be found (are you missing a using directive or an assembly reference?)

Error 2 The type or namespace name 'DataSourceAttribute' could not be found (are you missing a using directive or an assembly reference?)

What's? Where DataSource is defined? Is data DataSource attribute supported in device unit tests?

Upvotes: 1

Views: 2350

Answers (2)

user153923
user153923

Reputation:

It looks like you are trying to use a Comma Separated Values (CSV) file as a DataSource.

You should convert your CSV data into something a Data Control is familiar with first.

Here is an example:

private const string FIELD_SEP = "\t";
private const string LINE_BREAK = "\r\n";
private const int ONE_KB = 1024;

private System.Data.DataTable GenerateData(string csvDataFile) {
  var sb = new System.Text.StringBuilder();
  using (var file = File.Open(csvDataFile, FileMode.Open, FileAccess.Read)) {
    byte[] buffer = new byte[ONE_KB];
    int len = file.Read(buffer, 0, ONE_KB);
    while (-1 < len) {
      string oneK = Encoding.UTF8.GetString(buffer, 0, len);
      sb.Append(oneK);
      len = file.Read(buffer, 0, ONE_KB);
    }
  }
  var table = new System.Data.DataTable();
  var col1 = table.Columns.Add("ID", typeof(int));
  var col2 = table.Columns.Add("Name", typeof(string));
  var col3 = table.Columns.Add("Date", typeof(DateTime));
  var col4 = table.Columns.Add("Cost", typeof(decimal));
  var lines = sb.ToString().Split(LINE_BREAK.ToArray());
  foreach (var line in lines) {
    System.Data.DataRow row = table.NewRow();
    var fields = line.Split(FIELD_SEP.ToArray());
    row[col1] = int.Parse(fields[0]);
    row[col2] = fields[1];
    row[col3] = DateTime.Parse(fields[2]);
    row[col4] = decimal.Parse(fields[3]);
    table.Rows.Add(row);
  }
  return table;
}

Of course, I have no idea what kind of data you are trying to extract from this CSV file, and there is no error checking. If field[3] were an empty string, decimal.Parse(field[3]) would throw an exception.

Upvotes: 2

Elena
Elena

Reputation: 1946

DataSource is not supported by Device Unit Testing Framework, see Unit Testing Framework (Devices).

Upvotes: 3

Related Questions