我对CodedUI测试自动化框架非常陌生。我遇到了TestContext
,它有关于测试结果输出和目录的重要信息。 其实我创建了一个静态Logger
类,它将输出数据写入.txt
文件。现在我想在TestResults
文件夹下创建它。每次我运行测试方法时,它都会创建一个文件夹,然后是一些时间戳。我想在该位置创建我的Results.txt
文件。 下面是我使用的代码:我们可以将TextContext.TestDeploymentDir值分配给静态类数据成员吗?
public static class Logger
{
string logLocation = TestContext.TestDeploymentDir + "\\Results.txt";
static Logger() {
File.Create(logLocation);
using (var fs = new FileStream(logLocation, FileMode.Truncate))
{
}
}
public static void ResultLog(int testcasenum,String Expected,String Actual, String textResult)
{
FileInfo outtxt = new FileInfo(logLocation);
StreamWriter logline = outtxt.AppendText();
logline.WriteLine("Test Case : " + testcasenum);
logline.WriteLine("{0},{1},{2}", "Expected - "+Expected, "Actual - "+Actual, "Result - "+textResult);
// flush and close file.
logline.Flush(); logline.Close();
}
}
现在我得到一个编译时错误说A field initializer cannot reference the non-static field, method, or property TestContext.TestDeploymentDir.
不知道如何解决这个错误,或者是否有可能或没有?
构造函数打算做什么? 'var fs'的范围局限于空'using'块,所以除了可能清空输出流之外,它没有任何有用的作用。 – AdrianHHH
@AdrianHHH我只是截断日志文件。没有指定的逻辑来做任何事情。只需在这里清空文件。 –