2

im trying to load a CSV file using Microsoft Visual Studio 2013. The code:

    [TestMethod]
    [DeploymentItem("data.csv")]
    [DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\data.csv", "data#csv", DataAccessMethod.Sequential)]
    public void Teste_Arquivo_EXEC()
    {
        // DETAIL
        var row = TestContext.DataRow;
        var detail = new EXECDetail();

        detail.codigoVara = Int32.Parse(row["codigoVara"].ToString());
     ....
    }

But, when i run the test, it gives me a System.ArgumentException: The column "codigoVara" does not belong to table. Here is the CSV file (its is copying from source to output dir):

codigoVara,codigoComarca,numeroProcesso,numeroAlvara,tipoPessoa,sequencialMovimento,tipoCredito,codigoAgencia,codigoBanco,contaCorrente,valor,nomeParte,cpfCnpj,idDeposito,sequencialRegistro
3001,1,1235520148140300,14013001005,F,1,15,3372,1,242020,150.42,LEANDRO HERNANDEZ ALMEIDA,84887532253,1,1

I tried with " symbol, like "codigoVara", but without success. I also noted an interesting thing: when i access the column through the index, it works!!!

detail.codigoVara = Int32.Parse(row[0].ToString());

Thanks in advance for any help =)

  • Please see the response for [this answer](http://stackoverflow.com/questions/1110588/data-driven-unit-testing-problem-with-csv-encoding). Changing the encoding on the file worked for me. – julealgon Nov 24 '14 at 14:17

2 Answers2

4

I think your .csv-file is stored in UTF-8 encoding with BOM. If so, then the cause of the error is a bug of MS test runner, it parses column names incorrectly by including BOM to the first column name. Other columns are accessible by their names.

1

When the csv is opened, select in Visual Studio File -> Advanced Save Options. In the new dialogue set Encoding to: Western European (Windows) - Codepage 1252

Solved the problem for me

Tobias Neis
  • 69
  • 1
  • 5