如何使用 CSV 数据源控制数据驱动单元测试中列的类型?
我有一个数据驱动的单元测试,它使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据您提供的信息,我建议尝试在值周围使用双引号 (")。
其次,我总是将所有字段视为字符串,并在代码中调用适当的解析方法。
我在 CSV 文件中执行以下操作:
一个简单的测试方法读取这两个值。输入被解析为 Double,预期被视为 String。
我知道这是一个非常基本的例子,也许我正在做一些不建议的事情。我在 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:
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.
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.