Data Driven MSTest: DataRow is always null

Posted by David Back on Stack Overflow See other posts from Stack Overflow or by David Back
Published on 2012-12-17T22:20:24Z Indexed on 2012/12/17 23:03 UTC
Read the original article Hit count: 608

Filed under:
|
|

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.

© Stack Overflow or respective owner

Related posts about c#

Related posts about visual-studio-2012