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?

4

3 回答 3

2

今天在尝试使数据驱动测试与 CSV 输入文件一起工作时,我遇到了类似的问题。第一列的名称在它的开头有一些垃圾,即ID而不是只是ID.

原来这是一个编码问题。CSV 文件以 UTF-8 保存,它在开头添加了一个字节顺序标记,显然使解析器感到困惑。一旦我以 ANSI 编码保存文件,它就按预期工作。

我知道这是一个老问题,但这些信息可能会帮助其他人最终访问此页面。

于 2013-10-20T08:01:24.890 回答
1

您是否尝试过通过属性窗口添加它?

  • 转到测试菜单 -> Windows -> 测试视图 -> 将加载测试。
  • 单击测试以更改 ieTestMethod1并按 F4(属性)。
  • 查找“数据源”并单击它旁边的省略号
  • 它将引导您完成一个正确设置属性的向导TestMethod

您已经正确设置了部署部分,这通常是最大的绊脚石。

您也不必将构建操作设置为始终复制,因为部署会为您执行此操作。如果您将用于配置的 .xml 文件或图标/图像等项目包含在项目中,则使用此选项。

更新1:

也可以试试MSDN 上的这个教程

更新 2:

试试这个帖子,涉及 ProcMon

于 2012-12-17T22:28:11.123 回答
0

我看到您说您尝试将 CSV 本身放入 testsettings 文件中,但是您是否尝试过仅放入目录中?

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

然后您的 DataSource 行将如下所示: [DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\YOURCSV.csv", "YOURCSV#csv", DataAccessMethod.Sequential)]

如果您这样做,则无需指定 DeploymentItem 行。

我们的文件夹结构如下所示:Trunk\Test\Test\Data

我们在部署中包括:Test\Data

然后我们通过 |DataDirectory|\ 访问 Test\Data

所有 CSV 都位于 \Data 文件夹中

于 2013-02-11T20:22:41.990 回答