Skip to content
Advertisement

How to combine file to a folder path in NUnit C#

I’ve written a test using NUnit Testcase. I’ve defined the file name ‘irm_xxx_tbbmf_xu.csv.ovr’ and what data I expect that file to output.

I’ve defined a variable processFilePath containing the location of where that file will be and the file name within a NUnit TestCase attribute parameter.

My question is the way I’ve written the processFilePath how can I write it so it finds the file name from the [NUnit.Framework.TestCase] as I expect it to. As currently it’s not combining the two. And will the Assert.AreEqual work the way I’ve written it.

[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);
}

Advertisement

Answer

As per my comment, you aren’t actually using the path when locating the file you want to compare in the test. There are any number of ways to combine file paths – @juharr’s suggestion of using Path.Combine is best practice (especially on Windows), but you could really use any technique for string concatenation – I’ve used string interpolation to do this below.

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);
}

Notes

  • I’m assuming that the System Under Test is Common.LinuxCommandExecutor
  • The processFilePath path is constant and can be turned into a const string
  • I’ve cleaned out the double slashes //
  • You can add a using NUnit.Framework at the top of your NUnit .cs file and then you won’t need to repeat the full namespace NUnit.Framework.TestCase, i.e. just [TestCase(..)]
  • You may need to watch for extraneous whitespace on the output of the cat. In which case, you could consider:

  Assert.AreEqual(expected, actual.Trim());
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement