C# Nunit测试无法从辅助库项目加载文件
我正试图在一个单独的测试项目中进行nunit测试。当测试包含在同一个项目中时,它们工作得很好,但是当我在单独的项目中创建相同的测试时,它们失败了。但是,当我在解决方案中创建第三个消费者项目时,该项目调用此处测试的相同方法时没有错误,因此问题似乎与nunit无关。下面是一个重现问题的最小示例C# Nunit测试无法从辅助库项目加载文件,c#,nunit,.net-4.5,C#,Nunit,.net 4.5,我正试图在一个单独的测试项目中进行nunit测试。当测试包含在同一个项目中时,它们工作得很好,但是当我在单独的项目中创建相同的测试时,它们失败了。但是,当我在解决方案中创建第三个消费者项目时,该项目调用此处测试的相同方法时没有错误,因此问题似乎与nunit无关。下面是一个重现问题的最小示例 using MyUtilities.FileIO.XML; using NUnit.Framework; [TestFixture] public class ReaderTest { privat
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);
}
}
我不记得必须为nunit做任何额外的配置。如果有必要的话,我正在Windows 10中使用VS 2013。没有一个引用被破坏,我也不明白为什么它们会被锁定。然而,这里是MyUtilities项目中的引用列表,因为错误消息似乎认为它可能是导致问题的原因之一
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);
}
}
log4net
Microsoft.CSharp
nunit.framework
系统
System.ComponentModel.DataAnnotations
系统核心
系统数据
System.Data.DataSetExtensions
命名空间
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);
}
}
}
单独的测试项目必须引用MyUtilities项目,并且引用的CopyLocal属性必须为true。请参见是否为XmlObjectReader类提供了相对文件路径?我已在注释中添加了要求的其他信息。我相信我已经关闭了所有的文件对象访问器,如果我没有关闭,我想当测试也在项目中时,它会失败。为了解决vnikhil的问题,我添加的附加信息显示了传递的路径——绝对路径。您正在使用GetCurrentMethod初始化静态值。您可以通过创建一个静态构造函数,在其中移动logFile属性的初始化,然后在那里设置断点来确认这一点。考虑到我读过的所有内容,这与问题有关是有道理的。@Atoms我从XmoObjectReader中删除了所有log4net用法,包括声明,错误仍然存在。它似乎与log4net无关。它引用的是MyUtilities,CopyLocal为True。