如何在NUnit C#中将文件合并到文件夹路径

问题描述 投票:1回答:1

我用NUnit Testcase编写了一个测试。我已经定义了文件名'irm_xxx_tbbmf_xu.csv.ovr'以及我希望该文件输出的数据。

我已经定义了一个变量processFilePath,它包含该文件的位置以及NUnit TestCase属性参数中的文件名。

我的问题是我写processFilePath的方式如何编写它,以便它像我期望的那样从[NUnit.Framework.TestCase]中找到文件名。目前它并没有将两者结合起来。 Assert.AreEqual会按照我写的方式工作吗?

[NUnit.Framework.TestCase("irm_xxx_tbbmf_xu.csv.ovr", "6677,6677_6677,3001,6")]
[NUnit.Framework.TestCase("irm_xxx_tbbmf_xxx.csv.ovr", "6677,22,344")]
public void ValidateInventoryMeasurement(string path, string expected)
{
    var processFilePath = "/orabin/product//inputs//actuals/";
    var actual = Common.LinuxCommandExecutor.
        RunLinuxcommand("cat " + path);

    Assert.AreEqual(expected, actual);
}
c# linux nunit facebook-c#-sdk
1个回答
1
投票

根据我的评论,在测试中找到要比较的文件时,实际上并没有使用该路径。有多种方法可以组合文件路径 - @ juharr建议使用Path.Combine是最佳实践(特别是在Windows上),但你真的可以使用任何技术进行字符串连接 - 我已经使用字符串插值来执行此操作。

using System; // Other usings 
using NUnit.Framework;

namespace MyTests
{
....


[TestCase("irm_xxx_tbbmf_xu.csv.ovr", "6677,6677_6677,3001,6")]
[TestCase("irm_xxx_tbbmf_xxx.csv.ovr", "6677,22,344")]
public void ValidateInventoryMeasurement(string path, string expected)
{
    const string processFilePath = "/orabin/product/inputs/actuals/";
    var actual = Common.LinuxCommandExecutor
                       .RunLinuxcommand($"cat {processFilePath}{path}");

    Assert.AreEqual(expected, actual);
}

笔记

  • 我假设被测系统是Common.LinuxCommandExecutor
  • processFilePath路径是恒定的,可以变成const string
  • 我已经清理了double slashes //
  • 您可以在NUnit .cs文件的顶部添加使用NUnit.Framework,然后您将不需要重复完整的命名空间NUnit.Framework.TestCase,即只需[TestCase(..)]
  • 您可能需要在cat的输出上观察无关的空白。在这种情况下,您可以考虑:

  Assert.AreEqual(expected, actual.Trim());
© www.soinside.com 2019 - 2024. All rights reserved.