Nunit测试无法从辅助库项目

时间:2015-09-28 03:18:19

标签: c# nunit .net-4.5

我正在尝试让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
  • 的System.Xml
  • System.Xml.Linq的

我尝试调试.net代码(关闭Just My Code)。它通过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);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

单独的测试项目必须引用MyUtilities项目,并且引用的CopyLocal属性必须为true。见https://msdn.microsoft.com/en-us/library/vstudio/t1zz5y8c(v=vs.100).aspx