问题描述
使用Visual Studio数据驱动的测试时遇到问题.我试图将其解构为最简单的示例.我正在使用Visual Studio2012.我创建了一个新的单元测试项目.我正在引用系统数据.
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.
我的代码如下:
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; }
}
}
我有一个非常小的csv文件,已将构建选项"设置为内容"和始终复制".我已经在解决方案中添加了.testsettings
文件,并设置了启用部署,并添加了csv文件.
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.
我尝试了是否使用|DataDirectory|
以及是否指定了完整路径(与Environment.CurrentDirectory获得的路径相同).我已经尝试了"../"和"../../"的变体,以防万一.现在,csv位于项目根目录,与.cs测试代码文件相同.
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.
我尝试了xml和csv的变体.
I have tried variations with xml as well as csv.
TestContext不为null,但DataRow始终为null.
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.
mstest是否在任何地方创建日志,以告诉我是否找不到csv文件,或者是什么特定的错误可能导致DataRow无法填充?
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?
我尝试了以下csv文件:
I have tried the following csv files:
ID
1
2
3
4
和
ID,随便
1,0
2,1
3,2
4,3
到目前为止,还没有骰子.
So far, no dice.
我正在使用ReSharper,它会以某种方式干扰吗?
I am using ReSharper, could it be interfering in some way?
已更新我现在大部分都在工作!我可以使用XML,但是当我使用CSV时,名为ID的列将以ID
UpdatedI 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.
对于遇到类似问题的任何人,我都关闭了Just My Code并启用了Net Framework源代码步进等,以便获得更详细的调试信息.这使我能够确定ReSharper导致了我的问题.我禁用了reshaper并修改了我的属性,如下所示:
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)]
它起作用了(除非另有说明).我仍然怀疑自己的路径中的"bin \ debug",但我很高兴我的DataRow不再为null.谢谢!
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!
有什么想法吗?
推荐答案
今天,当我试图使数据驱动的测试与CSV输入文件一起使用时,我也在努力解决类似的问题.第一列的名称在开始时就有一些垃圾,即ID
而不是ID
.
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
.
原来是编码问题. CSV文件保存在UTF-8中,该文件的开头添加了字节顺序标记,这显然很令人困惑解析器.一旦我将文件保存为ANSI编码,它就会按预期工作.
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.
这篇关于数据驱动MSTest:DataRow始终为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!