首页 > 解决方案 > 根据文件输出验证数据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#bddspecflow

解决方案


首先。

路径应由 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}");

命令。

您还应该断言您的预期文件数与实际文件数相同。


推荐阅读