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
processFilePathpath is constant and can be turned into aconst string - I’ve cleaned out the double slashes //
- You can add a using
NUnit.Frameworkat the top of your NUnit .cs file and then you won’t need to repeat the full namespaceNUnit.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());