简体   繁体   English

避免在用于单元测试时在 CSV 文件中非自愿键入列

[英]Get around involuntary typing of columns in CSV file when using for unit test

I am using a CSV file to inject data into my test.我正在使用 CSV 文件将数据注入我的测试。

[TestMethod]
[DataSource( CsvData, CsvDir + "TC177023.csv", "TC177023#csv", SEQ )]

The file looks somethings like this: (other strings removed)该文件看起来像这样:(删除了其他字符串)

something,something,Value,something,something,something
,,0xDEADBEEF,,
,,-12,,
,,0,,
,,0,,
,,0,,

I have one column that I have integers in and I wanted to extend this to hexadecimals.我有一列有整数,我想把它扩展到十六进制。 So I took the value from the column "Value" and instead of casting it to an int所以我从“值”列中获取值,而不是将其转换为 int

int value = (int)TestContext["Value"];  

I tried to access the string representing the integer我试图访问代表 integer 的字符串

string text = TestContext.DataRow["Value"].ToString();  

But all I got was a empty string.但我得到的只是一个空字符串。 I wont bother you with all I tried to find out what the problem was.我不会用我试图找出问题所在的一切来打扰你。 I could at least see from inspecting the TestContext object while debugging that the type of the column indeed vas Int32.我至少可以从检查 TestContext object 中看到,同时调试列的类型确实是 vas Int32。

In the end I tried to replace the integer values in the column with strings and, lo and behold, I got my hexadecimal value.最后,我尝试用字符串替换列中的 integer 值,你瞧,我得到了十六进制值。 My conclusion is that, when loading the values, the test framework is "helping" me and sets the type of the column by look on values.我的结论是,在加载值时,测试框架正在“帮助”我并通过查看值来设置列的类型。

now to my question:现在我的问题:

Can I set something somewhere to inhibit this behavour so the MS unit test framework does not help me with this?我可以在某处设置一些东西来抑制这种行为,这样 MS 单元测试框架就不能帮助我吗? I cannot change the framework, I cannot change the int values in that column to something recognised as strings.我无法更改框架,我无法将该列中的 int 值更改为可识别为字符串的值。 I am not the end user of the solution so any workarounds would probably make things worse.我不是该解决方案的最终用户,因此任何变通方法都可能使事情变得更糟。 I just want to get the "raw value" of the "cell" for further processing.我只想获取“单元格”的“原始值”以进行进一步处理。

I have investigated it further and for whose who would have the same question, I write it down here.我已经进一步调查过,对于谁会有同样的问题,我在这里写下来。
And the short answer to my question here is no, I cannot within my limitations.对我的问题的简短回答是否定的,我不能在我的限制范围内。

The long answer is: The test framework is using oledb connection to read from excel and csv files.长答案是:测试框架使用 oledb 连接从 excel 和 csv 文件中读取。 There is a way to make oledb to accept "mixed values", sending the string "IMEX = 1" as Extended Properties in the connection string.有一种方法可以使 oledb 接受“混合值”,将字符串“IMEX = 1”作为连接字符串中的扩展属性发送。
Source: https://yoursandmyideas.com/2011/02/05/how-to-read-or-write-excel-file-using-ace-oledb-data-provider/资料来源: https://yoursandmyideas.com/2011/02/05/how-to-read-or-write-excel-file-using-ace-oledb-data-provider/
Unfortunatly the connection string is set inside the framework code, at least in the open source version, that is accessable on githug and I haven't found a way smuggle in the string.不幸的是,连接字符串设置在框架代码中,至少在开源版本中,可以在 githug 上访问,我还没有找到在字符串中走私的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM