我试图通过读取一个文本文件来获取我的nunit TestCases,该文件每行包含一个TestCase。我在这种情况下使用了TestCaseSourceAttribut。
[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;
}
[使用GetAmountTestCases(),我可以在文本文件中获取文本行,而GetTestCase()可以读取特定的文本行来获取我的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;
}
构建.dll后,包含内容为'null'的测试模块。没有代码中的特定TestCase,我什至无法调试。
您的处理方式非常复杂,以下代码对我有用:
[Test, TestCaseSource("GetMyFileData")]
//Method
public static string[] GetMyFileData()
{
var path = @"C:\temp\MyFile.txt";
return File.ReadAllLine(path)
.ToArray();
}
请仔细检查您的路径是否正确。
[如果它是在构建中部署的文件的相对路径,请检查其配置是否正确:其Build Action
应该类似于Content
,而Copy to output directory
应该是Copy of newer
或Copy always