防止 TestContext 数据被自动转换
我使用从 .csv 文件读取的数据来参数化我的测试用例。 csv 文件中的一列具有美国格式的简单日期值(作为常规字符串),例如 mm/dd/yyyy
。然而,当数据实际读取并填充到 TestContext 中时,TestContext.DataRow["MyDateColumn"] 实际上返回转换后的 System.DateTime 对象,时间戳为 12:00:00 AM
。我绝对不需要或想要这种自动转换。我该如何阻止这种情况发生?
I am parameterising my test cases by using data read from .csv files. One of the columns in the csv file has simple date values (as regular strings) in US format, eg mm/dd/yyyy
. When the data is actually read and populated into a TestContext
however, TestContext.DataRow["MyDateColumn"]
actually returns a converted System.DateTime
object, complete with a timestamp of 12:00:00 AM
. I absolutely do not require or want this automatic conversion. How do I stop this from happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果 MyDateColumn 的类型设置为 datetime,那么它将/应该返回一个 datetime 对象。
尝试将 MyDateColumn 的类型更改为字符串,看看是否有效。
更新
更改 CSV 中的日期,使其为“mm/dd/yyyy”,而不是 mm/dd/yyyy。
If the type of the MyDateColumn is set to datetime, then it would/should return a datetime object.
Try changing the type of MyDateColumn to be string and see if that does the trick.
UPDATE
Change dates in the CSV so that they are "mm/dd/yyyy" instead of mm/dd/yyyy.
我得到了这个问题的解决方案:)
我只是把 ' 放在实际数据之前。当我检索数据时,数据按原样出现。在使用它之前,我使用子字符串方法从数据中删除 ' 。
我记得在 Excel 上使用这种技术使数字显示为文本。这对我有用。
I got solution for this :)
I just put ' before actual data. When I retrieve data, data comes as is. Before using it, I remove ' from data with substring method.
I remember using this technique on excel to make numbers appear as text. It worked for me.
我在数据周围加了双引号。这会返回字符串,而不删除引号。
I put double qoutes around the data. That gives back strings, without removing the quotes.