如何使用 CSV 数据源控制数据驱动单元测试中列的类型?

发布于 2024-10-22 08:45:30 字数 191 浏览 8 评论 0原文

我有一个数据驱动的单元测试,它使用 CSV 文件作为其数据源。我的文件中的一列应被视为字符串。它工作正常,直到我添加一行,其中该列的值可以解释为日期。当我这样做时,前面几行的测试开始失败。看来,列中包含“日期”会使其将列中的所有值视为日期。无法解析为日期的值将被赋予 DBNull 值。 有什么办法可以防止这种情况发生吗?也许通过指定数据源中的每一列应被视为什么类型?

I have a data driven unit test that uses a CSV file for its data source. One of the columns in my file is meant to be treated as a string. It works fine until I add a line where the value for that column can be interpreted as a date. When I do this, the tests for the earlier lines start to fail. It appears that having a "date" in the column makes it treat all values in the columns as dates. Values that can't be parsed as a date are then given a DBNull value.
Is there anyway to prevent this? Maybe by specifying what type each column in my data source should be treated as?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱,才寂寞 2024-10-29 08:45:30

根据您提供的信息,我建议尝试在值周围使用双引号 (")。
其次,我总是将所有字段视为字符串,并在代码中调用适当的解析方法。

我在 CSV 文件中执行以下操作:

input,expected
"1600,1","1600,1"
"1600","1600"

一个简单的测试方法读取这两个值。输入被解析为 Double,预期被视为 String。

[DeploymentItem("UnitTest\\TestData.csv"), 
 DeploymentItem("TestData.csv"), TestMethod(),
       DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV",
                  "|DataDirectory|\\TestData.csv", 
                  "TestData#csv", 
                  DataAccessMethod.Sequential)
 ]
 public void Test()
 {
    double input= System.Double.Parse(TestContext.DataRow["input"].ToString());
    string expected = TestContext.DataRow["expected"].ToString();
    Assert.AreEqual(input, expected);
 }

我知道这是一个非常基本的例子,也许我正在做一些不建议的事情。我在 VS2010 中进行单元测试的经验有限,因此请随意提出对此答案的改进建议。

这个答案是基于我读取一些十进制值的问题。这是为了测试我目前正在实施的格式化程序。

Based on the information you gave, I would suggest to try doubles quotes (") around the value's.
Secondly, I always treat all fields as String and call an appropriate parse method in my code.

I do the following in the CSV file:

input,expected
"1600,1","1600,1"
"1600","1600"

A simple test method reads both of the value's. The input is parsed as a Double and the expected is treated as a String.

[DeploymentItem("UnitTest\\TestData.csv"), 
 DeploymentItem("TestData.csv"), TestMethod(),
       DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV",
                  "|DataDirectory|\\TestData.csv", 
                  "TestData#csv", 
                  DataAccessMethod.Sequential)
 ]
 public void Test()
 {
    double input= System.Double.Parse(TestContext.DataRow["input"].ToString());
    string expected = TestContext.DataRow["expected"].ToString();
    Assert.AreEqual(input, expected);
 }

I know this is a very basic example and maybe I'm doing things that are not advised. My experience with Unit Testing in VS2010 is limited, so feel free to suggest improvements to this answer.

This answer is based on a problem of me to read some decimal value's. This for testing a formatter I'm implementing for the moment.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文