我正在执行一个相当简单的XML串行器/解串器(日志文件分析器)在C#.NET应用程序与VS 2008年我有大约50单元测试,现在的代码的各个部分(主要为各种序列化操作),并且其中一些处理文件I/O时似乎大多是随机失败的。VS2008:文件创建在单元测试中随机失败?
的方式测试是结构化的是,在测试设置的方法,我在创建某个预定的位置,一个新的空文件,并关闭我回去流。然后我对这个文件进行一些基本的测试(根据测试的情况而定)。在清理方法中,我再次删除文件。
的很大一部分(通常为30或以上,虽然数量变化连续运行)我的单元测试将在初始化方法失败,声称他们不能访问我试图创建该文件。我不能确定确切的原因,因为一次运行的测试会失败;它们在单独运行时都会成功。
这里有什么问题?为什么我无法跨多个单元测试访问此文件?
相关方法用于一个单元测试,将失败一些的时间:
[TestInitialize()]
public void LogFileTestInitialize()
{
this.testFolder =
System.Environment.GetFolderPath(
System.Environment.SpecialFolder.LocalApplicationData
);
this.testPath = this.testFolder + "\\empty.lfp";
System.IO.File.Create(this.testPath).Close();
}
[TestMethod()]
public void LogFileConstructorTest()
{
string filePath = this.testPath;
LogFile target = new LogFile(filePath);
Assert.AreNotEqual(null, target);
Assert.AreEqual(this.testPath, target.filePath);
Assert.AreEqual("empty.lfp", target.fileName);
Assert.AreEqual(this.testFolder + "\\empty.lfp.lfpdat", target.metaPath);
}
[TestCleanup()]
public void LogFileTestCleanup()
{
System.IO.File.Delete(this.testPath);
}
而LogFile()
构造:
public LogFile(String filePath)
{
this.entries = new List<Entry>();
this.filePath = filePath;
this.metaPath = filePath + ".lfpdat";
this.fileName = filePath.Substring(filePath.LastIndexOf("\\") + 1);
}
精确错误消息:
初始化方法 LogFileParserTester.LogFileTest.LogFileTestInitialize 抛出异常。 System.IO.IOException: System.IO.IOException:过程 不能访问该文件 'C:\用户\ <用户> \应用程序数据\本地\ empty.lfp' ,因为它被另一个 过程..
你告诉我 - 不VS2008运行测试并发?我想到了这一点,但Google似乎表示只有VS2010支持多个同时测试。 – Tim 2010-04-01 03:42:28