3

I am having a problem using Visual Studio data driven testing. I have tried to deconstruct this to the simplest example. I am using Visual Studio 2012. I create a new unit test project. I am referencing system data.

My code looks like this:

namespace UnitTestProject1 
{
    [TestClass]
    public class UnitTest1 
    {
        [DeploymentItem(@"OrderService.csv")]
        [DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "OrderService.csv", "OrderService#csv", DataAccessMethod.Sequential)]
        [TestMethod]
        public void TestMethod1() 
        {
            try 
            {
                Debug.WriteLine(TestContext.DataRow["ID"]);    
            } 
            catch (Exception ex) 
            {
                Assert.Fail();
            }
        }

        public TestContext TestContext { get; set; }
    }
}

I have a very small csv file that I have set the Build Options to to 'Content' and 'Copy Always'. I have added a .testsettings file to the solution, and set enable deployment, and added the csv file.

I have tried this with and without |DataDirectory|, and with/without a full path specified (the same path that I get with Environment.CurrentDirectory). I've tried variations of "../" and "../../" just in case. Right now the csv is at the project root level, same as the .cs test code file.

I have tried variations with xml as well as csv.

TestContext is not null, but DataRow always is.

I have not gotten this to work despite a lot of fiddling with it. I'm not sure what I'm doing wrong.

Does mstest create a log anywhere that would tell me if it is failing to find the csv file, or what specific error might be causing DataRow to fail to populate?

I have tried the following csv files:

ID

1

2

3

4

and

ID, Whatever

1,0

2,1

3,2

4,3

So far, no dice.

I am using ReSharper, could it be interfering in some way?

Updated I have it mostly working now! I am able to use XML, but when I use CSV my column, which is named ID comes back as ID

Not sure why. I've checked the actual file of course, and no weird characters are present.

For anyone having a similar problem, I turned off Just My Code and enabled Net Framework source stepping, etc. so that I could get more detailed debug information. This allowed me to determine that ReSharper was causing me problems. I disabled resharper and modified my attributes like this:

[DeploymentItem("UnitTestProject1\\OrderService.csv")]
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\bin\\Debug\\OrderService.csv", "OrderService#csv", DataAccessMethod.Sequential)]

And it worked (except as noted). I am still suspicious of the "bin\debug" in my path, but I'm just happy my DataRow is no longer null. Thanks!

Any ideas?

abatishchev
  • 92,232
  • 78
  • 284
  • 421
David Back
  • 31
  • 1
  • 4

3 Answers3

2

I was struggling with a similar problem today when trying to make data-driven tests work with CSV input file. The name of the first column had some garbage at the beggining of it, i.e. ID instead of just ID.

It turned out it was an encoding issue. The CSV file was saved in UTF-8 which adds a byte order mark at the beginning, obviously confusing the parser. Once I saved the file in ANSI encoding, it worked as expected.

I know it's an old question, but this information might help someone else ending up on this page.

Damir Arh
  • 17,149
  • 2
  • 38
  • 78
  • Some links for more information: It is important to save the .csv file using the correct encoding. On the FILE menu, choose Advanced Save Options and choose Unicode (UTF-8 without signature) – Codepage 65001 as the encoding. (from: https://msdn.microsoft.com/en-us/library/ee624082.aspx) and this informative answer: http://stackoverflow.com/questions/2223882/whats-different-between-utf-8-and-utf-8-without-bom/38038099#38038099 – Albert Oct 11 '16 at 15:34
1

Have you tried adding it through the properties window?

  • Go to Test menu -> Windows -> Test View -> the tests will load up.
  • Click on the test to alter i.e. TestMethod1 and press F4 (properties).
  • Look for 'Data Source' and click the ellipses next to it
  • It will walk you through a wizard that sets up the attributes properly for the TestMethod

You have the deployment part set up properly, which is normally the big stumbling block.

You also don't have to set the build action to Copy Always as the deployment does this for you. This option is used if you include items like .xml files you use for configs, or icons/images as part of your project.

Update 1:

Also try this tutorial on MSDN.

Update 2:

Try this post, involving ProcMon

Community
  • 1
  • 1
Dominic Zukiewicz
  • 7,704
  • 7
  • 37
  • 57
  • Thank you for your response. I'm using Visual Studio 2012 which does not appear to have a "Test View" option. Only "Test Explorer" which doesn't seem to provide me with a Data Source property. Is there an alternate way to navigate to it in 2012? – David Back Dec 17 '12 at 22:33
  • Check out the link and see if that helps. – Dominic Zukiewicz Dec 17 '12 at 22:40
  • Additional Info: I have moved the directory (to check for permissions issues). I have located the solution in C:\TestMe with wide open permissions. The current path to the csv is C:\TestMe\UnitTestProject1\bin\Debug\B2BOrderService.csv and I have verified that it is being correctly copied here. I haven't found the tutorial link to be helpful, but thank you for trying to help me work through this. – David Back Dec 17 '12 at 22:57
0

I see that you said you tried putting the CSV itself into the testsettings file, but have you tried just putting in the directory?

<Deployment>
 <DeploymentItem filename="Test\Data\" />
</Deployment>

Then your DataSource line will look something like this: [DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\YOURCSV.csv", "YOURCSV#csv", DataAccessMethod.Sequential)]

If you do it this way, you don't need to specify the DeploymentItem line.

Our folder structure looks like this: Trunk\Test\Test\Data

We include: Test\Data in the deployment

We then access Test\Data via the |DataDirectory|\

All CSVs live within the \Data folder

Michiel Bugher
  • 836
  • 1
  • 6
  • 20