Reputation: 119
I try to get my nunit TestCases by reading a textfile, which contains a TestCase per line. I'm using the TestCaseSourceAttribute in that case.
[Test, TestCaseSource("TestSelection")]
//TestMethod
public static object[] TestSelection()
{
var amountOfTestCases = GetAmountTestCases(@"path\TestCases.txt");
var result = new object[amountOfTestCases];
for (var i = 0; i < amountOfTestCases; i++)
{
result[i] = new object[] { GetTestCase(i, @"path\TestCases.txt") };
}
return result;
}
With GetAmountTestCases()
I get lines of text within a textfile and GetTestCase()
reads the specific line of text to get my TestCase.
public static int GetAmountTestCases(String path)
{
var lineCount = 0;
using (var reader = File.OpenText(path))
{
while (reader.ReadLine() != null)
{
lineCount++;
}
}
return lineCount;
}
public static String GetTestCase(int lineNum, String path)
{
var lineCount = 0;
String testCase = String.Empty;
using (var reader = File.OpenText(path))
{
while (reader.ReadLine() != null)
{
if (lineCount == lineNum)
{
testCase = reader.ReadLine();
}
else
{
lineCount++;
}
}
}
return testCase;
}
After building the .dll contains test modules with 'null' as content. I cant even Debug without a specific TestCase within my Code.
Upvotes: 0
Views: 233
Reputation: 718
You have quite a complex way of going about it, the following code works for me:
[Test, TestCaseSource("GetMyFileData")]
//Method
public static string[] GetMyFileData()
{
var path = @"C:\temp\MyFile.txt";
return File.ReadAllLine(path)
.ToArray();
}
Please double check if your path is correct.
If it is a relative path to the file that is deployed on build, check if it is configured correctly: its Build Action
should be something like Content
and Copy to output directory
should be Copy of newer
or Copy always
Upvotes: 1