我想要在單獨的測試項目中進行nunit測試。當包含在同一個項目中時,測試工作很好,但是當我在單獨的項目中創建相同的測試時,它們會失敗。但是,當我在解決方案中創建第三個消費者項目時,該項目沒有錯誤調用正在測試的相同方法,所以問題似乎與nunit分離。以下是重現問題的最簡單示例。Nunit測試無法從輔助庫項目加載文件
using MyUtilities.FileIO.XML;
using NUnit.Framework;
[TestFixture]
public class ReaderTest
{
private string FilePath { get; set; }
private string FileName { get; set; }
[Setup]
public void Setup()
{
this.FilePath = @"c:\testdir\";
this.FileName = "testfile.xml";
}
[Test]
public void Reader_Test()
{
// Commenting out this line makes the test pass
var reader = new XmlObjectReader<string> { FilePath = this.FilePath, FileName = this.FileName };
Assert.True(true);
}
}
對XmlObjectReader的調用會導致MyUtilities程序集上的FileLoadException,從而導致測試失敗。如果我評論這個電話,它可以正常工作。從程序集內執行時,相同的測試工作正常。以下是確切的異常語言。
System.IO.FileLoadException : Could not load file or assembly 'MyUtilities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The process cannot access the file because it is being used by another process. (Exception from HRESULT: 0x80070020)
at MyUtilities.Test.FileIO.XML.XmlObjectReaderTest.Reader_Test()
我不記得有做任何額外的配置nunit。如果有問題,我在Windows 10中使用VS 2013。沒有參考文獻被打破,我不明白爲什麼他們中的任何一個會被鎖定。不過,這裏是MyUtilities項目中的參考列表,因爲錯誤消息似乎認爲它可能是導致問題的那些之一。
- log4net的
- Microsoft.CSharp
- nunit.framework
- 系統
- System.ComponentModel.DataAnnotations
- System.Core程序
- System.Data
- System.Data。 DataSetExtensions
- S ystem.Xml
- System.Xml.Linq的
我試着調試到.NET代碼(關閉僅我的代碼)。它通過Setup方法調試得很好,但在退出Setup方法後立即死亡,並立即失敗。我沒有機會調試其他任何東西。對於我可能做什麼,我感到不知所措。
編輯
我已經更新了,我沒有最初提供相應的屬性設置的設置。另外,我在下面包含了XmlObjectReader的實現。
public class XmlObjectReader<T> : IObjectReader<T>
{
private static readonly ILog logFile = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
[Required]
public string FilePath { get; set; }
[Required]
public string FileName { get; set; }
[Required]
public string FullPath { get { return Path.Combine(this.FilePath, this.FileName); } }
public T ReadObject()
{
this.Validate();
var returnValue = default(T);
var serializer = new XmlSerializer(typeof(T));
try
{
if (File.Exists(this.FullPath))
{
logFile.InfoFormat(Resources.DeserializingObject, this.FullPath);
using (var stream = new FileStream(this.FullPath, FileMode.Open))
using (var reader = XmlReader.Create(stream))
{
if (serializer.CanDeserialize(reader))
{ returnValue = (T)serializer.Deserialize(reader); }
}
}
}
catch (Exception ex)
{
logFile.ErrorFormat(Resources.ErrorDuringSerialization, ex);
}
return returnValue;
}
/// <summary>Determines whether the specified object is valid. </summary>
/// <returns>A collection that holds failed-validation information. </returns>
public void Validate()
{
var results = ValidatorHelper.ValidateObjectProperties(this);
if (results.Any())
{
var message = string.Format(Resources.ObjectFailedValidation, this.GetType().Name, results);
logFile.ErrorFormat(message);
throw new ArgumentException(message);
}
}
}
您是否將相對FilePath提供給XmlObjectReader類? – niksofteng
我已在評論中添加了所需的其他信息。我相信我已經關閉了所有的文件對象訪問器,如果我沒有,我會認爲當測試在項目中時也會失敗。爲了解決vnikhil的問題,我添加的附加信息顯示了傳遞的路徑 - 絕對路徑。 – Travis
ILog logFile的靜態初始化引起了我的注意。您正在使用GetCurrentMethod()來初始化一個靜態值。您可以通過創建一個靜態構造函數,移動logFile屬性的初始化來確認,然後在那裏設置一箇中斷點。這是有道理的,這涉及到我讀過的所有內容。 –