我正在執行一個相當簡單的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