c# - 根据文件输出验证数据c#
问题描述
我有多个 csv 文件。执行我的批处理命令后,将填充这些 csv 文件。我正在使用specflow BDD。
我正在为我的“然后”步骤编写代码。我正在采用的方法是在我的功能文件中,我使用了一个示例表来说明不同类型的状态(见下文)。我在编写将执行文件路径然后再次验证我的预期数据的代码时遇到问题。
所以下面我为我的 Then 步骤编写了代码。我已经说明了 processFilePath,它说明了我的文件所在的路径。我现在想放一段代码,它能够对包含多个文件名的 processFilePath 和路径进行分类。例如 xxx_ccx.csv.ovrr,xxx_bbx.csv.ovrr,xxx_aax.csv.ovrr。
处理完文件后,我想验证我的结果。
[Then("Transfer measure should be generated for (.*)")]
public void ValidateMeasurement(string path, string expected)
{
const string processFilePath = "/orabin/app/product/ff/actuals/";
var actual = Common.LinuxCommandExecutor
.RunLinuxcommand($"cat {processFilePath}{path}");
以下是我期望的文件名和预期输出。我如何进行验证,以便当我 cat 输出下方的文件时,可以针对预期的输出数据进行验证。
("xxx_txrbf_xxxx.csv.ovr", "6677,6677,1001,6"),
("xxx_tsxbf_xxxx.csv.ovrr", "6677,6677,3001,6"),
("xxx_tzxbf_xxxx.csv.ovrr", "6677,6677,2001,6")]")
Assert.AreEqual(expected, actual);
}
解决方案
首先。
路径应由 C# 中的 DirectoryInfo 处理。
并且文件应该通过 DirectoryInfo 检索,以保存 FileInfo 类。
使处理文件和搜索文件等变得如此容易。
外汇
string expectedPath = @"C:\Expected";
string path = @"C:\Test";
DirectoryInfo di = new DirectoryInfo(path);
FileInfo[] files = di.GetFiles();
DirectoryInfo diExpected = new DirectoryInfo(expectedPath);
FileInfo[] expectedFiles = diExpected.GetFiles();
for (int index = 0; index < files.Length; index++)
{
FileInfo currentFile = files[index];
FileInfo currentExpectedFile = expectedFiles[index];
//Expected data, should match "index" data location.
Assert.AreEqual(currentFile.FullName, currentExpectedFile.FullName);
//If you want to assert content:
string actualContent = File.ReadAllText(currentFile.FullName);
string expectedContent = File.ReadAllText(currentExpectedFile.FullName);
Assert.AreEqual(actualContent, expectedContent);
}
我不熟悉 BBD 和 specflow 内容,但我假设您能够修改我的示例以符合您的要求?以便您预期文件的内容与您的实际结果的内容相匹配:
var actual = Common.LinuxCommandExecutor
.RunLinuxcommand($"cat {processFilePath}{path}");
命令。
您还应该断言您的预期文件数与实际文件数相同。
推荐阅读
- c# - 检查类型是否为 IEnumerable 并获取其基本类型
- html - 使用 Xamarin.Forms 在 WebView 中单击 HTML 元素时如何捕获?
- java - 如何滚动直到一个对象在弹出窗口中可见?
- python - matplotlib 3D 线图,整个图形的颜色编码
- sql - 有序查询的第一组值
- java - 在 Spring Boot 应用的 pom.xml 中添加本地 jar
- javascript - 在 Wordpress 站点上显示来自 MSAccess DB 的自定义数据的最佳方式是什么?
- c++ - 如何在 vsprintf 之前确定 va_args 的大小
- css - 如何将这五个列框居中,以便它们与上面的 boostrap 按钮选项卡内联?
- javascript - 邮递员测试检查部分对象值