简体   繁体   English

Nunit测试无法从辅助库项目加载文件

[英]Nunit test can't load file from secondary library project

I am trying to get nunit tests going in a separate test project. 我正在尝试在单独的测试项目中进行nunit测试。 The tests work just fine when included in the same project, but when I create the same test in a separate project, they fail. 当包含在同一项目中时,这些测试工作正常,但是当我在单独的项目中创建相同的测试时,它们将失败。 However, when I create a third consumer project in the solution, that project has no errors calling into the same methods being tested here, so the problem seems to be isolated to nunit. 但是,当我在解决方案中创建第三个使用者项目时,该项目没有错误,调用了此处正在测试的相同方法,因此问题似乎被隔离到nunit中。 Below is a minimal example that recreates the problem. 下面是一个重现问题的最小示例。

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);
    }
}

The call into XmlObjectReader causes a FileLoadException on the MyUtilities assembly, which causes the test to fail. 调用XmlObjectReader会导致MyUtilities程序集上出现FileLoadException,这将导致测试失败。 If I comment the call, it works fine. 如果我对通话进行评论,则效果很好。 The same test works fine when executed from within the assembly. 从程序集中执行该测试可以正常工作。 Below is the exact exception language. 下面是确切的异常语言。

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()

I don't recall having to do any additional configuration for nunit. 我不记得必须对nunit进行任何其他配置。 I am using VS 2013 in Windows 10, if that matters. 如果重要的话,我正在Windows 10中使用VS 2013。 None of the references are broken, and I don't see why any of them would be locked. 没有一个参考是坏的,我不明白为什么它们中的任何一个都会被锁定。 Nevertheless, here is the list of references in the MyUtilities project, as the error message seems to think it may be one of those causing the problem. 但是,这是MyUtilities项目中的引用列表,因为错误消息似乎认为它可能是导致问题的原因之一。

  • log4net log4net
  • Microsoft.CSharp 微软CSharp
  • nunit.framework nunit.framework
  • System 系统
  • System.ComponentModel.DataAnnotations System.ComponentModel.DataAnnotations
  • System.Core 系统核心
  • System.Data 系统数据
  • System.Data.DataSetExtensions System.Data.DataSetExtensions
  • System.Xml 系统文件
  • System.Xml.Linq System.Xml.Linq

I've tried to debug into the .net code (turning off Just My Code). 我试图调试成.net代码(关闭“仅我的代码”)。 It debugs just fine through the Setup method, but it dies immediately upon exiting the Setup metho, and fails the test immediately. 它可以通过Setup方法进行调试,但是在退出Setup方法后会立即死亡,并立即使测试失败。 I don't get a chance to debug anything else. 我没有机会调试其他任何东西。 I'm at a loss as to what else I might do. 我茫然不知所措。

Edit 编辑

I've updated the setup with the appropriate property settings that I failed to provide initially. 我已使用最初未能提供的适当属性设置更新了设置。 Additionally, I've included the implementation of XmlObjectReader below. 另外,我在下面包括了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);
        }
    }
}

The separate test project must reference the MyUtilities project and the CopyLocal property of the reference must be true. 单独的测试项目必须引用MyUtilities项目,并且引用的CopyLocal属性必须为true。 See https://msdn.microsoft.com/en-us/library/vstudio/t1zz5y8c(v=vs.100).aspx 参见https://msdn.microsoft.com/zh-cn/library/vstudio/t1zz5y8c(v=vs.100).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将此文件加载到NUnit测试中? - How can I load this file into an NUnit Test? 从单独的NUnit测试库引用主项目 - Referencing Main Project from a separate NUnit Test Library .Net Core NUnit 测试项目无法使用 CopyToOutputDirectory 从 appconfig.json 文件中读取值 - .Net Core NUnit Test project couldn't read value from appconfig.json file with CopyToOutputDirectory NUnit测试不读取项目文件 - NUnit test not reading project file Nunit没有在测试项目类库(VS2012)中遇到断点 - Nunit not hitting breakpoints in test project class library (VS2012) 无法在 NUnit 测试项目(System.BadImageFormatException:无法加载文件或程序集)上加载 Oracle.DataAccess dll,但应用程序运行正常 - Cannot load Oracle.DataAccess dll on NUnit Test project (System.BadImageFormatException : Could not load file or assembly) but app runs fine vs2012加载测试期间无法从项目加载图像 - Can't load images from project during vs2012 load test 无法在nunit测试中打开sqlconnection - Can't open sqlconnection within nunit test 无法从 NUnit 单元测试中打印 - Can not print from NUnit unit test 从控制台应用程序调用但未从NUnit测试调用时,库工作 - Library working when called from Console app but not from NUnit test
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM